GetCurrentDirectory for startup App. c++ [duplicate]

心不动则不痛 提交于 2020-01-16 19:17:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!