如何在取得程序運行時所在的路徑

 在網上看到了一篇文章,大意是這樣的:

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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章