问题
Possible Duplicates:
Win32: Find what directory the running process EXE is stored in
How to get the application executable name in Windows (C++ Win32 or C++/CLI)?
hi, i want to make my application run on statup,it uses some of files in the same directory.it works good but when it start at startup the GetCurrentDirectory is "c:\Documents and Settings\User\"..but i want the actual path of the exe file.how i can get it in c++. please help me. thanks.
回答1:
Try using GetModuleFileName or GetModuleFileNameEx.
Do this:
wchar_t exeDirectory[1024]; //to store the directory
DWORD ret = GetModuleFileName(NULL, exeDirectory, 1024);
if ( ret )
{
/*the path to your EXE is stored in the variable "exeDirectory" - use it */
}
Note I'm passing NULL as first argument, because MSDN says,
"If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process."
which is what you want. Right?
回答2:
Using argv maybe:
int main(int argc, char* argv[])
{
// argv[0] is the path to binary file you're running
// ...
return 0;
}
The profit is that this method is platform-independent and has no needs for any system calls.
来源:https://stackoverflow.com/questions/4699301/getcurrentdirectory-for-startup-app-c