[Win32] Remotely execute functions of a D program

Rainer Schuetze r.sagitario at gmx.de
Sat Sep 15 05:48:41 PDT 2012



On 14.09.2012 20:53, alex wrote:
> On Friday, 14 September 2012 at 18:35:53 UTC, Sean Kelly wrote:
>> On Sep 14, 2012, at 10:34 AM, alex <info at alexanderbothe.com> wrote:
>>
>>> 1) So to anyone who's got richer experiences in programming assembler
>>> and hacking/'debugging' programs than I - how would you do it?
>>> 2) And why can't I inject a D DLL right into the program? I tried it
>>> with a C DLL, it's working with that one, but then I don't have
>>> access to D-specific functions..
>>> Looking at that, would it make a difference to use dmc to build/link
>>> a dll as a D/C++ hybrid or something?
>>
>> You might want to check the madCodeHook library.  It works well, and
>> there's a version that includes source code.  Though now that I check
>> the site, it doesn't look like you can get the source-included one for
>> free any more.
>
> The primary injection routine and how it works is not the problem - I
> can successfully insert a MSVC++ Dll without any problems into a D
> program. (I've used this https://github.com/jeffora/extemory library btw
> - it's for c# :-))
> Anyway, on a DLL built with dmc or dmd 1/2, the main program immediately
> crashes or unloads the dll when returning false in the DllMain()
>
> So how to insert a D DLL into a D program?

I recently implemented an injection DLL aswell, in D, but without any 
runtime library: 
https://github.com/rainers/visuald/blob/master/tools/filemonitor.d

The command line to build it is
dmd -offilemonitor.dll -defaultlib=user32.lib -L/ENTRY:_DllMain at 12 
filemonitor.d

It also crashed when using the standard DLL framework, my suspicion is 
that the druntime-initialization isn't properly run for the injected 
DLL. If you are creating the process suspended, the main thread cannot 
run, so initialization might have to be done differently. But I haven't 
investigated any further, I was fine with just using the Windows API.

For C/C++ the VS debugger can already call functions in the debuggee as 
a side effect to watch expressions, so it should work for D aswell. It 
might be a little complicated to specify the correct symbols, though.

To improve debugging experience in VS I think the better approach would 
be to extend the capabilities of mago, the debug engine explicitely 
built for dmd generated executables. Unfortunately development of it has 
stalled.




More information about the Digitalmars-d mailing list