#include "string.h"
#include "stdio.h"
#ifndef __WIN32__
#include "unistd.h"
#include "sys/types.h"
#endif
char getApplicationPath(char *pPath, int nMaxPathSize)
{
char proc_path[20], buf[256], *lastchar;
int len;
#ifdef __WIN32__
len = GetModuleFileName(NULL, buf, 256);
if(len == 0) {
// GetModuleFileName error
return -1;
}
if((len >= 256) || (len >= nMaxPathSize)) {
//insufficient size
return -2;
}
// terminate the returned string first
buf[len] = 0;
// then remove the application name
lastchar = strrchr(buf, '\\');
#else
sprintf(proc_path, "/proc/%d/exe", getpid());
len = readlink(proc_path, buf, 256);
if(ret < 0) {
// readlink error
perror("readlink");
return -1;
}
if((len >= 256) || (len >= nMaxPathSize)) {
// insufficient size
return -2;
}
// terminate the returned string first
buf[len] = 0;
// then remove the application name
lastchar = strrchr(buf, '/');
#endif
if(lastchar != NULL) {
*lastchar = 0;
} else {
// application name not found
return -3;
}
strcpy(pPath, buf);
// success
return 0;
}