Вот код простого аплоадера: 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 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); } Как сделать чтоб в строке PHP: #define FILE_NAME "test_file.rar" значение FILE_NAME передавалось параметром (переменная %1) т.е. "C:\SendFileToServer.exe" test_file.rar
как то так 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 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; } int main(int argc, char *argv[]) { WSAData wsaData; if(NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData)) { ExitProcess(0); } if(SendFileToServer(argv[0], HOST_NAME, URL_NAME)) { MessageBox(0, "SendFileToServer Ok", 0, 0); } else { MessageBox(0, "SendFileToServer Error", 0, 0); } ExitProcess(0);
При комплимяции ошибка: PHP: E:\>cl /O2 /MT new.cpp /link /SUBSYSTEM:CONSOLE Kernel32.Lib User32.lib Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. new.cpp e:\new.cpp(81) : warning C4700: uninitialized local variable 'pFile' used Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:new.exe /SUBSYSTEM:CONSOLE Kernel32.Lib User32.lib new.obj LINK : error LNK2001: unresolved external symbol _Entry new.exe : fatal error LNK1120: 1 unresolved externals
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