0

问题是:我有一个 Qt 应用程序,它动态加载一个库并从这个库执行一个函数(参见代码示例)

...
QLibrary library( "d:/Libs/MyLib.dll" );
if ( !library.load( ) )
    qDebug( ) << library.errorString( );
if ( library.load( ) )
    qDebug( ) << "library loaded";

typedef void ( *StartPrototype ) ( );
StartPrototype Start = ( StartPrototype ) library.resolve( "Start" );
if ( !Start )
{
    qDebug() << "Start( ) failed!";
}

Start();
...

Start() 函数从加载的 dll 中的定义如下:

extern "C" __declspec( dllexport ) void Start( )
{
    ofstream myfile( "example.txt" );
    if ( myfile.is_open( ) )
    {
        myfile << "This is a line.\n";
        myfile.close( );
    }
}

我的 Qt 应用程序位于d:/Projects/MyApp.exe,当我运行它时,example.txt出现在d:/Projects/example.txt中(作为MyApp.exe的当前工作目录)

但我想将example.txt保存在我的 dll 所在的同一目录中(d:/Libs/example.txt)。我可以在不直接指定路径的情况下这样做吗?我的意思是有没有办法为加载的库和其中的所有函数指定工作目录?如何将 dll 路径设置为 dll 函数的当前工作目录(例如创建文件)。谢谢。

4

0 回答 0