在 Visual Studio 6.0 中使用 WinHTTP

Use WinHTTP in Visual Studio 6.0

我正在维护一个用 Visual Studio C++ 6.0 编写的遗留应用程序。

我需要用对 WinHTTP 的调用替换部分代码 API。

我在 Visual Studio 6.0 测试项目中创建了一个小演示,这样我就可以看到事情会怎样了。

当我构建代码时出现以下错误(相同的代码在 Visual Studio 2013 中完美运行):

fatal error C1083: Cannot open include file: 'winhttp.h': No such file or directory

我添加了包含路径,如下图所示:

我已经添加了库的路径,如下图所示:

尝试编译后我得到了这个:

error C2146: syntax error : missing ';' before identifier 'dwResult'
error C2501: 'DWORD_PTR' : missing storage-class or type specifiers
error C2501: 'dwResult' : missing storage-class or type specifiers
error C2065: '__in' : undeclared identifier
error C2143: syntax error : missing ')' before 'const'
warning C4229: anachronism used : modifiers on data are ignored
error C2491: 'WinHttpTimeFromSystemTime' : definition of dllimport data not allowed
error C2059: syntax error : ')'
error C2065: '__in_z' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'LPCWSTR'
warning C4229: anachronism used : modifiers on data are ignored
error C2491: 'WinHttpTimeToSystemTime' : definition of dllimport data not allowed
error C2059: syntax error : ')'
error C2065: '__in_ecount' : undeclared identifier
error C2065: 'dwUrlLength' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'LPCWSTR'
warning C4229: anachronism used : modifiers on data are ignored
error C2491: 'WinHttpCrackUrl' : definition of dllimport data not allowed
error C2059: syntax error : ')'
error C2146: syntax error : missing ')' before identifier 'LPURL_COMPONENTS'
warning C4229: anachronism used : modifiers on data are ignored
error C2491: 'WinHttpCreateUrl' : definition of dllimport data not allowed
error C2059: syntax error : ')'
error C2065: '__in_z_opt' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'LPCWSTR'
warning C4229: anachronism used : modifiers on data are ignored
error C2491: 'WinHttpOpen' : definition of dllimport data not allowed
error C2440: 'initializing' : cannot convert from 'int' to 'void *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
error C2059: syntax error : ')'
error C2061: syntax error : identifier '__out_bcount_part'
error C2061: syntax error : identifier '__in_bcount_opt'
error C2061: syntax error : identifier '__out_data_source'
error C2059: syntax error : 'return'
warning C4518: '__declspec(dllimport ) ' : storage-class or type specifier(s) unexpected here; ignored
error C2146: syntax error : missing ';' before identifier 'BOOL'
    workaround.h(426) : fatal error C1004: unexpected end of file found

将新添加的路径移到顶部后,我只得到一个错误:

c:\program files (x86)\microsoft sdks\windows\v7.1a\include\specstrings.h(11) : fatal error C1083: Cannot open include file: 'sal.h': No such file or directory

我有 copy/pasted sal.hConcurrencySal.h 到 VC98/Include,还有 copy/pasted 目录 CodeAnalysis。

现在我收到以下错误:

c:\program files (x86)\microsoft visual studio\vc98\include\sal.h(708) : warning C4068: unknown pragma
c:\program files (x86)\microsoft visual studio\vc98\include\sal.h(1472) : warning C4068: unknown pragma
c:\program files (x86)\microsoft visual studio\vc98\include\sal.h(2866) : warning C4005: '__useHeader' : macro redefinition
        c:\program files (x86)\microsoft sdks\windows\v7.1a\include\sal_supp.h(57) : see previous definition of '__useHeader'
c:\program files (x86)\microsoft visual studio\vc98\include\sal.h(2876) : warning C4005: '__on_failure' : macro redefinition
        c:\program files (x86)\microsoft sdks\windows\v7.1a\include\specstrings_supp.h(77) : see previous definition of '__on_failure'
c:\program files (x86)\microsoft sdks\windows\v7.1a\include\winnt.h(3994) : warning C4035: 'ReadPMC' : no return value
c:\program files (x86)\microsoft sdks\windows\v7.1a\include\winnt.h(4023) : warning C4035: 'ReadTimeStampCounter' : no return value
c:\program files (x86)\microsoft sdks\windows\v7.1a\include\winnt.h(12804) : error C2144: syntax error : missing ';' before type 'int'
c:\program files (x86)\microsoft sdks\windows\v7.1a\include\winnt.h(12804) : error C2501: '__inner_checkReturn' : missing storage-class or type specifiers
c:\program files (x86)\microsoft sdks\windows\v7.1a\include\winnt.h(12804) : fatal error C1004: unexpected end of file found

目前我正在谷歌搜索这个问题的解决方案...

问题:

你能向我解释一下如何在 Visual C++ 6 中建立一个项目以便它可以使用 WinHTTP API(当然,如果可能的话)?

您不能将Windows v7.1 SDK与MSVC 6一起使用。这个版本太新了,不兼容这么旧版本的编译器。

您将需要使用旧版本的 SDK,它与 MSVC 6 兼容。最后一个兼容版本是 2003 年 2 月的版本。如果您订阅了 MSDN,您仍然可以下载它。我不知道你在哪里可以在网上找到它;我看到的所有链接都已失效。

如果找不到此版本的 SDK,或者它不支持 WinHTTP v5.1,您可能需要回退到 WinHTTP v5.0。这是一个单独的可再发行组件 (winhttp5.dll),并且受 Windows NT 4 和 IE 5.01 及更高版本的支持。使用 MSVC 6 定位此目标应该没有问题。不幸的是,现在也不受支持,并且不再可供下载(截至 2004 年 10 月)。你必须有一份旧副本。

动态调用所需函数是最后一个 last-resort 选项。您需要手动编写声明(或从最新的 SDK headers 移植它们),然后使用 LoadModuleGetProcAddress 动态调用导出的函数。这是很多繁忙的工作。也许首先调查升级您的编译器工具链。确保更新到更新版本的 MSVC 不会更容易。很有可能会;向后兼容比向前兼容容易得多。

在 VC 6.0 中使用 WinHTTP 是完全可行的。您可以将它与 SDK 5.0 一起使用,即