Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 22:20pm
NS_Thy221
Rate this: bad
good
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),
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 );
  Permalink  
Comments
pasztorpisti at 22-Sep-12 8:13am
   
+5 one of the simplest solutions
Richard MacCutchan at 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 at 22-Sep-12 8:57am
   
what do you mean?you know,my english is not very good,please,man^_^
Richard MacCutchan at 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 at 22-Sep-12 11:21am
   
i want to use createremotethread to load my dll,
Richard MacCutchan at 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
good
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.
  Permalink  
Comments
pasztorpisti at 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 at 22-Sep-12 8:48am
   
That's what I meant, but your explanation is much more detailed.
NS_Thy at 22-Sep-12 9:04am
   
well,i gotta spend lots of time to transtale this comment,it is too long~~~but really thanx!
pasztorpisti at 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
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2014
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