尝试构建C ++ .DLL时出现类型/转换错误

时间:2012-11-26 00:52:02

标签: c++ visual-c++

我找到了以下代码片段,并尝试使用它创建一个ISAPI .DLL。

#include <windows.h>
#include <httpfilt.h>
#include "tchar.h"
#include "strsafe.h"

// Portion of HttpOnly
DWORD WINAPI HttpFilterProc(
   PHTTP_FILTER_CONTEXT pfc,
   DWORD dwNotificationType,
   LPVOID pvNotification) {

   // Hard coded cookie length (2k bytes)
   CHAR szCookie[2048];
   DWORD cbCookieOriginal = sizeof(szCookie) / sizeof(szCookie[0]);
   DWORD cbCookie = cbCookieOriginal;

      HTTP_FILTER_SEND_RESPONSE *pResponse = 
         (HTTP_FILTER_SEND_RESPONSE*)pvNotification;

      CHAR *szHeader = "Set-Cookie:";
      CHAR *szHttpOnly = "; HttpOnly";
      if (pResponse->GetHeader(pfc,szHeader,szCookie,&cbCookie)) {
         if (SUCCEEDED(StringCchCat(szCookie,
                                    cbCookieOriginal,
                                    szHttpOnly))) {
            if (!pResponse->SetHeader(pfc,
                                      szHeader,
                                      szCookie)) {
                        // Fail securely - send no cookie!
                        pResponse->SetHeader(pfc,szHeader,"");
               }
            } else {
               pResponse->SetHeader(pfc,szHeader,"");
          }
   }

   return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

我使用VS 2010 Express创建了一个新的C ++项目。构建项目时出现以下错误:

------ Build started: Project: ISAPIHttpOnly, Configuration: Debug Win32 ------
  HttpOnly.cpp
c:\documents and settings\bob\my documents\visual studio 2010\projects\isapihttponly\isapihttponly\httponly.cpp(25): error C2664: 'StringCchCatW' : cannot convert parameter 1 from 'CHAR [2048]' to 'STRSAFE_LPWSTR'
          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我不知道如何解决这个问题。 :S

1 个答案:

答案 0 :(得分:4)

您的程序正在编译为Unicode,然后STRSAFE_LPWSTR,这是StringCchCat中第一个参数的类型,对char数组类型失败,而不是Unicode。

要解决此问题,您有两种选择,一种是将错误的字符串声明为TCHAR数组,因此可以将其预处理为wchar_t数组。但是你必须在你的代码中改变很多东西,比如文字转换需要TEXT("")宏等等。

但是看起来,你的程序没有使用Unicode字符串,所以另一种选择是将程序编译为多字节,然后你不需要更改代码中的任何内容,因为StringCchCat将有一个STRSAFE_LPSTR参数,该参数将被预处理为char *

要编译为多字节,只需转到项目设置 - &gt; 常规 - &gt; 字符集

相关问题