65,186
社区成员




int UploadFile(char * filename)
{
const char *type = "text/jpeg";
TCHAR hdrs[] = L"Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
char boundary[] = "-----------------------------7d82751e2bc0858"; //Header boundary
char nameForm[] = "file"; //Input form name
wchar_t iaddr[] = L"198.216.3.173"; //IP address
char url[] = "/api/Prober/UploadFile";
int port = 9090;
char * buffer; //Buffer containing file + headers
char * content; //Buffer containing file
FILE * pFile; //File pointer
long lSize; //File size
size_t result;
// Open file
pFile = fopen(filename, "rb");
if (pFile == NULL)
{
printf("ERROR_OPEN_FILE");
return ERROR_OPEN_FILE;
}
printf("OPEN_FILE\n");
// obtain file size:
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
content = (char*)malloc(sizeof(char)*lSize);
if (content == NULL)
{
printf("ERROR_MEMORY");
return ERROR_OPEN_FILE;
}
printf("MEMORY_ALLOCATED\t \"%d\" \n", lSize);
// copy the file into the buffer:
result = fread(content, 1, lSize, pFile);
rewind(pFile);
if (result != lSize)
{
printf("ERROR_SIZE");
return ERROR_OPEN_FILE;
}
printf("SIZE_OK\n");
// terminate
fclose(pFile);
printf("FILE_CLOSE\n");
//allocate memory to contain the whole file + HEADER
buffer = (char*)malloc(sizeof(char)*lSize + 2048);
//print header
sprintf(buffer, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", boundary, nameForm, filename);
sprintf(buffer, "%sContent-Type: %s\r\n", buffer, type);
//sprintf(buffer, "%sContent-Length: %d\r\n", buffer, lSize);
sprintf(buffer, "%s\r\n%s\r\n", buffer, content);
/**
sprintf(buffer, "%s\r\n", buffer);
memcpy(buffer + strlen(buffer),content,lSize);
sprintf(buffer, "%s\r\n", buffer);
*/
sprintf(buffer, "%s%s--\r\n", buffer, boundary);
//Open internet connection
HINTERNET hSession = InternetOpen(L"WINDOWS", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession == NULL)
{
printf("ERROR_INTERNET_OPEN");
return ERROR_OPEN_FILE;
}
printf("INTERNET_OPENED\n");
HINTERNET hConnect = InternetConnect(hSession, iaddr, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (hConnect == NULL)
{
printf("ERROR_INTERNET_CONN");
return ERROR_INTERNET_CONN;
}
printf("INTERNET_CONNECTED\n");
HINTERNET hRequest = HttpOpenRequest(hConnect, L"POST", T2W((LPTSTR)&url), NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 1);
if (hRequest == NULL)
{
printf("ERROR_INTERNET_REQ");
return ERROR_INTERNET_REQ;
}
printf("INTERNET_REQ_OPEN\n");
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen((char*)&hdrs), buffer, (DWORD)strlen(buffer));
if (!sent)
{
printf("ERROR_INTERNET_SEND");
return ERROR_INTERNET_CONN;
}
printf("INTERNET_SEND_OK\n");
printf("\r\n%s\r\n", buffer);
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return NOERROR;
}
BOOL IsExistProcess(CONST CHAR* szProcessName) {
PROCESSENTRY32 processEntry32;
HANDLE toolHelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (((int)toolHelp32Snapshot) != -1) {
processEntry32.dwSize = sizeof(processEntry32);
if (Process32First(toolHelp32Snapshot, &processEntry32))
{
do {
char output[300];
sprintf_s(output, "%ws", processEntry32.szExeFile);
if (strcmp(szProcessName, output) == 0)
{
return TRUE;
}
} while (Process32Next(toolHelp32Snapshot, &processEntry32));
}
CloseHandle(toolHelp32Snapshot);
}
return FALSE;
}