Sometimes there is value in determining an application name or folder from within the application. The application can use this to display the application name in the title bar, or even change behavior based on the name.
GetModuleFileName() fills in a string with the path and filename of the application. The following code fragment uses the hInstance passed into WinMain() to get the application path and filename.
 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR    lpCmdLine, int       nCmdShow)
{
TCHAR Path[MAX_PATH];
GetModuleFileName( hInstance, Path, MAX_PATH );
Now the application can search the Path for the application name using wcstok() with something like:
                TCHAR *LastPtr;
                TCHAR *CurrentPtr;
               
                GetModuleFileName( hInstance, Path, MAX_PATH );
 
                LastPtr = Path;
                CurrentPtr = wcstok( LastPtr, TEXT("\\"));
                while( CurrentPtr != NULL )
                {
                                LastPtr = CurrentPtr;
                                CurrentPtr = wcstok( NULL, TEXT("\\"));
                }
                if( wcscmp( LastPtr, TEXT("MyApp.exe")))
                {
                                DoMyApp();
                }
                else
                {
                                DoOtherApp();
                }
I have used this type of logic to control test applications. If the name is something like AutoTest.exe then the test automatically runs the tests, otherwise it presents a user interface to allow the user to select tests to run.
Copyright © 2009 – Bruce Eitman
All Rights Reserved