1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| #include "stdio.h" #include "windows.h" #include "shlobj.h" #include "Wininet.h" #include "tchar.h"
#pragma comment(lib, "Wininet.lib")
#define DEF_BUF_SIZE (4096) #define DEF_URL L"http://www.google.com/index.html" #define DEF_INDEX_FILE L"index.html"
HWND g_hWnd = NULL;
#ifdef __cplusplus extern "C" { #endif
__declspec(dllexport) void dummy() { return; } #ifdef __cplusplus } #endif
BOOL DownloadURL(LPCTSTR szURL, LPCTSTR szFile) { BOOL bRet = FALSE; HINTERNET hInternet = NULL, hURL = NULL; BYTE pBuf[DEF_BUF_SIZE] = {0,}; DWORD dwBytesRead = 0; FILE *pFile = NULL; errno_t err = 0;
hInternet = InternetOpen(L"ReverseCore", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if( NULL == hInternet ) { OutputDebugString(L"InternetOpen() failed!"); return FALSE; }
hURL = InternetOpenUrl(hInternet, szURL, NULL, 0, INTERNET_FLAG_RELOAD, 0); if( NULL == hURL ) { OutputDebugString(L"InternetOpenUrl() failed!"); goto _DownloadURL_EXIT; }
if( err = _tfopen_s(&pFile, szFile, L"wt") ) { OutputDebugString(L"fopen() failed!"); goto _DownloadURL_EXIT; }
while( InternetReadFile(hURL, pBuf, DEF_BUF_SIZE, &dwBytesRead) ) { if( !dwBytesRead ) break;
fwrite(pBuf, dwBytesRead, 1, pFile); }
bRet = TRUE;
_DownloadURL_EXIT: if( pFile ) fclose(pFile);
if( hURL ) InternetCloseHandle(hURL);
if( hInternet ) InternetCloseHandle(hInternet);
return bRet; }
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { DWORD dwPID = 0;
GetWindowThreadProcessId(hWnd, &dwPID);
if( dwPID == (DWORD)lParam ) { g_hWnd = hWnd; return FALSE; }
return TRUE; }
HWND GetWindowHandleFromPID(DWORD dwPID) { EnumWindows(EnumWindowsProc, dwPID);
return g_hWnd; }
BOOL DropFile(LPCTSTR wcsFile) { HWND hWnd = NULL; DWORD dwBufSize = 0; BYTE *pBuf = NULL; DROPFILES *pDrop = NULL; char szFile[MAX_PATH] = {0,}; HANDLE hMem = 0;
WideCharToMultiByte(CP_ACP, 0, wcsFile, -1, szFile, MAX_PATH, NULL, NULL);
dwBufSize = sizeof(DROPFILES) + strlen(szFile) + 1; if( !(hMem = GlobalAlloc(GMEM_ZEROINIT, dwBufSize)) ) { OutputDebugString(L"GlobalAlloc() failed!!!"); return FALSE; }
pBuf = (LPBYTE)GlobalLock(hMem);
pDrop = (DROPFILES*)pBuf; pDrop->pFiles = sizeof(DROPFILES); strcpy_s((char*)(pBuf + sizeof(DROPFILES)), strlen(szFile)+1, szFile);
GlobalUnlock(hMem);
if( !(hWnd = GetWindowHandleFromPID(GetCurrentProcessId())) ) { OutputDebugString(L"GetWndHandleFromPID() failed!!!"); return FALSE; }
PostMessage(hWnd, WM_DROPFILES, (WPARAM)pBuf, NULL);
return TRUE; }
DWORD WINAPI ThreadProc(LPVOID lParam) { TCHAR szPath[MAX_PATH] = {0,}; TCHAR *p = NULL;
OutputDebugString(L"ThreadProc() start...");
GetModuleFileName(NULL, szPath, sizeof(szPath)); if( p = _tcsrchr(szPath, L'\\') ) { _tcscpy_s(p+1, wcslen(DEF_INDEX_FILE)+1, DEF_INDEX_FILE);
OutputDebugString(L"DownloadURL()"); if( DownloadURL(DEF_URL, szPath) ) { OutputDebugString(L"DropFlie()"); DropFile(szPath); } }
OutputDebugString(L"ThreadProc() end...");
return 0; }
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch( fdwReason ) { case DLL_PROCESS_ATTACH : CloseHandle(CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL)); break; } return TRUE; }
|