source: trunk/Utilities/InjectLibrary/Inject/Inject.cpp @ 68

Last change on this file since 68 was 68, checked in by roman, 10 years ago
  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: Inject.cpp 68 2012-06-11 14:23:05Z roman $
6
7#include "stdafx.h"
8#include <atlstr.h>
9#include <atlpath.h>
10
11using namespace ATL::ATLPath;
12
13int _tmain(int argc, _TCHAR* argv[])
14{
15        DWORD nProcessIdentifier = 6436;
16        TCHAR pszPath[MAX_PATH] = { 0 };
17        ATLVERIFY(GetModuleFileName(NULL, pszPath, _countof(pszPath)));
18        ATLVERIFY(RemoveFileSpec(pszPath));
19        ATLVERIFY(Combine(pszPath, pszPath, _T("Library.dll")));
20        CHandle Process;
21        Process.Attach(OpenProcess(PROCESS_ALL_ACCESS, FALSE, nProcessIdentifier));
22        ATLENSURE_THROW(Process, AtlHresultFromLastError());
23        VOID* pvProcessPath = VirtualAllocEx(Process, NULL, sizeof pszPath, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
24        ATLENSURE_THROW(pvProcessPath, AtlHresultFromLastError());
25        _ATLTRY
26        {
27            ATLENSURE_THROW(WriteProcessMemory(Process, pvProcessPath, pszPath, sizeof pszPath, NULL), AtlHresultFromLastError());
28                _tprintf(_T("pvProcessPath 0x%p\n"), pvProcessPath);
29                #if !defined(_UNICODE)
30                #error Assumed is Unicode build
31                #endif
32                VOID* pvLoadLibraryAddress = GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "LoadLibraryW");
33                ATLENSURE_THROW(pvLoadLibraryAddress, AtlHresultFromLastError());
34                CHandle Thread;
35                DWORD nThreadIdentifier;
36                Thread.Attach(CreateRemoteThread(Process, NULL, NULL, (LPTHREAD_START_ROUTINE) pvLoadLibraryAddress, pvProcessPath, 0, &nThreadIdentifier));
37                ATLENSURE_THROW(Thread, AtlHresultFromLastError());
38                _tprintf(_T("nThreadIdentifier %d\n"), nThreadIdentifier);
39                const DWORD nWaitResult = WaitForSingleObject(Thread, INFINITE);
40                _tprintf(_T("nWaitResult 0x%x\n"), nWaitResult);
41        }
42        _ATLCATCHALL()
43        {
44                ATLVERIFY(VirtualFreeEx(Process, pvProcessPath, 0, MEM_RELEASE));
45                _ATLRETHROW;
46        }
47        ATLVERIFY(VirtualFreeEx(Process, pvProcessPath, 0, MEM_RELEASE));
48        return 0;
49}
50
Note: See TracBrowser for help on using the repository browser.