gcl-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gcl-devel] RE: Getting full pathname of running executable


From: Mike Thomas
Subject: [Gcl-devel] RE: Getting full pathname of running executable
Date: Wed, 10 Sep 2003 11:51:56 +1000

Hi Camm.

Windows:

================================================================
#include <windows.h>
#include <stdio.h>

#define BUF_SIZE 80

int main ( int argc, char ** argv )
{
  char ourpath[BUF_SIZE];
  GetModuleFileName ( NULL, &ourpath[0], BUF_SIZE );
  fprintf ( stdout, "The executable is in %s\n", &ourpath[0]  );

  return 0;
}

================================================================

address@hidden ~
$ gcc filename.c -o filename.exe

address@hidden ~
$ ./filename
The executable is in C:\msys\1.0\home\miketh\filename.exe

address@hidden ~
$ cd /c

address@hidden /c
$ ~/filename
The executable is in C:\msys\1.0\home\miketh\filename.exe

================================================================

DWORD GetModuleFileName(
  HMODULE hModule,
  LPTSTR lpFilename,
  DWORD nSize
);

Parameters
hModule
[in] Handle to the module whose path is being requested. If this parameter
is NULL, GetModuleFileName retrieves the path for the current module.
lpFilename
[out] Pointer to a buffer that receives a null-terminated string that
specifies the fully-qualified path of the module. If the length of the path
exceeds the size specified by the nSize parameter, the function succeeds and
the string is truncated to nSize characters and null terminated.
The path can have the prefix "\\?\", depending on how the module was loaded.
For more information, see Naming a File.

nSize
[in] Size of the lpFilename buffer, in TCHARs.
Return Values
If the function succeeds, the return value is the length of the string
copied to the buffer, in TCHARs. If the buffer is too small to hold the
module name, the string is truncated to nSize, and the function returns
nSize.

If the function fails, the return value is zero. To get extended error
information, call GetLastError.
================================================================


| -----Original Message-----
| From: Camm Maguire [mailto:address@hidden
| Sent: Wednesday, September 10, 2003 11:19 AM
| To: address@hidden
| Cc: Mike Thomas; Aurelien Chanudet
| Subject: Getting full pathname of running executable
|
|
| This nonportable proc dependant code is the best I can think of at the
| moment.  Alternatives, esp. on Windows/Mac?
|
|   int p;
|   char b[PATH_MAX],q[PATH_MAX];
|
|   p=getpid();
|   snprintf(q,sizeof(q),"/proc/%d/exe",p);
|   realpath(q,b);
|
| Take care,
|
| --
| Camm Maguire                                          address@hidden
| ==========================================================================
| "The earth is but one country, and mankind its citizens."  --  Baha'u'llah
|
|






reply via email to

[Prev in Thread] Current Thread [Next in Thread]