标签:
一直以来,以基础中间件开发和产品设计为主,C语言为根本,但随着用户业务的技术特点
等因素,造成此前产品中仅仅能支持256个字节(也就是128个汉字)以内的问题,逐渐显现,并成为用户或集成商所累赘或诟病,如何搞定?
调查了一下,也搜罗了网络上的说法,不外乎几种
如果真是如此,那使用原始C函数的产品或软件,不就惨了吗?不能与时俱进了
那怎么能行!调查后,发现GetShortPathName函数,可以将长文件名进行8.3格式转换,为_open等函数所使用,直白一点,也就是将文件名缩短了,_open等函数就认了。真是的,本是同根(Windows)生,相煎何太急!
经过验证,提供如下函数,可将长文件名缩短,以支持256字节以上的长文件名。有几个要点,要记牢
#define WMY_FULL_FILENAME_LEN 512
#if defined(WIN32) || defined(WIN64) || defined(_WIN32_WCE) void shortedFileName(char *inFileName, char *outFileName) { int sRet = 0; WCHAR wLongFilePath[WMY_FULL_FILENAME_LEN+1] = {0}; WCHAR wShortFilePath[WMY_FULL_FILENAME_LEN+1] = {0}; char newFilePath[WMY_FULL_FILENAME_LEN+1] = {0}; char *p = strrchr(inFileName, T_PATHCONNCHR); if(p){ MultiByteToWideChar (CP_ACP, 0, inFileName, p-inFileName, wLongFilePath, WMY_FULL_FILENAME_LEN); } else{ MultiByteToWideChar (CP_ACP, 0, inFileName, -1, wLongFilePath, WMY_FULL_FILENAME_LEN); } sRet = GetShortPathNameW(wLongFilePath, wShortFilePath, WMY_FULL_FILENAME_LEN); if(0!=sRet) { WideCharToMultiByte(CP_OEMCP,NULL,wShortFilePath,-1,newFilePath,WMY_FULL_FILENAME_LEN,NULL,FALSE); } else{ WideCharToMultiByte(CP_OEMCP,NULL,wLongFilePath,-1,newFilePath,WMY_FULL_FILENAME_LEN,NULL,FALSE); } if(p) sprintf(outFileName, "%s%c%s", newFilePath, T_PATHCONNCHR, p+1); else sprintf(outFileName, "%s", newFilePath); } #endif
附录参考:
The GetShortPathName function retrieves the short path form of a specified input path.
DWORD GetShortPathName( LPCTSTR lpszLongPath, // null-terminated path string LPTSTR lpszShortPath, // short form buffer DWORD cchBuffer // size of short form buffer );
Parameters
Windows NT/2000/XP: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see File Name Conventions.
Windows 95/98/Me: This string must not exceed MAX_PATH characters.
If the function succeeds, the return value is the length, in TCHARs, of the string copied tolpszShortPath, not including the terminating null character.
If the lpszShortPath buffer is too small to contain the path, the return value is the size of the buffer, inTCHARs, required to hold the path. Therefore, if the return value is greater thancchBuffer, call the function again with a buffer that is large enough to hold the path.
If the function fails for any other reason, the return value is zero. To get extended error information, callGetLastError.
When an application calls this function and specifies a path on a volume that does not support 8.3 aliases, the function fails with ERROR_INVALID_PARAMETER if the path is longer than 67 bytes.
The path specified by lpszLongPath does not have to be a full or a long path. The short form may be longer than the specifed path.
If the specified path is already in its short form, there is no need for any conversion, and the function simply copies the specified path to the buffer for the short path.
You can set lpszShortPath to the same value as lpszLongPath; in other words, you can set the buffer for the short path to the address of the input path string.
You can obtain the long name of a file from the short name by calling the GetLongPathName function. Alternatively, where GetLongPathName is not available, you can callFindFirstFile on each component of the path to get the corresponding long name.
Windows 95/98/Me: GetShortPathNameW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined inMicrosoft Layer for Unicode on Windows 95/98/Me Systems.
Windows NT/2000/XP: Included in Windows NT 3.5 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
File I/O Overview, File I/O Functions,FindFirstFile, GetFullPathName, GetLongPathName,SetFileShortName
老树(老产品)开新花,就看如何对待了。
欢迎感兴趣的,提供相关的产品解决之道,提高产品的应用范围和效益,延长生命周期,降低成本。
标签:
原文地址:http://blog.csdn.net/pony12/article/details/42192051