Y_Y
Y_Y

Reputation: 1295

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 installation 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.

Upvotes: 1

Views: 485

Answers (2)

bcoughlan
bcoughlan

Reputation: 26627

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?

Upvotes: 0

dmazzoni
dmazzoni

Reputation: 13256

Use GetModuleFileName and pass NULL for hModule.

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

Upvotes: 1

Related Questions