Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to inject dll into another programe,i want to add a menu on winmine,and add handle for that menu
Posted

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),
MEM_COMMIT, PAGE_READWRITE );
::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 );
 
Share this answer
 
Comments
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,
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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900