why can not I get a long filename through GetModuleFileName( )?
-
I only get a 8.3 format filename through following code, and I want to get a long filename, how?
HINSTANCE hInst = AfxGetInstanceHandle();
if(FAILED(!hInst)) return E_FAIL;
CString strDllFileName;
DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
TRACE(strDllFileName);by debugging, I find that the value of
strDllFileName
isF:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL
, and the corresponding longname isF:\hu\DD\DetailDesign\Debug\DetailDesign.dll
Note: I am programming inVC++ 6.0
, my Operating System isWindows XP
, theDisk F:
,where the .dll file is located, isNTFS
format. Thank you very much!!! ------------------- I am learning C++ and English -
I only get a 8.3 format filename through following code, and I want to get a long filename, how?
HINSTANCE hInst = AfxGetInstanceHandle();
if(FAILED(!hInst)) return E_FAIL;
CString strDllFileName;
DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
TRACE(strDllFileName);by debugging, I find that the value of
strDllFileName
isF:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL
, and the corresponding longname isF:\hu\DD\DetailDesign\Debug\DetailDesign.dll
Note: I am programming inVC++ 6.0
, my Operating System isWindows XP
, theDisk F:
,where the .dll file is located, isNTFS
format. Thank you very much!!! ------------------- I am learning C++ and Englishewighell wrote:
...I want to get a long filename, how?
Have you considered
GetLongPathName()
?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
I only get a 8.3 format filename through following code, and I want to get a long filename, how?
HINSTANCE hInst = AfxGetInstanceHandle();
if(FAILED(!hInst)) return E_FAIL;
CString strDllFileName;
DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
TRACE(strDllFileName);by debugging, I find that the value of
strDllFileName
isF:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL
, and the corresponding longname isF:\hu\DD\DetailDesign\Debug\DetailDesign.dll
Note: I am programming inVC++ 6.0
, my Operating System isWindows XP
, theDisk F:
,where the .dll file is located, isNTFS
format. Thank you very much!!! ------------------- I am learning C++ and EnglishHello you can try this function which transform the path you get from GetModuleFileName...
typedef DWORD (__stdcall *GETLONGPATHNAME)(LPCTSTR, LPTSTR, DWORD); ///////////////////////////////////////////////////////////////////////////// // DWORD MyGetLongPathNameEx (CString& sPath) { // Result DWORD dwRet = 0; // Load the KERNEL32 library HINSTANCE hdl = ::LoadLibrary ("KERNEL32.DLL"); if (hdl == NULL) return dwRet; // Get the function we will use (Not Unicode version) GETLONGPATHNAME pfnGetLongPathName = (GETLONGPATHNAME) GetProcAddress (hdl, "GetLongPathNameA"); if (pfnGetLongPathName == NULL) { ::FreeLibrary (hdl); return dwRet; } // Call the GetLongPathName function to converts the specified path to its long form. dwRet = (pfnGetLongPathName) (sPath, sPath.GetBuffer (_MAX_PATH), _MAX_PATH); sPath.ReleaseBuffer (); // Unload library ::FreeLibrary (hdl); return dwRet; } CString strDllFileName; DWORD Status = ::GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName.GetBuffer (_MAX_PATH), _MAX_PATH); strDllFileName.ReleaseBuffer (); DWORD dwRet = ::MyGetLongPathNameEx (strDllFileName); TRACE(strDllFileName);
-
I only get a 8.3 format filename through following code, and I want to get a long filename, how?
HINSTANCE hInst = AfxGetInstanceHandle();
if(FAILED(!hInst)) return E_FAIL;
CString strDllFileName;
DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
TRACE(strDllFileName);by debugging, I find that the value of
strDllFileName
isF:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL
, and the corresponding longname isF:\hu\DD\DetailDesign\Debug\DetailDesign.dll
Note: I am programming inVC++ 6.0
, my Operating System isWindows XP
, theDisk F:
,where the .dll file is located, isNTFS
format. Thank you very much!!! ------------------- I am learning C++ and Englishuse GetLongPathName after u have got the short file name. I would like to comment that the statement DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH); is wrong. u should never cast CString like (LPTSTR)(LPCTSTR)strDllFileName. U may have unexpected results because of this. Use as follows DWORD Status = GetModuleFileName (hInst, strDllFileName.GetBuffer( MAX_PATH ), MAX_PATH); strDllFileName.ReleaseBuffer( -1 ); nave
-
I only get a 8.3 format filename through following code, and I want to get a long filename, how?
HINSTANCE hInst = AfxGetInstanceHandle();
if(FAILED(!hInst)) return E_FAIL;
CString strDllFileName;
DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
TRACE(strDllFileName);by debugging, I find that the value of
strDllFileName
isF:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL
, and the corresponding longname isF:\hu\DD\DetailDesign\Debug\DetailDesign.dll
Note: I am programming inVC++ 6.0
, my Operating System isWindows XP
, theDisk F:
,where the .dll file is located, isNTFS
format. Thank you very much!!! ------------------- I am learning C++ and EnglishLet me try to actually answer the subject of ou post... That function dates back to 16-bit Windows, and my guess is that since only FAT-style 8.3 filenames were supported, Win32 inherited that behavior. Also, note that the
GetModuleFileName(...)
function is one of the few that is capable of returning filepaths longer thanMAX_PATH
, so do not assume thatMAX_PATH
is always going to be long enough... Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)