如何取得程序运行时的路径(目录) dll,GetCommandLineexe,GetModuleFileName 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DWORD GetCurrentDirectory( DWORD nBufferLength, LPTSTR lpBuffer ); Kernel32.lib Winbase.h;不用包含头文件,在MFC里。 BOOL CYourClass::GetModuleFileDir(CString& strFilename){ // Get the path of the application char szFilename[MAX_PATH]; DWORD dwPathLen = 0; if((dwPathLen = ::GetModuleFileName( ::AfxGetInstanceHandle(), szFilename, MAX_PATH )) == 0) { return FALSE; } for(int i=dwPathLen-1; i>=0; i--) { if(('\\' == szFilename[i]) || ('/' == szFilename[i])) { break; } else { szFilename[i] = '\0'; } } strFilename = szFilename; if(strFilename.GetLength() <= 0) { return FALSE; } return TRUE;} 对不起,把上面的szFileName改为szFileDir,strFilename改为strFilenDir. CString strAppPath;char appPath[128];GetModuleFileName(NULL,appPath,128);strAppPath.Format("%s",appPath); LPSTR lpTemp;GetModuleFileName(hInstance, szFilePath, sizeof(szFilePath);for(lpTemp = szFilePath + lstrlen(szFilePath); *(lpTemp - 1) != '\\'; lpTemp--);lstrcpy(lpTemp, "");szFilePath 就是路径. char *lud_module_path() {#define LUD_PATH_BASE 256#define LUD_PATH_INCR 128 char *temp; char *file = NULL; int len = LUD_PATH_BASE + 1; static char *res = NULL; if (res) free((void *)res); file = (char *)malloc(len); while (file) { memset(file, 0, len); if (GetModuleFileName(NULL, file, len-1)) { temp = strrchr(file, '\\'); if (temp == NULL) break; else { res = malloc(temp-file+2); if (res == NULL) break; memcpy(res, file, temp-file+1); *(res+(temp-file+1)) = '\0'; break; } } else { len += LUD_PATH_INCR; file = (char *)realloc(file, len); } } if (file) free(file); return res;} 一般情况下,GetCurrentDirectory()就可以胜任了。因为默认情况下,操作系统都会将应用程序所在的目录作为当前目录。 需要包含头文件 windows.h int nn=GetModuleFileName(NULL,s,1024);char szPath[255],szDrive[255],szFileName[255],szExt[255];_splitpath(s,szDrive,szPath,szFileName,szExt); CADORecordset 如何删除记录 可以从资源文件里载入DLL吗? 现在老人都有很多高血压的 - 今天养生推荐 东北黑木耳 黑芝麻 无法参看CString类型值 connect的TIMEOUT 如何实现:在打开对话框的同时拌有响铃 Link出错,请看一看. RegQueryValueEx读取注册表乱码!求助!急 还得问个菜问题:ado的异常处理 如何控制针式打印机 请教:如何在我的程序中截获别的进程的某一特定消息 关注过上次C++算法极度挑战的哥们,我已经将需求简化成几个问题了!!来看看吧
不用包含头文件,在MFC里。
{
// Get the path of the application
char szFilename[MAX_PATH];
DWORD dwPathLen = 0;
if((dwPathLen = ::GetModuleFileName(
::AfxGetInstanceHandle(),
szFilename,
MAX_PATH
)) == 0) {
return FALSE;
}
for(int i=dwPathLen-1; i>=0; i--) {
if(('\\' == szFilename[i])
|| ('/' == szFilename[i])) {
break;
} else {
szFilename[i] = '\0';
}
}
strFilename = szFilename;
if(strFilename.GetLength() <= 0) {
return FALSE;
} return TRUE;
}
char appPath[128];
GetModuleFileName(NULL,appPath,128);
strAppPath.Format("%s",appPath);
GetModuleFileName(hInstance, szFilePath, sizeof(szFilePath);
for(lpTemp = szFilePath + lstrlen(szFilePath);
*(lpTemp - 1) != '\\'; lpTemp--)
;
lstrcpy(lpTemp, "");
szFilePath 就是路径.
char *lud_module_path() {
#define LUD_PATH_BASE 256
#define LUD_PATH_INCR 128
char *temp;
char *file = NULL;
int len = LUD_PATH_BASE + 1;
static char *res = NULL;
if (res) free((void *)res);
file = (char *)malloc(len);
while (file) {
memset(file, 0, len);
if (GetModuleFileName(NULL, file, len-1)) {
temp = strrchr(file, '\\');
if (temp == NULL) break;
else {
res = malloc(temp-file+2);
if (res == NULL) break;
memcpy(res, file, temp-file+1);
*(res+(temp-file+1)) = '\0';
break;
}
} else {
len += LUD_PATH_INCR;
file = (char *)realloc(file, len);
}
}
if (file) free(file);
return res;
}
因为默认情况下,操作系统都会将应用程序所在的目录作为当前目录。
char szPath[255],szDrive[255],szFileName[255],szExt[255];
_splitpath(s,szDrive,szPath,szFileName,szExt);