Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I need to change the functionality of an application based on the executable name. Nothing huge, just changing strings that are displayed and some internal identifiers. The application is written in a mixture of native and .Net C++-CLI code.

Two ways that I have looked at are to parse the GetCommandLine() function in Win32 and stuffing around with the AppDomain and other things in .Net. However using GetCommandLine won't always work as when run from the debugger the command line is empty. And the .Net AppDomain stuff seems to require a lot of stuffing around.

So what is the nicest/simplest/most efficient way of determining the executable name in C++/CLI? (I'm kind of hoping that I've just missed something simple that is available in .Net.)

Edit: One thing that I should mention is that this is a Windows GUI application using C++/CLI, therefore there's no access to the traditional C style main function, it uses the Windows WinMain() function.

like image 209
Daemin Avatar asked Sep 24 '08 01:09

Daemin


People also ask

How do I find a program using command prompt?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.

How can you know the path of an executable file command?

PATH without parameters will display the current path. The %PATH% environment variable contains a list of folders. When a command is issued at the CMD prompt, the operating system will first look for an executable file in the current folder, if not found it will scan %PATH% to find it.

Where is executable stored?

The executable file for Excel – Excel.exe – is located in the installation directory for the 64-bit version of Microsoft Office 365 at C:\Program Files\Microsoft Office\root\Office16.

What is executable path?

The full pathname of the receiver's executable file.


2 Answers

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(), instead, will always give you a complete path and file name.

like image 67
Ferruccio Avatar answered Oct 27 '22 01:10

Ferruccio


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"));
like image 38
Adam Pierce Avatar answered Oct 27 '22 00:10

Adam Pierce