?
Solved

Executable name of currently executing program.

Posted on 2003-03-13
8
Medium Priority
?
251 Views
Last Modified: 2012-05-04
I need to retrieve the path and filename of the program currently executing.  For example, a user could rename an exe, or execute it from anywhere.  I cannot use GetCurrentDirectory because this can be different than the folder the app was executed from.  I assume there is a way to get the path and filename from an Instance Handle… or a way to get a Module handle from an Instance Handle.

Anyone know how?
0
Comment
Question by:mrmick
8 Comments
 
LVL 4

Accepted Solution

by:
Chase707 earned 200 total points
ID: 8132866
here is the Win32 function that you need:

char szFilename[255] = "";
DWORD dwRet = GetModuleFileName(NULL,szFilename, 255);

Chase707
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8132929
standard C / C++;

main(char argc, char *argv[])
{
   printf("My name is %s", argv[0]);
}
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8133086
>>/I need to retrieve the path and filename
Assumption .. UNIX PATHNAMES

from argv[0] u 'll get the first string on the command line with which it was invoked

like if u do
./myprog arg1 arg2
/home/usr/myself/myprog arg1 arg2
/home/usr/myself/../myself/myprog arg1 arg2

in above cases argv[0] will give u
./myprog
/home/usr/myself/myprog
/home/usr/myself/../myself/myprog
respetively

so what i would suggest is .. take out the part of the  argv[0] after the last '/'  
and get the current working directory .. and combine the two.. like this

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
char *name;
char *cwd;
char *fullname;
name=strrchr(argv[0],'/');

if(name!=NULL)name++;
else name=argv[0];
cwd=(char*)get_current_dir_name();

fullname=(char*)malloc(strlen(cwd)+strlen(name)+2);
strcpy(fullname,cwd);
strcat(fullname,"/");
strcat(fullname,name);

printf("fullpath= %s\n",fullname);

}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:akshayxx
ID: 8133092
well i didnt notice u needed windows solution.. sorry to bother u
0
 

Expert Comment

by:elpredicador
ID: 8133311
use getcwd(char* buffer, int sizeofbuffer)
it will store in buffer up to sizeofbuffer chars of the current dir
0
 

Expert Comment

by:elpredicador
ID: 8133322
the filename you get with
main(int argc, char* argv[])

argv[0] is the name of your file
0
 

Expert Comment

by:elpredicador
ID: 8133337
dont forget to add the "\\" between the dir and the filename.
sorry for so much postings..
0
 
LVL 8

Author Comment

by:mrmick
ID: 8136264
Thanks to all for the suggestions.  My bad, I should have mentioned that I was writing against the Windows OS.

Mick
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question