Click here to Skip to main content
13,140,742 members (47,938 online)
Rate this:
Please Sign up or sign in to vote.
See more:
how to inject dll into another programe,i want to add a menu on winmine,and add handle for that menu
Posted 21-Sep-12 21:20pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

how about CreateRemoteThread,here is the code
HANDLE hThread;
char szLibPath[_MAX_PATH]; 
void* pLibRemote; 
DWORD hLibModule; 
HMODULE hKernel32 = ::GetModuleHandle("Kernel32");
pLibRemote = ::VirtualAllocEx( hProcess, NULL, sizeof(szLibPath),
::WriteProcessMemory( hProcess, pLibRemote, (void*)szLibPath,
sizeof(szLibPath), NULL );
hThread = ::CreateRemoteThread( hProcess, NULL, 0,(LPTHREAD_START_ROUTINE) ::GetProcAddress( hKernel32,
"LoadLibraryA" ),
pLibRemote, 0, NULL );
::WaitForSingleObject( hThread, INFINITE );
::GetExitCodeThread( hThread, &hLibModule );
::CloseHandle( hThread );
::VirtualFreeEx( hProcess, pLibRemote, sizeof(szLibPath), MEM_RELEASE );
hThread = ::CreateRemoteThread( hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE) ::GetProcAddress( hKernel32,
"FreeLibrary" ),
(void*)hLibModule, 0, NULL );
::WaitForSingleObject( hThread, INFINITE );
::CloseHandle( hThread );
pasztorpisti 22-Sep-12 8:13am
+5 one of the simplest solutions
Richard MacCutchan 22-Sep-12 8:47am
I don't see how this could inject a DLL into an existing executable such as winmine. What am I missing?
NS_Thy 22-Sep-12 8:57am
what do you mean?you know,my english is not very good,please,man^_^
Richard MacCutchan 22-Sep-12 9:09am
I mean that I do not understand how this code could solve the problem you are asking about.
NS_Thy 22-Sep-12 11:21am
i want to use createremotethread to load my dll,
Richard MacCutchan 22-Sep-12 13:05pm
Sorry, but that does not answer my question. What you want to do does not always match with what you can do.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You cannot 'inject' a DLL into an existing program. The executable code has to make a call out to the DLL. You could (possibly) replace a Windows DLL with your own version which offers modified functionality, but it would not be a trivial task.
pasztorpisti 22-Sep-12 8:21am
Given just a 4 because its possible to inject DLLs in several ways, however the DLL proxy method is a very nice solution. I explain it to the OP in detail: First you have to find out which DLLs does the winmine load statically and which functions does it import from the DLLs. I would choose a DLL from which winmine imports the least functions. Then you should write a dll that implements and exports the DLL functions and then you should put your DLL next the the winmine executable to the same directory witht the same name as the original DLL it imports. This way windows will load your dll into winmine and not the one that is for example in the windows system directory. Of course your DLL loads the original DLL and forwards the incoming function calls from winmine to the original DLL. Since the game loads your DLL when the program is starting up you can run your some initialization code from the DllMain, or if that isn't a right spot for your work you can execute own code when some of your dll functions are called by winmine. If DllMain isnt a good place for you to init then you might want to choose another DLL to make proxy for by selecting one that has a dllfunc that is called at the right time. However you might be forced to use other techniques as well (like api redirection setup from your DllMain is a very useful trick).
Richard MacCutchan 22-Sep-12 8:48am
That's what I meant, but your explanation is much more detailed.
NS_Thy 22-Sep-12 9:04am
well,i gotta spend lots of time to transtale this comment,it is too long~~~but really thanx!
pasztorpisti 22-Sep-12 9:08am
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100