CFileDialog .. LPWSTR与char类型不兼容 [英] CFileDialog.. LPWSTR is incompatible with type char

查看:119
本文介绍了CFileDialog .. LPWSTR与char类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在visual 2012中运行基于2008编码的编码。所以我没有找到关于如何在visual 2012中编写它的任何示例。



错误

So I run coding based on 2008 coding in visual 2012. So I havent found any example on how to write it in visual 2012.

The error

gaschedule\gaschedule\childview.cpp(333): error C2664: 'Configuration::ParseFile' : cannot convert parameter 1 from 'wchar_t *' to 'char *'
          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast









编码在这里。





The coding is here.

void CChildView::OnFileOpenConfiguration()
{
    CFileDialog dlg( TRUE, NULL, NULL, 0,
         ("Class Schedule Config Files (*.cfg)|*.cfg|All Files (*.*)|*.*|"), this );

    if( dlg.DoModal() == IDOK )
    {
        Configuration::GetInstance().ParseFile(dlg.GetFileName().GetBuffer() );

        ComputeScrollBars();
        Invalidate();
    }
}





推荐答案

您遇到的问题可能是在Unicode版本中使用多字节字符。如果这是问题,您可以通过将项目设置为使用多字节字符来解决它。 (我相信MFC会适当地切换类型,但我不确定因为我从未使用过MFC。)



这不是推荐的解决方案,虽然。将它重写为Unicode会更好,特别是这一行:



The problem you are having is probably using Multi-Byte characters in a Unicode build. If that is the issue, you may be able to solve it by setting your project to use Multi-Byte characters instead. (I believe MFC will switch between the types appropriately, but I'm not sure because I've never used MFC.)

That isn't the recommended solution, though. It would be better to rewrite it as Unicode, specifically, this line:

CFileDialog dlg( TRUE, NULL, NULL, 0,
     (_T("Class Schedule Config Files (*.cfg)|*.cfg|All Files (*.*)|*.*|")), this );





应该修复它。建议使用Unicode,因为这是Windows在引擎盖下使用的内容,并且不需要转换。



另一种方法,如果你真的想强制它(这不是(建议)是使用WideCharToMultiByte从我放置前面的_T的字符串创建另一个字符串。你必须设置一个变量,并做更多的工作。但同样,这不是一个推荐 - 它只是出于知识目的。



快乐的编码!

David



That should fix it. Unicode is recommended because that is what Windows uses under the hood, and no conversions will be required.

Another approach, if you really want to force it in (which isn't suggested either) is to use WideCharToMultiByte to create another string from the one I placed the "_T" in front of. You would have to set up a variable, and do more work. But again, that wouldn't be a recommendation - it's just for knowledge purposes.

Happy coding!
David


除了解决方案1之外,在_T泛型类型上:此CodeProject文章提供了一个很好的解释:什么是TCHAR,WCHAR,LPSTR,LPWSTR,LPCTSTR(等)? [ ^ ]。



-SA
In addition to Solution 1, on "_T" generic types: this CodeProject article provides a good explanation: What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?[^].

—SA


这篇关于CFileDialog .. LPWSTR与char类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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