1

My executable file is in this place --> D:\Examples\C_Pro\newApp.exe Also in that "C_Pro" folder contain several files ( file1.txt, file2.txt, file44.exe, newApp.c )

In my newApp.c file, I includes a ShellExecute Function to execute "file44.exe" file in same folder like this --> ShellExecute(NULL,"open","D:\Examples\C_Pro\file44.exe",NULL,NULL,1)

in this way all work properly..

I'm talking about AppPath like thing in VB

But the case is I want to run this newApp.exe on different pc's So I want to replace ""D:\Examples\C_Pro\" this by whatever the path that contain "newApp.exe" file in another pc. (like C:\Software\ )

I get the path using GetModuleFileName Function but it contain newApp.exe part I want only at to the Point that new directory PathRemoveFileSpec function doesn't work.

and also the return path of GetModuleFileName like --> D:\Examples\C_Pro\newApp.exe but when we put some path in to ShellEcxecute the require double shalse (space sequence) like this --> D:\Examples\C_Pro\

How can I get rid of this problem.

Actual code snippt is this...

int main()
{
    ShellExecute(NULL,"open","D:\\Softwares\\TypingMaster700.exe",NULL,NULL,SW_SHOWNORMAL);
}

But I want to do like this. (this is dummy one, here "some_Funtion" means dummy function for explanation purpose.

int main()
{
    char *dirPath = some_Function(a,x,d);
    char *fullPath;
    fullPath = strcat(dirPath,"\\TypingMaster700.exe");
    ShellExecute(NULL,"open",fullPath,NULL,NULL,SW_SHOWNORMAL);
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Aspen
  • 53
  • 1
  • 9

2 Answers2

2

Getting the absolute path to the directory where the executable is located is not standardly supported in the C standard because not all systems where the program runs support such a concept. Nevertheless in practice it is a desirable function to have. In short: good question.

Unfortunately it is not so simple and if your program is invoked using execl c.s. it may even be impossible. You will have to replay the shell in determining which application to run and start with argv[0] as paulsm4 also does. On Linux if the path starts with /, then argv[0] is the absolute path to the executable and you can find the directory by stripping of the executable name at the end. On Windows you will have to check for \ and possibly a drive letter, I'm not sure. We will assume Linux in the remainder, just read \ for every / to apply it to Windows.

If argv[0] is not an absolute path as above, you should check if the it contains any / at all, because if it does it must be relative to getcwd as was described also by paulsm4.

If argv[0] does not contain any /, then you will have to run through the PATH environment variable to find the first directory containing argv[0].

If that all fails, your application has been invoked through execl or one of its friends and they were not honest about the location of the executable. You are out of luck.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18
  • Thank you Bryan for ur detailed explanation. I'm doin on Windows using MinGW32 gcc compiler.. I will be in big trouble. is I can't do this My App is not Completed. because it have to run defferent pcs – Aspen Apr 02 '13 at 23:57
1

Something like this works on Windows:

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  char buff[255];
  getcwd (buff, sizeof (buff));
  printf ("path=%s\\%s\n", buff, argv[0]);
  return 0;
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thank you for ur reply, But this is not what I want. Can't put paths like this type --> D:\Examples\C_Pro\ in to ShellExecute funtion. it requires path in this manner --> "D:\\Examples\\C_Pro\\" – Aspen Apr 02 '13 at 23:39
  • No. You're confusing a compile-time string literal (a constant in your code, like "c:\\moose\\turd") with a runtime string. It's the *compiler* that sees the "\" and tries to interpret it as a metacharacter. The string you get from "getcwd()" or "GetModuleFileName()" at runtime can be used as-is with other functions. – paulsm4 Apr 02 '13 at 23:48
  • Thanks paul, I'll try and let you know – Aspen Apr 02 '13 at 23:59
  • 1
    Bryan Olivier also makes a good point: some platforms (like DOS/Windows) return just the program name in argv[0]; other platforms (like Linux) return the entire path. This may or may not be important for you to keep in mind. – paulsm4 Apr 03 '13 at 00:00
  • I do like thi --> ShellExecute(NULL,"explore", getcwd (buff, sizeof (buff)),NULL,NULL,SW_SHOWNORMAL); but it gives error ??/ – Aspen Apr 03 '13 at 00:12
  • PAul ... iys works... thank yyou soo much.. but do me another little favor.. I want to append another file name to getcwd() out put. I do like this --> ShellExecute(NULL,"explore", strcpy(getcwd (buff, sizeof (buff)),"\\file44.exe"),NULL,NULL,SW_SHOWNORMAL); but bot working – Aspen Apr 03 '13 at 00:31
  • Hey paul... I did it.. Thanks a lot for ur help. because of u it is hepl ti me coz I get to now about getcwd() from you.. thanks a lot – Aspen Apr 03 '13 at 00:43
  • 1
    Keep in mind that `getcwd()` returns the current directory. In general that is not the same directory that the executable image is in. – Michael Burr Apr 03 '13 at 06:46