?
Solved

Executable name of currently executing program.

Posted on 2003-03-13
8
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 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