windows平台获取程序所在路径

本文探讨了在Qt和Windows API环境下,如何在DLL中获取当前目录,发现动态库中的路径依赖于主程序传递。通过实例展示了如何在TestLibDir的DLL中使用QDir::currentPath(),并指出实际路径是由主程序的QCoreApplication::applicationDirPath()决定的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.控制台和界面程序

qt:
控制台程序:QCoreApplication::applicationDirPath()
界面程序:QApplication::applicationDirPath();
windows API:
GetModuleFileName

2动态库

qt:
动态库中无法使用QCoreApplication::applicationDirPath()或QApplication::applicationDirPath();

测试:

class TESTLIBDIRSHARED_EXPORT TestLibDir
{

public:
    TestLibDir();
    void PrintCurrentDir();
};

void TestLibDir::PrintCurrentDir()
{
    qDebug()<<QDir::currentPath();
}

其dll生成目录为 \TestLibDir\TestLibDir\Moc\Debug\debug

调用:

#include <QCoreApplication>
#include "testlibdir.h"
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    TestLibDir te;
    te.PrintCurrentDir();
    qDebug()<<QCoreApplication::applicationDirPath();
    return a.exec();
}

结果:
“/TestLibDir/TestLibDirTest/Moc/Debug”
“/TestLibDir/TestLibDirTest/Moc/Debug/debug”

所以dll工程中的路径由主程序调用传参传入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值