Параметр запуска в переменной

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

  1. AntiMaster

    AntiMaster New Member

    Joined:
    5 Sep 2010
    Messages:
    10
    Likes Received:
    0
    Reputations:
    0
    Вот код простого аплоадера:
    PHP:
          // 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 pDataDWORD dwDataSizeWORD wPortPCHAR pAddress)
          {
              
    SOCKET      ConnectSocket;
              
    sockaddr_in ClientService;
              
    INT         nSendBytes 0;
           
              if(
    INVALID_SOCKET == (ConnectSocket socket(AF_INETSOCK_STREAMIPPROTO_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*)&ClientServicesizeof(ClientService))) {
                  
    closesocket(ConnectSocket);
                  return 
    0;
              }
           
              if(
    SOCKET_ERROR == (nSendBytes send(ConnectSocket, (PCHAR)pDatadwDataSize0))) {
                  
    closesocket(ConnectSocket);
                  return 
    0;
              }
           
              
    closesocket(ConnectSocket);
           
              return 
    nSendBytes;
          }
           
          
    BOOL WINAPI GetIpByHostName(PCHAR pHostNamePCHAR pIpDWORD dwIpSize)
          {
              
    hostent *pHosten;
           
              if(!(
    pHosten gethostbyname(pHostName))) {
                  return 
    0;
              }
           
              if(
    lstrlen(inet_ntoa(*(in_addr*)*pHosten->h_addr_list)) > dwIpSize) {
                  return 
    0;
              }
           
              
    lstrcpy(pIpinet_ntoa(*(in_addr*)*pHosten->h_addr_list));
           
              return 
    1;
          }
           
          
    BOOL WINAPI SendFileToServer(PCHAR pFileNamePCHAR pAddressPCHAR pUrl)
          {
              
    PVOID   pFile;
              
    DWORD   dwFileSizedwRetSize;
              
    CHAR    szIp[17];
              
    PCHAR   pSendData;
              
    HANDLE  hFile;
           
              if(!
    GetIpByHostName(pAddressszIp17)) {
                  return 
    0;
              }
           
              if(
    INVALID_HANDLE_VALUE == (hFile CreateFile(pFileNameGENERIC_READ00OPEN_EXISTING00))) {
                  return 
    0;
              }
              if(
    0xFFFFFFFF == (dwFileSize GetFileSize(hFile0))) {
                  
    CloseHandle(hFile);
                  return 
    0;
              }
           
              if(!(
    pSendData = (PCHAR)VirtualAlloc(0dwFileSize+300MEM_COMMIT MEM_RESERVEPAGE_READWRITE))) {
                  
    VirtualFree(pFile0MEM_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=",
                  
    pUrlpFileNamepAddressdwFileSize+strlen("upload_file="));
           
              if(!
    ReadFile(hFilepSendData+strlen(pSendData), dwFileSize, &dwRetSize0)) {
                  
    VirtualFree(pSendData0MEM_RELEASE);
                  
    CloseHandle(hFile);
                  return 
    0;
              }
              
    CloseHandle(hFile);
           
              if(!
    SendDataToServer(pSendDatastrlen(pSendData), 80szIp)) {
                  
    VirtualFree(pSendData0MEM_RELEASE);
                  return 
    0;
              }
           
              
    VirtualFree(pSendData0MEM_RELEASE);
           
              return 
    1;
          }
           
          
    VOID WINAPI Entry()
          {  
              
    WSAData wsaData;
           
              if(
    NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData)) {
                  
    ExitProcess(0);
              }
           
              if(
    SendFileToServer(FILE_NAMEHOST_NAMEURL_NAME)) {
                  
    MessageBox(0"SendFileToServer Ok"00);
              } else {
                  
    MessageBox(0"SendFileToServer Error"00);
              }
           
              
    ExitProcess(0);
          }
    Как сделать чтоб в строке
    PHP:
          #define FILE_NAME "test_file.rar"
    значение FILE_NAME передавалось параметром (переменная %1) т.е.
    "C:\SendFileToServer.exe" test_file.rar
     
  2. Int32

    Int32 New Member

    Joined:
    24 Aug 2010
    Messages:
    19
    Likes Received:
    1
    Reputations:
    1
    как то так


    PHP:
    #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 pDataDWORD dwDataSizeWORD wPortPCHAR pAddress)
          {
              
    SOCKET      ConnectSocket;
              
    sockaddr_in ClientService;
              
    INT         nSendBytes 0;
           
              if(
    INVALID_SOCKET == (ConnectSocket socket(AF_INETSOCK_STREAMIPPROTO_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*)&ClientServicesizeof(ClientService))) {
                  
    closesocket(ConnectSocket);
                  return 
    0;
              }
           
              if(
    SOCKET_ERROR == (nSendBytes send(ConnectSocket, (PCHAR)pDatadwDataSize0))) {
                  
    closesocket(ConnectSocket);
                  return 
    0;
              }
           
              
    closesocket(ConnectSocket);
           
              return 
    nSendBytes;
          }
           
          
    BOOL WINAPI GetIpByHostName(PCHAR pHostNamePCHAR pIpDWORD dwIpSize)
          {
              
    hostent *pHosten;
           
              if(!(
    pHosten gethostbyname(pHostName))) {
                  return 
    0;
              }
           
              if(
    lstrlen(inet_ntoa(*(in_addr*)*pHosten->h_addr_list)) > dwIpSize) {
                  return 
    0;
              }
           
              
    lstrcpy(pIpinet_ntoa(*(in_addr*)*pHosten->h_addr_list));
           
              return 
    1;
          }
           
          
    BOOL WINAPI SendFileToServer(PCHAR pFileNamePCHAR pAddressPCHAR pUrl)
          {
              
    PVOID   pFile;
              
    DWORD   dwFileSizedwRetSize;
              
    CHAR    szIp[17];
              
    PCHAR   pSendData;
              
    HANDLE  hFile;
           
              if(!
    GetIpByHostName(pAddressszIp17)) {
                  return 
    0;
              }
           
              if(
    INVALID_HANDLE_VALUE == (hFile CreateFile(pFileNameGENERIC_READ00OPEN_EXISTING00))) {
                  return 
    0;
              }
              if(
    0xFFFFFFFF == (dwFileSize GetFileSize(hFile0))) {
                  
    CloseHandle(hFile);
                  return 
    0;
              }
           
              if(!(
    pSendData = (PCHAR)VirtualAlloc(0dwFileSize+300MEM_COMMIT MEM_RESERVEPAGE_READWRITE))) {
                  
    VirtualFree(pFile0MEM_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=",
                  
    pUrlpFileNamepAddressdwFileSize+strlen("upload_file="));
           
              if(!
    ReadFile(hFilepSendData+strlen(pSendData), dwFileSize, &dwRetSize0)) {
                  
    VirtualFree(pSendData0MEM_RELEASE);
                  
    CloseHandle(hFile);
                  return 
    0;
              }
              
    CloseHandle(hFile);
           
              if(!
    SendDataToServer(pSendDatastrlen(pSendData), 80szIp)) {
                  
    VirtualFree(pSendData0MEM_RELEASE);
                  return 
    0;
              }
           
              
    VirtualFree(pSendData0MEM_RELEASE);
           
              return 
    1;
          }
           
         
    int main(int argcchar *argv[])
          {  
              
    WSAData wsaData;
           
              if(
    NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData)) {
                  
    ExitProcess(0);
              }
           
              if(
    SendFileToServer(argv[0], HOST_NAMEURL_NAME)) {
                  
    MessageBox(0"SendFileToServer Ok"00);
              } else {
                  
    MessageBox(0"SendFileToServer Error"00);
              }
           
              
    ExitProcess(0);
     
  3. AntiMaster

    AntiMaster New Member

    Joined:
    5 Sep 2010
    Messages:
    10
    Likes Received:
    0
    Reputations:
    0
    При комплимяции ошибка:

    PHP:
    E:\>cl /O2 /MT new.cpp  /link /SUBSYSTEM:CONSOLE Kernel32.Lib User32.lib
    Microsoft 
    (R32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
    Copyright 
    (CMicrosoft Corporation.  All rights reserved.

    new.
    cpp
    e
    :\new.cpp(81) : warning C4700uninitialized local variable 'pFile' used
    Microsoft 
    (RIncremental Linker Version 9.00.21022.08
    Copyright 
    (CMicrosoft Corporation.  All rights reserved.

    /
    out:new.exe
    /SUBSYSTEM:CONSOLE
    Kernel32
    .Lib
    User32
    .lib
    new.obj
    LINK 
    error LNK2001unresolved external symbol _Entry
    new.exe fatal error LNK11201 unresolved externals
     
  4. Gar|k

    Gar|k Moderator

    Joined:
    20 Mar 2009
    Messages:
    1,166
    Likes Received:
    266
    Reputations:
    82
    Code:
    VOID WINAPI Entry() 
          {   
              WSAData wsaData;
              LPWSTR *argv; int argc;
    
    	
              if(NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData)) { 
                  ExitProcess(0); 
              } 
    
             argv = CommandLineToArgvW(GetCommandLineW(), &argc);
            // проверку втыкни на количество аргументов и вывод USAGE если надо
    
              if(SendFileToServer(argv[1], argv[2], argv[3])) { 
                  MessageBox(0, "SendFileToServer Ok", 0, 0); 
              } else { 
                  MessageBox(0, "SendFileToServer Error", 0, 0); 
              } 
            
              ExitProcess(0); 
          }
    Еще я бы тебе порекомендовал перевести все в тип TCHAR, потому что например командная строка принимается только в UNICODE
     
    _________________________