如何在取得程序运行时所在的路径

 在网上看到了一篇文章,大意是这样的:

TCHAR exeFullPath[_MAX_PATH];

char szDrive[_MAX_DRIVE]={0}, szDir[_MAX_DIR]={0}, szFNAME[_MAX_FNAME]={0}, szExt[_MAX_EXT]={0};

GetModuleFileName(AfxGetInstanceHandle(), exeFullPath, sizeof(exeFullPath));

_splitpath (exeFullPath, szDrive, szDir, NULL, NULL);

m_strWorkDIR.Format("%s%s", szDrive, szDir);

GetModuleFileName可以取得程序运行时的路径和文件名,具体的帮助请查看MSDN.但是在WinCE(PocketPC,SmartPhone)上没有找到_makepath,_splitpath ,着两个函数在Windows SDK的stdlib.h中.没办法,自己写了一个函数,取得程序的运行是的路径,代码如下,仅功参考:

/*
Parameters
    hModule :
        [in] Handle to the module whose executable file name is being requested.
        If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.

    lpFilename :
        [out] Pointer to a buffer that is filled in with the path and file name of the module.
    nSize :
        [in] Specifies the length, in characters, of the lpFilename buffer.
        If the length of the path and file name exceeds this limit, the string is truncated.

Return Values
    The length, in characters, of the string copied to the buffer indicates success.

    Zero indicates failure.

    To get extended error information, call GetLastError.
*/
DWORD GetAppPath(
            HMODULE hModule,
            LPWSTR lpFilename,
            DWORD nSize
            )
{
    DWORD result;
    result = GetModuleFileName( NULL,lpFilename,nSize);
    if (0==result){
        return result;
    }

    LPWSTR pc;
    pc = lpFilename + wcslen(lpFilename);
    while('//'!=*pc){
        *pc=0;
        pc--;
    }
}

转载请注明出处(http://blog.csdn.net/enjoyo)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章