Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Who am I? 3

Status
Not open for further replies.

Bork

Programmer
Apr 18, 2001
7
CA
How do I tell from inside a program what name the .exe has?

For example, if the running program is called myprog.exe, I want to have code in the program that will get a string containing "myprog.exe".
 
when defining main() use this notation
int main(int argc, char *argv[])
{
..
..
.
.
}
Any time in ur program whenever u'll say argv[0] it will return u a string containing name of the program. And if u r diehard to get .exe extension along with that. use strcat() to concatenate prog name with .exe like this
strcat(name,argv[0]);
strcat(name,".exe");
in name now u'll have "program.exe".
C C is that simple.
Regards,
SwapSawe.
 

If you are running a Windows Program which doesnt
use the console, then this approach doesnt work.

For a windows program this is the approach.

// your program
WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, ... )
{
LPCTSTR lpModName;
DWORD dwSz = 256; // size of the path of file

if ( GetModuleFileName( hThisInst, lpModName, dwSz ))
{
cout << lpModName << endl;
}
}

abp :cool:


 
The two posts offered are correct. Unfortunately, I didn't specify that I'm using MFC with MDI. Can anyone tell me how to identify my .exe name from inside the program in this situation?
 
The following sequence can be put in every place from your program. It will retrieve the full path and name of your application:

char szName[MAX_PATH];
GetModuleFileName(AfxGetInstanceHandle(), szName, MAX_PATH);

Also if you need to read the command line parameters you can do this in the InitInstance method of you CWinApp derived class by reading the from a CCommmandLine class
(the wizard automatically puts this code there, check it out)

Hope this helps,


s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
this one also wofk
char szName[MAX_PATH];
GetModuleFileName(GetModuleHandle(0), szName, MAX_PATH);
John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top