我怎么能转换的Uni code路径C字符串? [英] How can I convert a Unicode path to a c string?

查看:92
本文介绍了我怎么能转换的Uni code路径C字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个统一code路径名(LPWSTR)转换成等价的ASCII码?被调用的库只能理解C字符串。

编辑:
好吧,我把GetShortPathName和调用WideCharToMultiByte建议,并创建了一张code,我含在路径的Uni code字符一些文件夹测试,它完美地工作:

  wlength = GetShortPathNameW(CPY,0,0);
LPWSTR SHORTP =(LPWSTR)释放calloc(wlength,sizeof的(WCHAR));
GetShortPathNameW(CPY,SHORTP,wlength);
clength =调用WideCharToMultiByte(CP_OEMCP,WC_NO_BEST_FIT_CHARS,SHORTP,wlength,0,0,0,0);
LPSTR CPATH =(LPSTR)释放calloc(clength,sizeof的(CHAR));
调用WideCharToMultiByte(CP_OEMCP,WC_NO_BEST_FIT_CHARS,SHORTP,wlength,CPATH,clength,0,0);


解决方案

GetShortPathName()功能

http://msdn.microsoft.com/ EN-US /库/ aa364989%28VS.85%29.aspx

会给你一个等效8.3文件名,指向同一个文件中,与传统code使用。

这可能是你能做的最好的,虽然理论上的8.3文件名可能包含非ASCII字符,这取决于注册表设置。在这种情况下,你没有得到适当的char *一个简单的方法,和GetShortPathNameA()将不会做,要么如果文件创建时codePAGE设置不匹配当前设置。

请参阅 http://technet.microsoft.com /en-us/library/cc781607%28WS.10%29.aspx 有关的设置。有一个concensus这里(见下文),这起案件是合理的忽视。

由于伦,一切为这个职位的贡献。

How can i convert from a Unicode path name (LPWSTR) to the ASCII equivalent? The library that gets called understands only c strings.

Edit: Okay, I took the GetShortPathName and the WideCharToMultiByte suggestions and created that piece of code, i tested it with some folders containing Unicode characters in the path and it worked flawlessly:

wlength = GetShortPathNameW(cpy,0,0);
LPWSTR shortp = (LPWSTR)calloc(wlength,sizeof(WCHAR));
GetShortPathNameW(cpy,shortp,wlength);
clength = WideCharToMultiByte(CP_OEMCP, WC_NO_BEST_FIT_CHARS, shortp, wlength, 0, 0, 0, 0);
LPSTR cpath = (LPSTR)calloc(clength,sizeof(CHAR));
WideCharToMultiByte(CP_OEMCP, WC_NO_BEST_FIT_CHARS, shortp, wlength, cpath, clength, 0, 0);

解决方案

GetShortPathName() Function

http://msdn.microsoft.com/en-us/library/aa364989%28VS.85%29.aspx

Will give you an equivalent 8.3 filename, pointing to the same file, for use with legacy code.

[EDIT] This is probably the best you can do, although theoretically the 8.3 filenames may contain non-ascii characters, depending on registry setting. In this case, you don't have an easy way of getting the proper char*, and GetShortPathNameA() will not do that either if codepage setting during file creation does not match current setting.

See http://technet.microsoft.com/en-us/library/cc781607%28WS.10%29.aspx about the setting. There's a concensus here (see below) that this case is reasonable to neglect.

Thanks Moron, All, for contribution to this post.

这篇关于我怎么能转换的Uni code路径C字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆