В чем ошибка?

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by AntiMaster, 9 Oct 2010.

  1. AntiMaster

    AntiMaster New Member

    Joined:
    5 Sep 2010
    Messages:
    10
    Likes Received:
    0
    Reputations:
    0
    Вот код простого аплоадера.
    Code:
    // by SlyBit (c) 07.2008 /SendFileToServer.cpp/
      
    #include "winsock2.h"
    #include <windows.h>
      
    #pragma comment(lib, "Ws2_32.lib")
    #pragma comment(linker, "/ENTRY:Entry")
       
    #define HOST_NAME "test.ru"
    #define FILE_NAME "test_file.rar"
    #define URL_NAME  "upload.php" 
     
    BOOL WINAPI SendDataToServer(PVOID pData, DWORD dwDataSize, WORD wPort, PCHAR pAddress)
    {
        SOCKET      ConnectSocket;
        sockaddr_in ClientService; 
        INT         nSendBytes = 0;
        
        if(INVALID_SOCKET == (ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))) {
            return 0;
        }
        
        ClientService.sin_family = AF_INET;
        ClientService.sin_port = htons(wPort);
        ClientService.sin_addr.s_addr = inet_addr(pAddress);
        
        if(SOCKET_ERROR == connect(ConnectSocket, (SOCKADDR*)&ClientService, sizeof(ClientService))) {
            closesocket(ConnectSocket);
            return 0;
        }
        
        if(SOCKET_ERROR == (nSendBytes = send(ConnectSocket, (PCHAR)pData, dwDataSize, 0))) {
            closesocket(ConnectSocket);
            return 0;
        }
        
        closesocket(ConnectSocket);
        
        return nSendBytes;
    }
        
    BOOL WINAPI GetIpByHostName(PCHAR pHostName, PCHAR pIp, DWORD dwIpSize)
    {
        hostent *pHosten;
        
        if(!(pHosten = gethostbyname(pHostName))) {
            return 0;
        }
        
        if(lstrlen(inet_ntoa(*(in_addr*)*pHosten->h_addr_list)) > dwIpSize) {
            return 0;
        }
        
        lstrcpy(pIp, inet_ntoa(*(in_addr*)*pHosten->h_addr_list));
       
        return 1;
    }
       
    BOOL WINAPI SendFileToServer(PCHAR pFileName, PCHAR pAddress, PCHAR pUrl)
    {
        PVOID   pFile;
        DWORD   dwFileSize, dwRetSize;
        CHAR    szIp[17];
        PCHAR   pSendData;
        HANDLE  hFile;
       
        if(!GetIpByHostName(pAddress, szIp, 17)) {
            return 0;
        }
       
        if(INVALID_HANDLE_VALUE == (hFile = CreateFile(pFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0))) {
            return 0;
        }
        if(0xFFFFFFFF == (dwFileSize = GetFileSize(hFile, 0))) {
            CloseHandle(hFile);
            return 0;
        }
        
        if(!(pSendData = (PCHAR)VirtualAlloc(0, dwFileSize+300, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))) {
            VirtualFree(pFile, 0, MEM_RELEASE);
            return 0;
        }
        
        wsprintf(pSendData, "POST /%s?file_name=%s HTTP/1.1\r\nHost: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-length: %i\r\n\r\nupload_file=",
            pUrl, pFileName, pAddress, dwFileSize+strlen("upload_file="));
       
        if(!ReadFile(hFile, pSendData+strlen(pSendData), dwFileSize, &dwRetSize, 0)) {
            VirtualFree(pSendData, 0, MEM_RELEASE);
            CloseHandle(hFile);
            return 0;
        }
        CloseHandle(hFile);
       
        if(!SendDataToServer(pSendData, strlen(pSendData), 80, szIp)) {
            VirtualFree(pSendData, 0, MEM_RELEASE);
            return 0;
        }
      
        VirtualFree(pSendData, 0, MEM_RELEASE);
      
        return 1;
    }
       
    VOID WINAPI Entry()
    {   
        WSAData wsaData;
       
        if(NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData)) {
            ExitProcess(0);
        }
         
        if(SendFileToServer(FILE_NAME, HOST_NAME, URL_NAME)) {
            MessageBox(0, "SendFileToServer Ok", 0, 0);
        } else {
            MessageBox(0, "SendFileToServer Error", 0, 0);
        }
        
        ExitProcess(0);
    }
    Но при комплимяции ошибка:
    Помогите исправить
     
  2. GRRRL Power

    GRRRL Power Elder - Старейшина

    Joined:
    13 Jul 2010
    Messages:
    823
    Likes Received:
    185
    Reputations:
    84
    VOID WINAPI Entry()

    подозреваю, что надо заменить на

    void main() или void _main()

    И кстати второе предупреждение, выданное компилятором про pFile, тоже нехорошее...
     
  3. AntiMaster

    AntiMaster New Member

    Joined:
    5 Sep 2010
    Messages:
    10
    Likes Received:
    0
    Reputations:
    0
    Изменил, но ошибка при комплимяции такая же.
     
  4. GRRRL Power

    GRRRL Power Elder - Старейшина

    Joined:
    13 Jul 2010
    Messages:
    823
    Likes Received:
    185
    Reputations:
    84
    И убрать
    #pragma comment(linker, "/ENTRY:Entry")

    Вообще странно, что студия не обработала такое. Какой компилятор? У меня в VS2008 и изначальный вариант собирается.
     
    #4 GRRRL Power, 9 Oct 2010
    Last edited: 9 Oct 2010
  5. AntiMaster

    AntiMaster New Member

    Joined:
    5 Sep 2010
    Messages:
    10
    Likes Received:
    0
    Reputations:
    0
    У меня MS Visual C++ 6.0
     
  6. GRRRL Power

    GRRRL Power Elder - Старейшина

    Joined:
    13 Jul 2010
    Messages:
    823
    Likes Received:
    185
    Reputations:
    84
    Очень, очень старый компилятор. Советую поставить как минимум 2008-й, и работать в нем спокойно.