How to get the application executable name in WindowsC++/CLI?

白昼怎懂夜的黑 提交于 2019-11-26 17:52:36

Call GetModuleFileName() using 0 as a module handle.

Note: you can also use the argv[0] parameter to main or call GetCommandLine() if there is no main. However, keep in mind that these methods will not necessarily give you the complete path to the executable file. They will give back the same string of characters that was used to start the program. Calling GetModuleFileName() will always give you a complete path & file name.

Adam Pierce

Ferruccio's answer is good. Here's some example code:

TCHAR exepath[MAX_PATH+1];

if(0 == GetModuleFileName(0, exepath, MAX_PATH+1))
    MessageBox(_T("Error!"));

MessageBox(exepath, _T("My executable name"));

Use the argv argument to main:

int main(int argc, char* argv[])
{
    printf("%s\n", argv[0]);  //argv[0] will contain the name of the app.
    return 0;
}

You may need to scan the string to remove directory information and/or extensions, but the name will be there.

Use __argv[0]

There is a static Method on Assembly that will get it for you in .NET.

Assembly.GetEntryAssembly().FullName

Edit: I didn't realize that you wanted the file name... you can also get that by calling:

Assembly.GetEntryAssembly().CodeBase

That will get you the full path to the assembly (including the file name).

I can confirm it works under win64/visual studio 2017/ MFC

TCHAR szFileName[MAX_PATH + 1];
GetModuleFileName(NULL, szFileName, MAX_PATH + 1);
auto exe = CString(szFileName);

exe contains full path to exe.

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