有人可以指向一个基于winhttp / wininet的c ++包装器,它不需要mfc / atl吗?这样我就可以在MSVC express中使用它。
我希望能够通过HTML格式使用GET,POST和上传文件。
我在codeproject.com上检查了几个但是找不到合适的包装器。
有什么东西可以帮助我吗?
答案 0 :(得分:0)
在这里,我让你成为一个让你入门的人。如果您使用的是MSVC,您应该学习如何深入了解dll并找到导出的函数。 DLL Export Viewer is what I used for this.您需要找到机器上wininet.dll的位置并相应地更改代码。我希望这有帮助。所以这是我的WinInetRTW类(运行时包装器)。
// wininetrtw.h
#ifndef WININETRTW_H
#define WININETRTW_H
#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
#include <wininet.h>
typedef HINTERNET (__cdecl *HttpOpenRequestFuncPtr)(HINTERNET, LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR, DWORD, DWORD);
typedef HINTERNET (__cdecl *InternetConnectFuncPtr)(HINTERNET, LPCTSTR, INTERNET_PORT, LPCTSTR, LPCTSTR, DWORD, DWORD, DWORD_PTR);
typedef HINTERNET (__cdecl *InternetOpenFuncPtr)(LPCTSTR, DWORD, LPCTSTR, LPCTSTR, DWORD);
typedef HINTERNET (__cdecl *InternetOpenUrlFuncPtr)(HINTERNET, LPCTSTR, LPCTSTR, DWORD, DWORD, DWORD_PTR);
typedef BOOL (__cdecl *InternetReadFileFuncPtr)(HINTERNET, LPVOID, DWORD, LPDWORD);
class WinInetRTW {
public:
WinInetRTW();
~WinInetRTW();
// WinInet functions
bool LoadLibrary(std::string&);
bool HttpOpenRequest(HINTERNET hConnect, LPCTSTR lpszVerb, LPCTSTR lpszObjectName, LPCTSTR lpszVersion, LPCTSTR lpszReferer, LPCTSTR *lplpszAcceptTypes, DWORD dwFlags, DWORD_PTR dwContext);
bool InternetConnect(HINTERNET hInternet, LPCTSTR lpszServerName, INTERNET_PORT nServerPort, LPCTSTR lpszUsername, LPCTSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext);
bool InternetOpen(LPCTSTR lpszAgent, DWORD dwAccessType, LPCTSTR lpszProxyName, LPCTSTR lpszProxyBypass, DWORD dwFlags);
bool InternetOpenUrl(HINTERNET, LPCTSTR, LPCTSTR, DWORD, DWORD, DWORD_PTR);
bool InternetReadFile(HINTERNET, LPVOID, DWORD, LPDWORD);
// Helper methods
bool InternetReadFileToBuffer(HINTERNET);
// Results from respective methods
HINTERNET InternetOpenResult;
HINTERNET InternetConnectResult;
HINTERNET HttpOpenRequestResult;
HINTERNET InternetOpenUrlResult;
std::stringstream InternetReadFileStream;
private:
HttpOpenRequestFuncPtr HttpOpenRequestFunc;
InternetConnectFuncPtr InternetConnectFunc;
InternetOpenFuncPtr InternetOpenFunc;
InternetOpenUrlFuncPtr InternetOpenUrlFunc;
InternetReadFileFuncPtr InternetReadFileFunc;
};
#endif // WININETRTW_H
// wininetrtw.cpp
#include "wininetrtw.h"
WinInetRTW::WinInetRTW()
{
}
WinInetRTW::~WinInetRTW()
{
}
bool WinInetRTW::LoadLibrary(std::string& PathToDll)
{
HINSTANCE dllInstance;
dllInstance = ::LoadLibrary(PathToDll.c_str()); // Load library
this->HttpOpenRequestFunc = reinterpret_cast<HttpOpenRequestFuncPtr>(::GetProcAddress(dllInstance, "HttpOpenRequest")); // Load function
this->InternetConnectFunc = reinterpret_cast<InternetConnectFuncPtr>(::GetProcAddress(dllInstance, "InternetConnectA"));
this->InternetOpenFunc = reinterpret_cast<InternetOpenFuncPtr>(::GetProcAddress(dllInstance, "InternetOpenA"));
this->InternetOpenUrlFunc = reinterpret_cast<InternetOpenUrlFuncPtr>(::GetProcAddress(dllInstance, "InternetOpenUrlA"));
this->InternetReadFileFunc = reinterpret_cast<InternetReadFileFuncPtr>(::GetProcAddress(dllInstance, "InternetReadFile"));
if(!dllInstance){
return false;
}
return true;
}
/*
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa384363(v=vs.85).aspx
* */
bool WinInetRTW::InternetConnect(HINTERNET hInternet, LPCTSTR lpszServerName, INTERNET_PORT nServerPort, LPCTSTR lpszUsername, LPCTSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
{
InternetConnectResult = InternetConnectFunc(hInternet, lpszServerName, nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext);
if(!InternetConnectResult){
return false;
}
return true;
}
/*
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa385096(v=vs.85).aspx
* */
bool WinInetRTW::InternetOpen(LPCTSTR lpszAgent, DWORD dwAccessType, LPCTSTR lpszProxyName, LPCTSTR lpszProxyBypass, DWORD dwFlags)
{
this->InternetOpenResult = this->InternetOpenFunc(lpszAgent, dwAccessType, lpszProxyName, lpszProxyBypass, dwFlags);
if(!this->InternetOpenResult){
return false;
}
return true;
}
/*
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa385098(v=vs.85).aspx
* */
bool WinInetRTW::InternetOpenUrl(HINTERNET hInternet, LPCTSTR lpszUrl, LPCTSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
{
InternetOpenUrlResult = InternetOpenUrlFunc(hInternet, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
if(!InternetOpenUrlResult){
return false;
}
return true;
}
/*
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa385103(v=vs.85).aspx
* */
bool WinInetRTW::InternetReadFile(HINTERNET hFile, LPVOID lpBufferOut, DWORD dwNumberOfBytesToRead, LPDWORD lpdwNumberOfBytesReadOut)
{
return InternetReadFileFunc(hFile, lpBufferOut, dwNumberOfBytesToRead, lpdwNumberOfBytesReadOut);
}
bool WinInetRTW::InternetReadFileToBuffer(HINTERNET hFile)
{
DWORD dwRead;
char Buffer[1024];
while (this->InternetReadFile(hFile, Buffer, 1023, &dwRead ) )
{
if ( dwRead == 0 )
break;
Buffer[dwRead] = 0;
InternetReadFileStream << Buffer;
}
return true;
}
// main.cpp
#include "wininetrtw.h"
using namespace std;
int main(int argc, char **argv)
{
// Find the path to the file on your own system.
string PathToDll = "C:\\Windows\\winsxs\\x86_microsoft-windows-i..tocolimplementation_31bf3856ad364e35_9.4.8112.16441_none_1a52f663bc299b3d\\wininet.dll";
WinInetRTW net;
if(net.LoadLibrary(PathToDll)){
cout << "Library loaded!\n";
net.InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(net.InternetOpenUrl(net.InternetOpenResult, "http://www.google.com", NULL, 0, 0, 0))
{
net.InternetReadFileToBuffer(net.InternetOpenUrlResult);
cout << net.InternetReadFileStream.str();
}
}
else{
cout << "Library failed to load.";
}
cin.get();
return 0;
}