C++ 如何将 char* 转换为 LPCWSTR?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19715144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:04:35  来源:igfitidea点击:

How to convert char* to LPCWSTR?

c++visual-c++visual-studio-2012

提问by sigil

I know this has already been discussed in several questions on SO, but none of those solutions have worked for me.

我知道这已经在关于 SO 的几个问题中讨论过,但是这些解决方案都没有对我有用。

I start with a char*because this is for a DLL that will be called from VBA, and char*is necessary for VBA to pass a string to the DLL.

我从 a 开始,char*因为这是一个将从 VBA 调用的 DLL,并且char*是 VBA 将字符串传递给 DLL 所必需的。

I need to return a LPCWSTRbecause that's the input parameter for the API function I'm trying to call, and I can't enable casting by switching from Unicode to multi-byte character set in the Properties window, because the API has this code:

我需要返回 aLPCWSTR因为这是我尝试调用的 API 函数的输入参数,并且我无法通过在属性窗口中从 Unicode 切换到多字节字符集来启用转换,因为 API 具有以下代码:

#if !defined(UNICODE) && !defined(NOUNICODE)
#error UNICODE is not defined. UNICODE must be defined for correct API arguments.
#endif

I tried this:

我试过这个:

LPCWSTR convertCharArrayToLPCWSTR(char* charArray)
    {
        const char* cs=charArray;
        wchar_t filename[4096] = {0};
        MultiByteToWideChar(0, 0, cs[1], strlen(cs[1]), filename, strlen(cs[1]));
    }

which gave these errors:

这给出了这些错误:

error C2664: 'strlen' : cannot convert parameter 1 from 'const char' to 'const char *'
error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'const char' to 'LPCCH'

I tried this (same function header), loosely adapted from this post:

我试过这个(相同的函数头),松散地改编自这篇文章

size_t retVal;
const char * cs = charArray;    
size_t length=strlen(cs);
wchar_t * buf = new wchar_t[length]();  // value-initialize to 0 (see below)
size_t wn = mbsrtowcs_s(&retVal,buf,20, &cs, length + 1, NULL);
return buf;

This compiled ok, but when I passed it an example string of "xyz.xlsx", mbsrtowcs_s()set bufto an empty string: L""

这编译没问题,但是当我传递给它一个示例字符串“xyz.xlsx”时,mbsrtowcs_s()设置buf为一个空字符串:L""

So, how do I make this conversion?

那么,我该如何进行这种转换?

采纳答案by cHao

Since csis a const char*, cs[1]is a const char. C++ won't convert it to a pointer for you, because in most cases that doesn't make sense.

由于csconst char*cs[1]const char。C++ 不会为您将其转换为指针,因为在大多数情况下这没有意义。

You could instead say &cs[1]or cs+1if the intent is to skip the first char. (That's what you're doing when you pass a pointer to the 1th element; in C++, indexes start at 0.) If the intent is to pass the whole string, then just pass cs.

你可以说&cs[1]或者cs+1如果意图是跳过第一个字符。(这就是传递指向第 1 个元素的指针时所做的;在 C++ 中,索引从 0 开始。)如果意图传递整个字符串,则只需传递cs.

回答by sigil

Following Hans Passant's advice regarding pointers to local variables, I worked out this approach, which seems to work well:

遵循 Hans Passant 关于局部变量指针的建议,我制定了这种方法,它似乎工作得很好:

wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
{
    wchar_t* wString=new wchar_t[4096];
    MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
    return wString;
}

I'm aware that the use of newrequires memory management, which I perform in the function that calls this one.

我知道使用new需要内存管理,我在调用这个的函数中执行。