www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D Dll injection problem

reply "Gyron" <Threk web.de> writes:
Hey there, I want to inject a dll which was created in D into a c 
Program.

Informations:
DMD vs. 2.058
IDE: MonoDevelop with Mono-D
System: Windows 7 64bit

Program Informations:
32-bit
written in c

The Injector is working for sure, so thats not the Problem.

the Source of the DLL:
import std.c.windows.windows;
import core.sys.windows.dll;

__gshared HINSTANCE g_hInst;

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID 
pvReserved)
{
     final switch (ulReason)
     {
	case DLL_PROCESS_ATTACH:
	    g_hInst = hInstance;
	    dll_process_attach( hInstance, true );
	    break;

	case DLL_PROCESS_DETACH:
	    dll_process_detach( hInstance, true );
	    break;

	case DLL_THREAD_ATTACH:
	    dll_thread_attach( true, true );
	    break;

	case DLL_THREAD_DETACH:
	    dll_thread_detach( true, true );
	    break;
     }
     return true;
}

It builds fine, but If I inject it, the program (where the dll is 
injected) says that the dll is not a valid image.
screenshot: http://imagr.eu/up/4f72240329a846_Unbenannt.png

Maybe it's because I have no def file.
But i dont know how to link it with Mono-D.
Mar 27 2012
next sibling parent reply maarten van damme <maartenvd1994 gmail.com> writes:
when I tried the previous dmd compiler (have yet to try the curent one on
this problem) I got the same problems while trying to compile a dll and use
it. I have no clue as to why this is happening. worked in 2.54 I thought
Mar 27 2012
parent "Gyron" <Threk web.de> writes:
On Tuesday, 27 March 2012 at 20:45:52 UTC, maarten van damme 
wrote:
 when I tried the previous dmd compiler (have yet to try the 
 curent one on
 this problem) I got the same problems while trying to compile a 
 dll and use
 it. I have no clue as to why this is happening. worked in 2.54 
 I thought
I thought D would be a good alternative for c++, but as it seems I need to stay with c++ :/ Thats really bad, a minus point on my "Why choose D" list.
Mar 27 2012
prev sibling parent reply Trass3r <un known.com> writes:
 Maybe it's because I have no def file.
Very possible. Just pass it to dmd like the other files. Or try the new -shared flag.
Mar 27 2012
parent reply "Gyron" <Threk web.de> writes:
On Tuesday, 27 March 2012 at 21:12:59 UTC, Trass3r wrote:
 Maybe it's because I have no def file.
Very possible. Just pass it to dmd like the other files. Or try the new -shared flag.
I have tried both now (shared and def file linking), but know it's crashing my App, lol. I inject it but it returns nothing and the App(where the dll is injected) is hanging( not responding). Could you try it maybe? I would like to know whether it's a Problem with D or with me.
Mar 27 2012
parent reply Trass3r <un known.com> writes:
 I inject it but it returns nothing and the App(where the dll is  
 injected) is hanging( not responding).

 Could you try it maybe?
 I would like to know whether it's a Problem with D or with me.
Are dlls without injection working?
Mar 27 2012
parent reply "Gyron" <Threk web.de> writes:
On Tuesday, 27 March 2012 at 21:46:23 UTC, Trass3r wrote:
 I inject it but it returns nothing and the App(where the dll 
 is injected) is hanging( not responding).

 Could you try it maybe?
 I would like to know whether it's a Problem with D or with me.
Are dlls without injection working?
I don't know, haven't tested it. Will test it when I'm back home, but I think they work( not sure).
Mar 27 2012
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
I wrote my own injector and this makes the target exe call loadlibrary.
this works on every dll I try to inject apart from dll's written in D
(starting with dmd version 2,054 or something like that).
I'll try with D calling loadlibrary on D dll's this evening.
Mar 28 2012
parent reply Trass3r <un known.com> writes:
 this works on every dll I try to inject apart from dll's written in D
 (starting with dmd version 2,054 or something like that).
If this is a regression, please narrow it down to the exact version.
Mar 28 2012
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
I went ahead and went back to as far as 2.045 and I still couldn't get a
working dll. This would suggest something is wrong with my dll injection
code but I've tested with a few other random dll's and that appears to
work. according to my debugger the problem is an access violation while
executing the main function of the D dll.
the code I use for injecting is

/**
* injectDLL injects a dll in a given process using the CreateRemoteThread
function.
*
* arguments:
*  HANDLE proc = A HANDLE to the process
*  string dllName = A string containting the name of the dll
**/
void injectDLL(HANDLE proc,string  dllName)
{
//first we need to get a pointer to the loadlibrary function
LPVOID LoadLibAddy =
cast(LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
 //The problem is that we need to pass an argument(string) but that string
is in our memory space
//so we have to allocate space to write our dllName to using
writeprocessmemory
LPVOID RemoteString = VirtualAllocEx(proc,null,dllName.length,MEM_COMMIT |
MEM_RESERVE,PAGE_READWRITE);
 //write the dllName
WriteProcessMemory(proc,RemoteString,toStringz(dllName),dllName.length,null);
 //create a thread in the remote process loading the dll
CreateRemoteThread(proc, null, 0, cast(LPTHREAD_START_ROUTINE)LoadLibAddy,
cast(LPVOID)RemoteString, 0, null);
}

Op 28 maart 2012 13:13 schreef Trass3r <un known.com> het volgende:

 this works on every dll I try to inject apart from dll's written in D
 (starting with dmd version 2,054 or something like that).
If this is a regression, please narrow it down to the exact version.
Apr 11 2012
next sibling parent "Kagamin" <spam here.lot> writes:
On Wednesday, 11 April 2012 at 13:26:23 UTC, maarten van damme 
wrote:
 I went ahead and went back to as far as 2.045 and I still 
 couldn't get a
 working dll. This would suggest something is wrong with my dll 
 injection
 code but I've tested with a few other random dll's and that 
 appears to
 work. according to my debugger the problem is an access 
 violation while
 executing the main function of the D dll.
How do you initialize runtime and GC?
Apr 11 2012
prev sibling parent reply "Kagamin" <spam here.lot> writes:
On Wednesday, 11 April 2012 at 13:26:23 UTC, maarten van damme 
wrote:
 the code I use for injecting is

 /**
 * injectDLL injects a dll in a given process using the 
 CreateRemoteThread
 function.
 *
 * arguments:
 *  HANDLE proc = A HANDLE to the process
 *  string dllName = A string containting the name of the dll
 **/
 void injectDLL(HANDLE proc,string  dllName)
 {
 //first we need to get a pointer to the loadlibrary function
 LPVOID LoadLibAddy =
 cast(LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), 
 "LoadLibraryA");
  //The problem is that we need to pass an argument(string) but 
 that string
 is in our memory space
 //so we have to allocate space to write our dllName to using
 writeprocessmemory
 LPVOID RemoteString = 
 VirtualAllocEx(proc,null,dllName.length,MEM_COMMIT |
 MEM_RESERVE,PAGE_READWRITE);
  //write the dllName
 WriteProcessMemory(proc,RemoteString,toStringz(dllName),dllName.length,null);
  //create a thread in the remote process loading the dll
 CreateRemoteThread(proc, null, 0, 
 cast(LPTHREAD_START_ROUTINE)LoadLibAddy,
 cast(LPVOID)RemoteString, 0, null);
 }
Try to run a simple C program like --- #include <windows.h> void main() { LoadLibraryA("mydll.dll"); } --- And check whether it fails and how.
Apr 11 2012
next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
"How do you initialize runtime and GC?"
I don't... it should simply call loadlibraryA on the target dll.
your C example works however.
There must be something wrong with both my method and that injector. My
example is used by a number of different sources and works for them. I
don't know why it suddenly stopped working for me.
it used to work in the past...
Apr 11 2012
prev sibling parent reply maarten van damme <maartenvd1994 gmail.com> writes:
I tried again with a few other random C dll's stolen around my system and
they all work perfectly. it's only the D dll that gives me trouble.
Apr 12 2012
parent reply "Kagamin" <spam here.lot> writes:
#include <windows.h>
void main()
{
   LPTHREAD_START_ROUTINE LoadLibAddy = 
(LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("kernel32.dll"), 
"LoadLibraryA");
   CreateThread(NULL,0,LoadLibAddy,"mydll.dll",0,NULL);
}

?
Apr 12 2012
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
works and GetLastError() returns 0 in both cases.

Op 12 april 2012 16:13 schreef Kagamin <spam here.lot> het volgende:

 #include <windows.h>
 void main()
 {
  LPTHREAD_START_ROUTINE LoadLibAddy = (LPTHREAD_START_ROUTINE)**
 GetProcAddress(**GetModuleHandle("kernel32.dll"**), "LoadLibraryA");
  CreateThread(NULL,0,**LoadLibAddy,"mydll.dll",0,**NULL);
 }

 ?
Apr 12 2012
parent reply "Gyron" <Threk web.de> writes:
On Thursday, 12 April 2012 at 14:43:01 UTC, maarten van damme
wrote:
 works and GetLastError() returns 0 in both cases.

 Op 12 april 2012 16:13 schreef Kagamin <spam here.lot> het 
 volgende:

 #include <windows.h>
 void main()
 {
  LPTHREAD_START_ROUTINE LoadLibAddy = 
 (LPTHREAD_START_ROUTINE)**
 GetProcAddress(**GetModuleHandle("kernel32.dll"**), 
 "LoadLibraryA");
  CreateThread(NULL,0,**LoadLibAddy,"mydll.dll",0,**NULL);
 }

 ?
Any news about the Problem with D ? I really would like to use D for that.
May 12 2012
parent reply maarten van damme <maartenvd1994 gmail.com> writes:
I found a couple of errors in my code but couldn't get it to work.
May 13 2012
parent reply "Kagamin" <spam here.lot> writes:
Try to make C dll, which loads D dll, and inject the C dll :)
May 14 2012
parent reply "Gyron" <Threk web.de> writes:
On Monday, 14 May 2012 at 09:53:55 UTC, Kagamin wrote:
 Try to make C dll, which loads D dll, and inject the C dll :)
I made a bootstrapper (a c DLL which loads the D Dll) and it works fine (the D entrypoint get called (a made a msgbox test) but then the Process freezes after it displayed the MsgBox. C Dll: #include <windows.h> bool _stdcall DllMain(_In_ void * _HDllHandle, _In_ unsigned _Reason, _In_opt_ void * _Reserved) { if(_Reason == DLL_PROCESS_ATTACH) { MessageBox(NULL,L"test",L"test",MB_OK); LPTHREAD_START_ROUTINE LoadLibAddy = (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryA"); CreateThread(NULL,0,LoadLibAddy,"C:\\Users\\Moritz\\Documents\\Visual Studio 11\\Projects\\D_Projects\\D_BootStrapper\\Debug\\DAstral.dll",0,NULL); } return true; } D Dll: import std.c.windows.windows; import core.sys.windows.dll; __gshared HINSTANCE g_hInst; extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) { final switch (ulReason) { case DLL_PROCESS_ATTACH: g_hInst = hInstance; MessageBoxA(null,"test2","test2",MB_OK); dll_process_attach( hInstance, true ); break; case DLL_PROCESS_DETACH: dll_process_detach( hInstance, true ); break; case DLL_THREAD_ATTACH: MessageBoxA(null,"test","test",MB_OK); dll_thread_attach( true, true ); break; case DLL_THREAD_DETACH: dll_thread_detach( true, true ); break; } return true; } Any help ?
May 15 2012
parent "Kagamin" <spam here.lot> writes:
Hmm... It's probably confused by multiple threads. You can try to 
copy the source for dll_process_attach and insert debugging code 
there.
May 16 2012