• <i id='c8N85'><tr id='c8N85'><dt id='c8N85'><q id='c8N85'><span id='c8N85'><b id='c8N85'><form id='c8N85'><ins id='c8N85'></ins><ul id='c8N85'></ul><sub id='c8N85'></sub></form><legend id='c8N85'></legend><bdo id='c8N85'><pre id='c8N85'><center id='c8N85'></center></pre></bdo></b><th id='c8N85'></th></span></q></dt></tr></i><div id='c8N85'><tfoot id='c8N85'></tfoot><dl id='c8N85'><fieldset id='c8N85'></fieldset></dl></div>
    <tfoot id='c8N85'></tfoot>

        <small id='c8N85'></small><noframes id='c8N85'>

        • <bdo id='c8N85'></bdo><ul id='c8N85'></ul>
        <legend id='c8N85'><style id='c8N85'><dir id='c8N85'><q id='c8N85'></q></dir></style></legend>

        如何将 char* 转换为 LPCWSTR?

        时间:2023-06-03

          • <bdo id='pmCy4'></bdo><ul id='pmCy4'></ul>

            <legend id='pmCy4'><style id='pmCy4'><dir id='pmCy4'><q id='pmCy4'></q></dir></style></legend>
            • <i id='pmCy4'><tr id='pmCy4'><dt id='pmCy4'><q id='pmCy4'><span id='pmCy4'><b id='pmCy4'><form id='pmCy4'><ins id='pmCy4'></ins><ul id='pmCy4'></ul><sub id='pmCy4'></sub></form><legend id='pmCy4'></legend><bdo id='pmCy4'><pre id='pmCy4'><center id='pmCy4'></center></pre></bdo></b><th id='pmCy4'></th></span></q></dt></tr></i><div id='pmCy4'><tfoot id='pmCy4'></tfoot><dl id='pmCy4'><fieldset id='pmCy4'></fieldset></dl></div>

              <small id='pmCy4'></small><noframes id='pmCy4'>

                <tbody id='pmCy4'></tbody>
              1. <tfoot id='pmCy4'></tfoot>
                • 本文介绍了如何将 char* 转换为 LPCWSTR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  我以 char* 开头,因为这是用于从 VBA 调用的 DLL,并且 char* 是 VBA 将字符串传递给 DLL 所必需的.

                  我需要返回一个 LPCWSTR 因为这是我试图调用的 API 函数的输入参数,我无法通过从 Unicode 切换到多字节字符集来启用转换属性窗口,因为 API 有这样的代码:

                  #if !defined(UNICODE) &&!defined(NOUNICODE)#error UNICODE 未定义.必须为正确的 API 参数定义 UNICODE.#万一

                  我试过了:

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

                  给出了这些错误:

                  错误 C2664:strlen":无法将参数 1 从const char"转换为const char *"错误 C2664:MultiByteToWideChar":无法将参数 3 从const char"转换为LPCCH"

                  我试过这个(相同的函数头),大致改编自这篇文章:

                  size_t retVal;const char * cs = charArray;size_t 长度=strlen(cs);wchar_t * buf = new wchar_t[length]();//值初始化为 0(见下文)size_t wn = mbsrtowcs_s(&retVal,buf,20, &cs, length + 1, NULL);返回缓冲区;

                  编译没问题,但是当我向它传递一个示例字符串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:

                  #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 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何测试预处理器符号是否已#define'd 但没有值? 下一篇:在 msvc 中引用临时文件

                  相关文章

                  1. <small id='PPEke'></small><noframes id='PPEke'>

                    <tfoot id='PPEke'></tfoot>

                      • <bdo id='PPEke'></bdo><ul id='PPEke'></ul>
                      <i id='PPEke'><tr id='PPEke'><dt id='PPEke'><q id='PPEke'><span id='PPEke'><b id='PPEke'><form id='PPEke'><ins id='PPEke'></ins><ul id='PPEke'></ul><sub id='PPEke'></sub></form><legend id='PPEke'></legend><bdo id='PPEke'><pre id='PPEke'><center id='PPEke'></center></pre></bdo></b><th id='PPEke'></th></span></q></dt></tr></i><div id='PPEke'><tfoot id='PPEke'></tfoot><dl id='PPEke'><fieldset id='PPEke'></fieldset></dl></div>

                      <legend id='PPEke'><style id='PPEke'><dir id='PPEke'><q id='PPEke'></q></dir></style></legend>