问题描述
我知道这已经在关于 SO 的几个问题中讨论过,但这些解决方案都没有对我有用.
我以 char*
开头,因为这是用于从 VBA 调用的 DLL,并且 char*
是 VBA 将字符串传递给 DLL 所必需的.
我需要返回一个 LPCWSTR
因为这是我试图调用的 API 函数的输入参数,我无法通过从 Unicode 切换到多字节字符集来启用转换属性窗口,因为 API 有这样的代码:
我试过了:
给出了这些错误:
我试过这个(相同的函数头),大致改编自这篇文章:
编译没问题,但是当我向它传递一个示例字符串xyz.xlsx"时,mbsrtowcs_s()
将 buf
设置为空字符串:L""
那么,我该如何进行这种转换?
由于 cs
是一个 const char*
,cs[1]
是一个 const char
.C++ 不会为您将其转换为指针,因为在大多数情况下这是没有意义的.
如果要跳过第一个字符,您可以改为使用 &cs[1]
或 cs+1
.(这就是传递指向第 1 个元素的指针时所做的;在 C++ 中,索引从 0 开始.)如果意图传递整个字符串,则只需传递 cs
.>
I know this has already been discussed in several questions on SO, but none of those solutions have worked for me.
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.
I need to return a LPCWSTR
because 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:
I tried this:
which gave these errors:
I tried this (same function header), loosely adapted from this post:
This compiled ok, but when I passed it an example string of "xyz.xlsx", mbsrtowcs_s()
set buf
to an empty string: L""
So, how do I make this conversion?
Since cs
is 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.
You could instead say &cs[1]
or cs+1
if 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
.
这篇关于如何将 char* 转换为 LPCWSTR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!