开发者

How to set the application path to the running program?

I have a program that is executed by another program. The program that is being executed needs files located at its own location [same folder]. If I call myfile.open("xpo.dll") I might get an error because I am not passing the [fullpath + name + extension]. The program that is being executed can vary paths depending on the installa开发者_如何学Ction path. Therefore, I was wondering if there is a way to get the application path [where the application is located] and set it so that when another program executes from another path everything might work properly...?

[Using C++ without .NET Framework]

Thanks.


Use GetModuleFileName and pass NULL for hModule.

DWORD GetModuleFileName(
    HMODULE hModule,   // handle to module
    LPTSTR lpFilename, // path buffer
    DWORD nSize        // size of buffer
);


First off, I run into this problem in other languages a lot, and find Process Monitor (http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) very useful for finding out what folder it is currently trying to access.

There is no standard function for doing this.

  1. Just a thought, have you tried doing myfile.open "./xpo.dll"?

  2. If it's a console application, you can use the POSIX getcwd function: http://www.dreamincode.net/code/snippet77.htm

  3. If it's a Windows app and you can use the windows API, you can use GetModuleFileName... see the second reply to this question here: How do I get the directory that a program is running from?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜