www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - link from a dll to another function in another dll?

reply maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd2e110aadbfa04a122d838
Content-Type: text/plain; charset=ISO-8859-1

Hello everyone, this is my second post in the digitalmars.d newsgroup and I
hope it gets as good support and suggestions as my first post :)

I'm playing around with the d programming language and am trying out some
exotic things you normally would write in c++.
Right now I'm trying to 'intercept' all calls from a program to a dll by
renaming that dll and writing my own in d.
In c++ you would write in the header file:
#pragma comment(linker,
"/export:exportfunction=nameofotherdll.dll.destinationfunction, location")

How could one write this in the d programming language?
Asuming this has to be done with the pragma(lib,...) function but I don't
really know how.

thanks in advance,

Maarten

--000e0cd2e110aadbfa04a122d838
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hello everyone, this is my second post in the digitalmars.d newsgroup and I=
 hope it gets as good support and suggestions as my first post :)<div><br><=
/div><div>I&#39;m playing around with the d programming language and am try=
ing out some exotic things you normally would write in c++.</div>
<div>Right now I&#39;m trying to &#39;intercept&#39; all calls from a progr=
am to a dll by renaming that dll and writing my own in d.</div><div>In c++ =
you would write in the header file:</div><div>#pragma comment(linker, &quot=
;/export:exportfunction=3Dnameofotherdll.dll.destinationfunction, location&=
quot;)</div>
<div><br></div><div>How could one write this in the d programming language?=
</div><div>Asuming this has to be done with the pragma(lib,...) function bu=
t I don&#39;t really know how.</div><div><br></div><div>thanks in advance,<=
/div>
<div><br></div><div>Maarten</div>

--000e0cd2e110aadbfa04a122d838--
Apr 17 2011
next sibling parent reply "Robert Jacques" <sandford jhu.edu> writes:
On Sun, 17 Apr 2011 16:09:02 -0400, maarten van damme  
<maartenvd1994 gmail.com> wrote:

 Hello everyone, this is my second post in the digitalmars.d newsgroup  
 and I
 hope it gets as good support and suggestions as my first post :)

 I'm playing around with the d programming language and am trying out some
 exotic things you normally would write in c++.
 Right now I'm trying to 'intercept' all calls from a program to a dll by
 renaming that dll and writing my own in d.
 In c++ you would write in the header file:
 #pragma comment(linker,
 "/export:exportfunction=nameofotherdll.dll.destinationfunction, location")

 How could one write this in the d programming language?
 Asuming this has to be done with the pragma(lib,...) function but I don't
 really know how.

 thanks in advance,

 Maarten

I don't know of an automated way of doing this is D. pragma(lib,...) exists, but it simply loads a specified static library. (i.e. to simplify linking/ project setup, etc). Personally, I'd just export extern(C)/extern(System) functions toa DLL, and link in a manually define the renamed DLL using a .def file and implib.
Apr 17 2011
parent reply Rainer Schuetze <r.sagitario gmx.de> writes:
I'm not sure your wrapping will work with kernel32.dll, but in general 
here are a few tips:

- most functions in the windows API use the __stdcall calling convention 
in C/C++, which translates to D as "extern(Windows)"

- this will usually add the number of bytes passed on the stack as a 
" NN" postfix to the function name. This postfix does not exist in 
kernel32.dll, but in the import library kernel32.lib that you find in 
the dmd lib folder. Maybe you can use the standard import library, or 
use the translation shown below.

- as the exported function and the function you want to chain to have 
identical names, you have to change at least one of these and modify 
them in some build step. I'd suggest to do this in the def file:

The symbols in the d-source file containing:

----
extern(Windows) HANDLE imported_GetCurrentProcess();

export extern(Windows) HANDLE internal_GetCurrentProcess()
{
   return imported_GetCurrentProcess();
}
----

can be mapped to other symbols in the def file:

----
EXPORTS
   GetCurrentProcess = internal_GetCurrentProcess

IMPORTS
   imported_GetCurrentProcess = kernel33.GetCurrentProcess
----

- if you don't know the number of arguments, you should not call the 
wrapped function, as this will change the callstack. Instead, you should 
just jump to it:

void internal_hread()
{
   asm
   {
     naked;
     jmp imported_hread;
   }
}

I haven't tried all that, though, so there might be some mistakes...

Rainer


Denis Koroskin wrote:
 On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme 
 <maartenvd1994 gmail.com> wrote:
 
 I've changed this, I think I'm still kinda confused with lib files. 
 They've
 told me you can't do something with them without a .di file
 So I went ahead and made a kernel33.di file. I now import it in 
 kernel32.d
 and my declaration is
 System(C){
 export void * exportedfunctionblablabal(){
    return exportedfunctionblablablal();
 }
 ...
 }

 The file in the directory are:
 kernel32.d : http://dl.dropbox.com/u/15024434/d/kernel32.d
 kernel33.di : http://dl.dropbox.com/u/15024434/d/kernel33.di
 kernel33.lib : http://dl.dropbox.com/u/15024434/d/kernel33.lib
 kernel33.dll : http://dl.dropbox.com/u/15024434/d/kernel33.dll

 I've tried to compile using dmd -d kernel32.d kernel33.di kernel33.lib 
 but
 it throws errors like
 "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
 I have literally no clue why this is the case, can someone help me out or
 look at the files?

 2011/4/27 maarten van damme <maartenvd1994 gmail.com>

 I'm afraid I've been a little unclear.
 I've copied kernel32.dll from the windows dir, renamed it to 
 kernel33.dll
 and generated a .lib from it using implib.
 Then I've created a d file with a correct dllmain(stolen from 
 examples) and
 between

 system(C){
 export void * exportedfunctionfromkernel33.dll();
 export void * exportedfunction2fromkernel33.dll();
 ...
 }

 But it looks like you can't both declare a function from another lib and
 export it at the same time.


In your kernel33.di, try making it extern (C) export void* _hread(); etc. You functions get D mangling otherwise. I'd also suggest you to start with a less complex example, e.g. export only one function, make sure it works, then add the rest. If you think your .lib files doesn't do its job, try using .def file instead. I find them extremely helpful, and they are a lot easier to edit/extend. Hope that helps.

Apr 30 2011
parent reply Rainer Schuetze <r.sagitario gmx.de> writes:
It seems you have hit another of those dreaded optlink bugs.

With less symbols, it works if you declare the imports like this 
(because of the described name mangling):

IMPORTS	
	_imported_hread 0 =  kernel33._hread

2 more notes:
- you don't need to import kernel33.di
- you should not use "SINGLE" in the DATA statement of the def file, it 
will share the memory across processes.



maarten van damme wrote:
 Number overflow?
 So I implemented the suggested changes (you can check them out 
 at http://dl.dropbox.com/u/15024434/version2.zip)
 But now I get when I compile it : 
 "kernel32.def(738) : Error 12: Number Overflow: (strange symbol over here)"
 
 I do agree I should've picked a simpler example but I think the 
 statisfaction will be even bigger if I were to succeed :p
 
 2011/5/1 maarten van damme <maartenvd1994 gmail.com 
 <mailto:maartenvd1994 gmail.com>>
 
     Wow, thanks for the help
     The first thing I did was in the .di file adding extern(windows){ ... }
     and now compiling doesn't give errors and when examining with dllexp
     I can see that it exports the same functions as the real kernel32.dll :D
 
     Now I'm going to implement all other suggested changes, thanks a lot
 
 
     2011/4/30 Rainer Schuetze <r.sagitario gmx.de
     <mailto:r.sagitario gmx.de>>
 
         I'm not sure your wrapping will work with kernel32.dll, but in
         general here are a few tips:
 
         - most functions in the windows API use the __stdcall calling
         convention in C/C++, which translates to D as "extern(Windows)"
 
         - this will usually add the number of bytes passed on the stack
         as a " NN" postfix to the function name. This postfix does not
         exist in kernel32.dll, but in the import library kernel32.lib
         that you find in the dmd lib folder. Maybe you can use the
         standard import library, or use the translation shown below.
 
         - as the exported function and the function you want to chain to
         have identical names, you have to change at least one of these
         and modify them in some build step. I'd suggest to do this in
         the def file:
 
         The symbols in the d-source file containing:
 
         ----
         extern(Windows) HANDLE imported_GetCurrentProcess();
 
         export extern(Windows) HANDLE internal_GetCurrentProcess()
         {
          return imported_GetCurrentProcess();
         }
         ----
 
         can be mapped to other symbols in the def file:
 
         ----
         EXPORTS
          GetCurrentProcess = internal_GetCurrentProcess
 
         IMPORTS
          imported_GetCurrentProcess = kernel33.GetCurrentProcess
         ----
 
         - if you don't know the number of arguments, you should not call
         the wrapped function, as this will change the callstack.
         Instead, you should just jump to it:
 
         void internal_hread()
         {
          asm
          {
            naked;
            jmp imported_hread;
          }
         }
 
         I haven't tried all that, though, so there might be some mistakes...
 
         Rainer
 
 
 
         Denis Koroskin wrote:
 
             On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme
             <maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>>
             wrote:
 
                 I've changed this, I think I'm still kinda confused with
                 lib files. They've
                 told me you can't do something with them without a .di file
                 So I went ahead and made a kernel33.di file. I now
                 import it in kernel32.d
                 and my declaration is
                 System(C){
                 export void * exportedfunctionblablabal(){
                   return exportedfunctionblablablal();
                 }
                 ....
                 }
 
                 The file in the directory are:
                 kernel32.d : http://dl.dropbox.com/u/15024434/d/kernel32.d
                 kernel33.di : http://dl.dropbox.com/u/15024434/d/kernel33.di
                 kernel33.lib :
                 http://dl.dropbox.com/u/15024434/d/kernel33.lib
                 kernel33.dll :
                 http://dl.dropbox.com/u/15024434/d/kernel33.dll
 
                 I've tried to compile using dmd -d kernel32.d
                 kernel33.di kernel33.lib but
                 it throws errors like
                 "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
                 I have literally no clue why this is the case, can
                 someone help me out or
                 look at the files?
 
                 2011/4/27 maarten van damme <maartenvd1994 gmail.com
                 <mailto:maartenvd1994 gmail.com>>
 
                     I'm afraid I've been a little unclear.
                     I've copied kernel32.dll from the windows dir,
                     renamed it to kernel33.dll
                     and generated a .lib from it using implib.
                     Then I've created a d file with a correct
                     dllmain(stolen from examples) and
                     between
 
                     system(C){
                     export void * exportedfunctionfromkernel33.dll();
                     export void * exportedfunction2fromkernel33.dll();
                     ....
                     }
 
                     But it looks like you can't both declare a function
                     from another lib and
                     export it at the same time.
 
 
             In your kernel33.di, try making it extern (C) export void*
             _hread(); etc. You functions get D mangling otherwise.
 
             I'd also suggest you to start with a less complex example,
             e.g. export only one function, make sure it works, then add
             the rest.
 
             If you think your .lib files doesn't do its job, try using
             .def file instead. I find them extremely helpful, and they
             are a lot easier to edit/extend.
 
             Hope that helps.
 
 
 

May 01 2011
parent reply Rainer Schuetze <r.sagitario gmx.de> writes:
I must have completely misunderstood what you want to do. What do you 
want to build from kernel33.def? Isn't kernel33.dll the original DLL 
that you want to intercept by replacing it with the compiled DLL?

maarten van damme wrote:
 Great, now the error in kernel32.def is resolved but it gets the same 
 problem in kernel33.def.
 here is the start of the exports from kernel33.def:
 EXPORTS
 _hread  1334
 how can I change this to resolve that?
 
 2011/5/1 Rainer Schuetze <r.sagitario gmx.de <mailto:r.sagitario gmx.de>>
 
     It seems you have hit another of those dreaded optlink bugs.
 
     With less symbols, it works if you declare the imports like this
     (because of the described name mangling):
 
     IMPORTS
            _imported_hread 0 =  kernel33._hread
 
     2 more notes:
     - you don't need to import kernel33.di
     - you should not use "SINGLE" in the DATA statement of the def file,
     it will share the memory across processes.
 
 
 
     maarten van damme wrote:
 
         Number overflow?
         So I implemented the suggested changes (you can check them out
         at http://dl.dropbox.com/u/15024434/version2.zip)
 
         But now I get when I compile it : "kernel32.def(738) : Error 12:
         Number Overflow: (strange symbol over here)"
 
         I do agree I should've picked a simpler example but I think the
         statisfaction will be even bigger if I were to succeed :p
 
         2011/5/1 maarten van damme <maartenvd1994 gmail.com
         <mailto:maartenvd1994 gmail.com> <mailto:maartenvd1994 gmail.com
         <mailto:maartenvd1994 gmail.com>>>
 
 
            Wow, thanks for the help
            The first thing I did was in the .di file adding
         extern(windows){ .... }
            and now compiling doesn't give errors and when examining with
         dllexp
            I can see that it exports the same functions as the real
         kernel32.dll :D
 
            Now I'm going to implement all other suggested changes,
         thanks a lot
 
 
            2011/4/30 Rainer Schuetze <r.sagitario gmx.de
         <mailto:r.sagitario gmx.de>
            <mailto:r.sagitario gmx.de <mailto:r.sagitario gmx.de>>>
 
 
                I'm not sure your wrapping will work with kernel32.dll,
         but in
                general here are a few tips:
 
                - most functions in the windows API use the __stdcall calling
                convention in C/C++, which translates to D as
         "extern(Windows)"
 
                - this will usually add the number of bytes passed on the
         stack
                as a " NN" postfix to the function name. This postfix
         does not
                exist in kernel32.dll, but in the import library kernel32.lib
                that you find in the dmd lib folder. Maybe you can use the
                standard import library, or use the translation shown below..
 
                - as the exported function and the function you want to
         chain to
                have identical names, you have to change at least one of
         these
                and modify them in some build step. I'd suggest to do this in
                the def file:
 
                The symbols in the d-source file containing:
 
                ----
                extern(Windows) HANDLE imported_GetCurrentProcess();
 
                export extern(Windows) HANDLE internal_GetCurrentProcess()
                {
                 return imported_GetCurrentProcess();
                }
                ----
 
                can be mapped to other symbols in the def file:
 
                ----
                EXPORTS
                 GetCurrentProcess = internal_GetCurrentProcess
 
                IMPORTS
                 imported_GetCurrentProcess = kernel33.GetCurrentProcess
                ----
 
                - if you don't know the number of arguments, you should
         not call
                the wrapped function, as this will change the callstack.
                Instead, you should just jump to it:
 
                void internal_hread()
                {
                 asm
                 {
                   naked;
                   jmp imported_hread;
                 }
                }
 
                I haven't tried all that, though, so there might be some
         mistakes...
 
                Rainer
 
 
 
                Denis Koroskin wrote:
 
                    On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme
                    <maartenvd1994 gmail.com
         <mailto:maartenvd1994 gmail.com> <mailto:maartenvd1994 gmail.com
         <mailto:maartenvd1994 gmail.com>>>
 
                    wrote:
 
                        I've changed this, I think I'm still kinda
         confused with
                        lib files. They've
                        told me you can't do something with them without
         a .di file
                        So I went ahead and made a kernel33.di file.. I now
                        import it in kernel32.d
                        and my declaration is
                        System(C){
                        export void * exportedfunctionblablabal(){
                          return exportedfunctionblablablal();
                        }
                        ....
                        }
 
                        The file in the directory are:
                        kernel32.d :
         http://dl.dropbox.com/u/15024434/d/kernel32.d
                        kernel33.di :
         http://dl.dropbox.com/u/15024434/d/kernel33.di
                        kernel33.lib :
                        http://dl.dropbox.com/u/15024434/d/kernel33.lib
                        kernel33.dll :
                        http://dl.dropbox.com/u/15024434/d/kernel33.dll
 
                        I've tried to compile using dmd -d kernel32.d
                        kernel33.di kernel33.lib but
                        it throws errors like
                        "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
                        I have literally no clue why this is the case, can
                        someone help me out or
                        look at the files?
 
                        2011/4/27 maarten van damme
         <maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>
                        <mailto:maartenvd1994 gmail.com
         <mailto:maartenvd1994 gmail.com>>>
 
 
                            I'm afraid I've been a little unclear.
                            I've copied kernel32.dll from the windows dir,
                            renamed it to kernel33.dll
                            and generated a .lib from it using implib.
                            Then I've created a d file with a correct
                            dllmain(stolen from examples) and
                            between
 
                            system(C){
                            export void * exportedfunctionfromkernel33.dll();
                            export void *
         exportedfunction2fromkernel33.dll();
                            ....
                            }
 
                            But it looks like you can't both declare a
         function
                            from another lib and
                            export it at the same time.
 
 
                    In your kernel33.di, try making it extern (C) export
         void*
                    _hread(); etc. You functions get D mangling otherwise.
 
                    I'd also suggest you to start with a less complex
         example,
                    e.g. export only one function, make sure it works,
         then add
                    the rest.
 
                    If you think your .lib files doesn't do its job, try
         using
                    .def file instead. I find them extremely helpful, and
         they
                    are a lot easier to edit/extend.
 
                    Hope that helps.
 
 
 
 

May 01 2011
parent Rainer Schuetze <r.sagitario gmx.de> writes:
If you are coming from java, you are probalby used to the ideal world of 
VM programming, where everything is abstracted away from the actual 
platform you are running on.

What you are trying to do with replacing kernel32.dll pokes around in 
very lowlevel Windows specific implementation details. The complications 
are not introduced by the language, but by the DLL file layout and the 
tools available to deal with it. I have no experience using java, but I 
don't think you'll be able to do something similar with it. Even with 
C/C++, it's almost impossible without inline assembler and similar 
linker support as with D.

Coming back to your wrapper DLL, compilation should work (with symbols 
restricted to those actual needed to avoid the recent bug) with

dmd kernel32.d kernel32.def

The generated kernel32.dll is good enough to replace the original DLL 
(if it has not been loaded from the windows directory before the 
implicite DLL loading takes place). As soon as it is loaded, its' import 
dependencies will automatically load kernel33.dll. No other build steps 
required.


maarten van damme wrote:
 To avoid any confusing on my end, the files I need are
 kernel33.dll (original kernel32.dll)
 kernel33.def (So d can acces those functions)
 kernel32.def (renaming happens over here, contains a list of all 
 import-exported functions)
 kernel32.d (the code)
 
 kernel33.def can be seen as a substitute for the import libary generated 
 by implib?
 and when you have an import library you also need a .di file to acces 
 the contents?
 
 the compile commands are
 dmd -d kernel32 kernel32.def kernel33.def
 dmd -d kernel32.obj kernel32.def
 
 Tomorow I can show the files (can't acces them from this laptop)
 and I'll post some snipets from the .def files ?
 
 Can someone correct me? I'm finding d promising as language but some 
 things seem to be overly complicated to me (I'm a java guy, you have 
 .java for source, .class compiled and .jar packaged Seems way simpler xd).
 
 
 really apreciating your info here :D
 2011/5/1 maarten van damme <maartenvd1994 gmail.com 
 <mailto:maartenvd1994 gmail.com>>
 
     Yes, and i have a kernel32.def for my .d file and a kernel33.def for
     the original kernel dll. Your not confused, I am. I thought i needed
     kerel33.def so i could acces the dll from d, isnt this the case?
 
     Op 1-mei-2011 22:10 schreef "Rainer Schuetze" <r.sagitario gmx.de
     <mailto:r.sagitario gmx.de>> het volgende:
 
      > I must have completely misunderstood what you want to do. What do
     you
      > want to build from kernel33.def? Isn't kernel33.dll the original DLL
      > that you want to intercept by replacing it with the compiled DLL?
      >
      > maarten van damme wrote:
      >> Great, now the error in kernel32.def is resolved but it gets the
     same
      >> problem in kernel33.def.
      >> here is the start of the exports from kernel33.def:
      >> EXPORTS
      >> _hread  1334
      >> how can I change this to resolve that?
      >>
      >> 2011/5/1 Rainer Schuetze <r.sagitario gmx.de
     <mailto:r.sagitario gmx.de> <mailto:r.sagitario gmx.de
     <mailto:r.sagitario gmx.de>>>
      >>
      >> It seems you have hit another of those dreaded optlink bugs.
      >>
      >> With less symbols, it works if you declare the imports like this
      >> (because of the described name mangling):
      >>
      >> IMPORTS
      >> _imported_hread 0 = kernel33._hread
      >>
      >> 2 more notes:
      >> - you don't need to import kernel33.di
      >> - you should not use "SINGLE" in the DATA statement of the def file,
      >> it will share the memory across processes.
      >>
      >>
      >>
      >> maarten van damme wrote:
      >>
      >> Number overflow?
      >> So I implemented the suggested changes (you can check them out
      >> at http://dl.dropbox.com/u/15024434/version2.zip)
      >>
      >> But now I get when I compile it : "kernel32.def(738) : Error 12:
      >> Number Overflow: (strange symbol over here)"
      >>
      >> I do agree I should've picked a simpler example but I think the
      >> statisfaction will be even bigger if I were to succeed :p
      >>
      >> 2011/5/1 maarten van damme <maartenvd1994 gmail.com
     <mailto:maartenvd1994 gmail.com>
      >> <mailto:maartenvd1994 gmail.com
     <mailto:maartenvd1994 gmail.com>> <mailto:maartenvd1994 gmail.com
     <mailto:maartenvd1994 gmail.com>
      >> <mailto:maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>>>>
      >>
      >>
      >> Wow, thanks for the help
      >> The first thing I did was in the .di file adding
      >> extern(windows){ .... }
      >> and now compiling doesn't give errors and when examining with
      >> dllexp
      >> I can see that it exports the same functions as the real
      >> kernel32.dll :D
      >>
      >> Now I'm going to implement all other suggested changes,
      >> thanks a lot
      >>
      >>
      >> 2011/4/30 Rainer Schuetze <r.sagitario gmx.de
     <mailto:r.sagitario gmx.de>
      >> <mailto:r.sagitario gmx.de <mailto:r.sagitario gmx.de>>
      >> <mailto:r.sagitario gmx.de <mailto:r.sagitario gmx.de>
     <mailto:r.sagitario gmx.de <mailto:r.sagitario gmx.de>>>>
      >>
      >>
      >> I'm not sure your wrapping will work with kernel32.dll,
      >> but in
      >> general here are a few tips:
      >>
      >> - most functions in the windows API use the __stdcall calling
      >> convention in C/C++, which translates to D as
      >> "extern(Windows)"
      >>
      >> - this will usually add the number of bytes passed on the
      >> stack
      >> as a " NN" postfix to the function name. This postfix
      >> does not
      >> exist in kernel32.dll, but in the import library kernel32.lib
      >> that you find in the dmd lib folder. Maybe you can use the
      >> standard import library, or use the translation shown below..
      >>
      >> - as the exported function and the function you want to
      >> chain to
      >> have identical names, you have to change at least one of
      >> these
      >> and modify them in some build step. I'd suggest to do this in
      >> the def file:
      >>
      >> The symbols in the d-source file containing:
      >>
      >> ----
      >> extern(Windows) HANDLE imported_GetCurrentProcess();
      >>
      >> export extern(Windows) HANDLE internal_GetCurrentProcess()
      >> {
      >> return imported_GetCurrentProcess();
      >> }
      >> ----
      >>
      >> can be mapped to other symbols in the def file:
      >>
      >> ----
      >> EXPORTS
      >> GetCurrentProcess = internal_GetCurrentProcess
      >>
      >> IMPORTS
      >> imported_GetCurrentProcess = kernel33.GetCurrentProcess
      >> ----
      >>
      >> - if you don't know the number of arguments, you should
      >> not call
      >> the wrapped function, as this will change the callstack.
      >> Instead, you should just jump to it:
      >>
      >> void internal_hread()
      >> {
      >> asm
      >> {
      >> naked;
      >> jmp imported_hread;
      >> }
      >> }
      >>
      >> I haven't tried all that, though, so there might be some
      >> mistakes...
      >>
      >> Rainer
      >>
      >>
      >>
      >> Denis Koroskin wrote:
      >>
      >> On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme
      >> <maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>
      >> <mailto:maartenvd1994 gmail.com
     <mailto:maartenvd1994 gmail.com>> <mailto:maartenvd1994 gmail.com
     <mailto:maartenvd1994 gmail.com>
      >> <mailto:maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>>>>
      >>
      >> wrote:
      >>
      >> I've changed this, I think I'm still kinda
      >> confused with
      >> lib files.. They've
      >> told me you can't do something with them without
      >> a .di file
      >> So I went ahead and made a kernel33.di file.. I now
      >> import it in kernel32.d
      >> and my declaration is
      >> System(C){
      >> export void * exportedfunctionblablabal(){
      >> return exportedfunctionblablablal();
      >> }
      >> ....
      >> }
      >>
      >> The file in the directory are:
      >> kernel32.d :
      >> http://dl..dropbox.com/u/15024434/d/kernel32.d
     <http://dl.dropbox.com/u/15024434/d/kernel32.d>
      >> kernel33.di :
      >> http://dl.dropbox.com/u/15024434/d/kernel33.di
      >> kernel33.lib :
      >> http://dl.dropbox.com/u/15024434/d/kernel33.lib
      >> kernel33.dll :
      >> http://dl.dropbox.com/u/15024434/d/kernel33.dll
      >>
      >> I've tried to compile using dmd -d kernel32.d
      >> kernel33.di kernel33.lib but
      >> it throws errors like
      >> "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
      >> I have literally no clue why this is the case, can
      >> someone help me out or
      >> look at the files?
      >>
      >> 2011/4/27 maarten van damme
      >> <maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>
     <mailto:maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>>
      >> <mailto:maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>
      >> <mailto:maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>>>>
      >>
      >>
      >> I'm afraid I've been a little unclear.
      >> I've copied kernel32.dll from the windows dir,
      >> renamed it to kernel33.dll
      >> and generated a .lib from it using implib.
      >> Then I've created a d file with a correct
      >> dllmain(stolen from examples) and
      >> between
      >>
      >> system(C){
      >> export void * exportedfunctionfromkernel33.dll();
      >> export void *
      >> exportedfunction2fromkernel33.dll();
      >> ....
      >> }
      >>
      >> But it looks like you can't both declare a
      >> function
      >> from another lib and
      >> export it at the same time.
      >>
      >>
      >> In your kernel33.di, try making it extern (C) export
      >> void*
      >> _hread(); etc. You functions get D mangling otherwise.
      >>
      >> I'd also suggest you to start with a less complex
      >> example,
      >> e.g. export only one function, make sure it works,
      >> then add
      >> the rest.
      >>
      >> If you think your .lib files doesn't do its job, try
      >> using
      >> .def file instead. I find them extremely helpful, and
      >> they
      >> are a lot easier to edit/extend.
      >>
      >> Hope that helps.
      >>
      >>
      >>
      >>
 
 

May 02 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd2e11090738f04a12cef01
Content-Type: text/plain; charset=ISO-8859-1

The problem with that aproach would be that the functions are in another
location in the export table.
I've read that the locations need to stay exactly the same.
Am I wrong about this?

2011/4/18 Robert Jacques <sandford jhu.edu>

 On Sun, 17 Apr 2011 16:09:02 -0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  Hello everyone, this is my second post in the digitalmars.d newsgroup and
 I
 hope it gets as good support and suggestions as my first post :)

 I'm playing around with the d programming language and am trying out some
 exotic things you normally would write in c++.
 Right now I'm trying to 'intercept' all calls from a program to a dll by
 renaming that dll and writing my own in d.
 In c++ you would write in the header file:
 #pragma comment(linker,
 "/export:exportfunction=nameofotherdll.dll.destinationfunction, location")

 How could one write this in the d programming language?
 Asuming this has to be done with the pragma(lib,...) function but I don't
 really know how.

 thanks in advance,

 Maarten

I don't know of an automated way of doing this is D. pragma(lib,...) exists, but it simply loads a specified static library. (i.e. to simplify linking/ project setup, etc). Personally, I'd just export extern(C)/extern(System) functions toa DLL, and link in a manually define the renamed DLL using a .def file and implib.

--000e0cd2e11090738f04a12cef01 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable The problem with that aproach would be that the functions are in another lo= cation in the export table.<div>I&#39;ve read that the locations need to st= ay exactly the same.</div><div>Am I wrong about this?<br><br><div class=3D"= gmail_quote"> 2011/4/18 Robert Jacques <span dir=3D"ltr">&lt;<a href=3D"mailto:sandford j= hu.edu">sandford jhu.edu</a>&gt;</span><br><blockquote class=3D"gmail_quote= " style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">= <div> <div></div><div class=3D"h5">On Sun, 17 Apr 2011 16:09:02 -0400, maarten va= n damme &lt;<a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_blank">ma= artenvd1994 gmail.com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Hello everyone, this is my second post in the digitalmars.d newsgroup and I= <br> hope it gets as good support and suggestions as my first post :)<br> <br> I&#39;m playing around with the d programming language and am trying out so= me<br> exotic things you normally would write in c++.<br> Right now I&#39;m trying to &#39;intercept&#39; all calls from a program to= a dll by<br> renaming that dll and writing my own in d.<br> In c++ you would write in the header file:<br> #pragma comment(linker,<br> &quot;/export:exportfunction=3Dnameofotherdll.dll.destinationfunction, loca= tion&quot;)<br> <br> How could one write this in the d programming language?<br> Asuming this has to be done with the pragma(lib,...) function but I don&#39= ;t<br> really know how.<br> <br> thanks in advance,<br> <br> Maarten<br> </blockquote> <br></div></div> I don&#39;t know of an automated way of doing this is D. pragma(lib,...) ex= ists, but it simply loads a specified static library. (i.e. to simplify lin= king/ project setup, etc). Personally, I&#39;d just export extern(C)/extern= (System) functions toa DLL, and link in a manually define the renamed DLL u= sing a .def file and implib.<br> </blockquote></div><br></div> --000e0cd2e11090738f04a12cef01--
Apr 18 2011
prev sibling next sibling parent "Robert Jacques" <sandford jhu.edu> writes:
On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme  
<maartenvd1994 gmail.com> wrote:

 The problem with that aproach would be that the functions are in another
 location in the export table.
 I've read that the locations need to stay exactly the same.
 Am I wrong about this?

I don't know for sure, but my gut would say that not knowing the exact layout of the DLL is half the point. In practice, I've used D with DLLs that have drastically added to/changed their layout (according to dumpbin) without a problem.
Apr 18 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--00504502ccab8faa3804a16c54ac
Content-Type: text/plain; charset=ISO-8859-1

Hello, I'm back (I've been ill, nothing serious)
I woul really like a bit more explanation with that particular approach.
Would declaring the functions I want to keep from the renamed dll in a
extern(c) block and linking that to the renamed dll while also declaring
them as export work?
And the function I want to change I declare myself and write in d?

I haven't really mastered the d language and I'm simply playing around with
it, simply checking if I've understood it.

2011/4/18 Robert Jacques <sandford jhu.edu>

 On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  The problem with that aproach would be that the functions are in another
 location in the export table.
 I've read that the locations need to stay exactly the same.
 Am I wrong about this?

I don't know for sure, but my gut would say that not knowing the exact layout of the DLL is half the point. In practice, I've used D with DLLs that have drastically added to/changed their layout (according to dumpbin) without a problem.

--00504502ccab8faa3804a16c54ac Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello, I&#39;m back (I&#39;ve been ill, nothing serious)<div>I woul really = like a bit more explanation with that particular approach.</div><div>Would = declaring the functions I want to keep from the renamed dll in a extern(c) = block and linking that to the renamed dll while also declaring them as expo= rt work?</div> <div>And the function I want to change I declare myself and write in d?</di= v><div><br></div><div>I haven&#39;t really mastered the d language and I&#3= 9;m simply playing around with it, simply checking if I&#39;ve understood i= t.</div> <div><br><div class=3D"gmail_quote">2011/4/18 Robert Jacques <span dir=3D"l= tr">&lt;<a href=3D"mailto:sandford jhu.edu">sandford jhu.edu</a>&gt;</span>= <br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-lef= t:1px #ccc solid;padding-left:1ex;"> <div class=3D"im">On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme &lt= ;<a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994= gmail.com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> The problem with that aproach would be that the functions are in another<br=

I&#39;ve read that the locations need to stay exactly the same.<br> Am I wrong about this?<br> </blockquote> <br></div> I don&#39;t know for sure, but my gut would say that not knowing the exact = layout of the DLL is half the point. In practice, I&#39;ve used D with DLLs= that have drastically added to/changed their layout (according to dumpbin)= without a problem.<br> </blockquote></div><br></div> --00504502ccab8faa3804a16c54ac--
Apr 21 2011
prev sibling next sibling parent "Robert Jacques" <sandford jhu.edu> writes:
On Thu, 21 Apr 2011 07:49:14 -0400, maarten van damme  
<maartenvd1994 gmail.com> wrote:

 Hello, I'm back (I've been ill, nothing serious)
 I woul really like a bit more explanation with that particular approach.
 Would declaring the functions I want to keep from the renamed dll in a
 extern(c) block and linking that to the renamed dll while also declaring
 them as export work?
 And the function I want to change I declare myself and write in d?

 I haven't really mastered the d language and I'm simply playing around  
 with
 it, simply checking if I've understood it.

 2011/4/18 Robert Jacques <sandford jhu.edu>

 On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  The problem with that aproach would be that the functions are in  
 another
 location in the export table.
 I've read that the locations need to stay exactly the same.
 Am I wrong about this?

I don't know for sure, but my gut would say that not knowing the exact layout of the DLL is half the point. In practice, I've used D with DLLs that have drastically added to/changed their layout (according to dumpbin) without a problem.


Hmm... It should work, but I've never tried it. Def files allow you to rename DLL functions, so you could rename the single function you want to override something else, or leave it out entirely. The only thing to be careful of is call style and name mangling (i.e. System vs C, etc.)
Apr 21 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd2118a19e14804a16ef3f3
Content-Type: text/plain; charset=ISO-8859-1

There is another problem, I don't know the return types of the functions
from that dll, so I gave them the type "void *". I think this is incorrect.
I've tried with the little knowledge I have from d and in the link is my
kernel32.d. I have compiled it succesfully in a .dll but the application
using that dll states that that dll isn't valid.

thank you for taking your time to answer this question :) .


http://dl.dropbox.com/u/15024434/kernel32.d

 2011/4/21 Robert Jacques <sandford jhu.edu>

 On Thu, 21 Apr 2011 07:49:14 -0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  Hello, I'm back (I've been ill, nothing serious)
 I woul really like a bit more explanation with that particular approach.
 Would declaring the functions I want to keep from the renamed dll in a
 extern(c) block and linking that to the renamed dll while also declaring
 them as export work?
 And the function I want to change I declare myself and write in d?

 I haven't really mastered the d language and I'm simply playing around
 with
 it, simply checking if I've understood it.

 2011/4/18 Robert Jacques <sandford jhu.edu>

  On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  The problem with that aproach would be that the functions are in
 another

 location in the export table.
 I've read that the locations need to stay exactly the same.
 Am I wrong about this?

layout of the DLL is half the point. In practice, I've used D with DLLs that have drastically added to/changed their layout (according to dumpbin) without a problem.


rename DLL functions, so you could rename the single function you want to override something else, or leave it out entirely. The only thing to be careful of is call style and name mangling (i.e. System vs C, etc.)


--000e0cd2118a19e14804a16ef3f3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div><span class=3D"Apple-style-span" style=3D"border-collapse: collapse; f= ont-family: arial, sans-serif; font-size: 13px; ">There is another problem,= I don&#39;t know the return types of the functions from that dll, so I gav= e them the type &quot;void *&quot;. I think this is incorrect.<div> I&#39;ve tried with the little knowledge I have from d and in the link is m= y kernel32.d. I have compiled it succesfully in a .dll but the application = using that dll states that that dll isn&#39;t valid.</div><div><br></div> <div>thank you for taking your time to answer this question :) .</div><div>= <br></div><div><br></div></span></div><a href=3D"http://dl.dropbox.com/u/15= 024434/kernel32.d">http://dl.dropbox.com/u/15024434/kernel32.d</a><br><div = class=3D"gmail_quote"> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div><div></div><div class=3D"h5"><div><br>= <div class=3D"gmail_quote">2011/4/21 Robert Jacques <span dir=3D"ltr">&lt;<= a href=3D"mailto:sandford jhu.edu" target=3D"_blank">sandford jhu.edu</a>&g= t;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div>On Thu, 21 Apr 2011 07:49:14 -0400, maa= rten van damme &lt;<a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_bl= ank">maartenvd1994 gmail.com</a>&gt; wrote:<br> <br> </div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l= eft:1px #ccc solid;padding-left:1ex"><div> Hello, I&#39;m back (I&#39;ve been ill, nothing serious)<br> I woul really like a bit more explanation with that particular approach.<br=

extern(c) block and linking that to the renamed dll while also declaring<br=

And the function I want to change I declare myself and write in d?<br> <br> I haven&#39;t really mastered the d language and I&#39;m simply playing aro= und with<br> it, simply checking if I&#39;ve understood it.<br> <br> 2011/4/18 Robert Jacques &lt;<a href=3D"mailto:sandford jhu.edu" target=3D"= _blank">sandford jhu.edu</a>&gt;<br> <br> </div><div><div></div><div><blockquote class=3D"gmail_quote" style=3D"margi= n:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme &lt;<br> <a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 = gmail.com</a>&gt; wrote:<br> <br> =A0The problem with that aproach would be that the functions are in another= <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> location in the export table.<br> I&#39;ve read that the locations need to stay exactly the same.<br> Am I wrong about this?<br> <br> </blockquote> <br> I don&#39;t know for sure, but my gut would say that not knowing the exact<= br> layout of the DLL is half the point. In practice, I&#39;ve used D with DLLs= that<br> have drastically added to/changed their layout (according to dumpbin)<br> without a problem.<br> <br> </blockquote></div></div></blockquote> <br> Hmm... It should work, but I&#39;ve never tried it. Def files allow you to = rename DLL functions, so you could rename the single function you want to o= verride something else, or leave it out entirely. The only thing to be care= ful of is call style and name mangling (i.e. System vs C, etc.)<br> </blockquote></div><br></div> </div></div></blockquote></div><br> --000e0cd2118a19e14804a16ef3f3--
Apr 21 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd14f989ce4cc04a170473f
Content-Type: text/plain; charset=ISO-8859-1

according to dllexp.exe (a dll examiner) my dll does not export any
functions.
So there is something wrong in my declaration:

pragma(lib,kernel33.lib);
extern(C){
export void * functionfromkernel33.lib () ;
...
}

How can one write this correctly?

2011/4/21 maarten van damme <maartenvd1994 gmail.com>

 There is another problem, I don't know the return types of the functions
 from that dll, so I gave them the type "void *". I think this is incorrect.
 I've tried with the little knowledge I have from d and in the link is my
 kernel32.d. I have compiled it succesfully in a .dll but the application
 using that dll states that that dll isn't valid.

 thank you for taking your time to answer this question :) .


 http://dl.dropbox.com/u/15024434/kernel32.d


 2011/4/21 Robert Jacques <sandford jhu.edu>

 On Thu, 21 Apr 2011 07:49:14 -0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  Hello, I'm back (I've been ill, nothing serious)
 I woul really like a bit more explanation with that particular approach.
 Would declaring the functions I want to keep from the renamed dll in a
 extern(c) block and linking that to the renamed dll while also declaring
 them as export work?
 And the function I want to change I declare myself and write in d?

 I haven't really mastered the d language and I'm simply playing around
 with
 it, simply checking if I've understood it.

 2011/4/18 Robert Jacques <sandford jhu.edu>

  On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  The problem with that aproach would be that the functions are in
 another

 location in the export table.
 I've read that the locations need to stay exactly the same.
 Am I wrong about this?

layout of the DLL is half the point. In practice, I've used D with DLLs that have drastically added to/changed their layout (according to dumpbin) without a problem.


rename DLL functions, so you could rename the single function you want to override something else, or leave it out entirely. The only thing to be careful of is call style and name mangling (i.e. System vs C, etc.)



--000e0cd14f989ce4cc04a170473f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable according to dllexp.exe (a dll examiner) my dll does not export any functio= ns.<div>So there is something wrong in my declaration:</div><div><br></div>= <div>pragma(lib,kernel33.lib);</div><div>extern(C){</div><div>export void *= functionfromkernel33.lib () ;</div> <div>...</div><div>}</div><div><br></div><div>How can one write this correc= tly?</div><div><br><div class=3D"gmail_quote">2011/4/21 maarten van damme <= span dir=3D"ltr">&lt;<a href=3D"mailto:maartenvd1994 gmail.com">maartenvd19= 94 gmail.com</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div><span style=3D"border-collapse:collaps= e;font-family:arial, sans-serif;font-size:13px"><div class=3D"im">There is = another problem, I don&#39;t know the return types of the functions from th= at dll, so I gave them the type &quot;void *&quot;. I think this is incorre= ct.</div> <div> I&#39;ve tried with the little knowledge I have from d and in the link is m= y kernel32.d. I have compiled it succesfully in a .dll but the application = using that dll states that that dll isn&#39;t valid.</div><div class=3D"im"=

<div>thank you for taking your time to answer this question :) .</div><div>= <br></div><div><br></div></div></span></div><a href=3D"http://dl.dropbox.co= m/u/15024434/kernel32.d" target=3D"_blank">http://dl.dropbox.com/u/15024434= /kernel32.d</a><div> <div></div><div class=3D"h5"><br><div class=3D"gmail_quote"> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div><div></div><div><div><br><div class=3D"= gmail_quote">2011/4/21 Robert Jacques <span dir=3D"ltr">&lt;<a href=3D"mail= to:sandford jhu.edu" target=3D"_blank">sandford jhu.edu</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div>On Thu, 21 Apr 2011 07:49:14 -0400, maa= rten van damme &lt;<a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_bl= ank">maartenvd1994 gmail.com</a>&gt; wrote:<br> <br> </div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l= eft:1px #ccc solid;padding-left:1ex"><div> Hello, I&#39;m back (I&#39;ve been ill, nothing serious)<br> I woul really like a bit more explanation with that particular approach.<br=

extern(c) block and linking that to the renamed dll while also declaring<br=

And the function I want to change I declare myself and write in d?<br> <br> I haven&#39;t really mastered the d language and I&#39;m simply playing aro= und with<br> it, simply checking if I&#39;ve understood it.<br> <br> 2011/4/18 Robert Jacques &lt;<a href=3D"mailto:sandford jhu.edu" target=3D"= _blank">sandford jhu.edu</a>&gt;<br> <br> </div><div><div></div><div><blockquote class=3D"gmail_quote" style=3D"margi= n:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> On Mon, 18 Apr 2011 04:11:16 -0400, maarten van damme &lt;<br> <a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 = gmail.com</a>&gt; wrote:<br> <br> =A0The problem with that aproach would be that the functions are in another= <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> location in the export table.<br> I&#39;ve read that the locations need to stay exactly the same.<br> Am I wrong about this?<br> <br> </blockquote> <br> I don&#39;t know for sure, but my gut would say that not knowing the exact<= br> layout of the DLL is half the point. In practice, I&#39;ve used D with DLLs= that<br> have drastically added to/changed their layout (according to dumpbin)<br> without a problem.<br> <br> </blockquote></div></div></blockquote> <br> Hmm... It should work, but I&#39;ve never tried it. Def files allow you to = rename DLL functions, so you could rename the single function you want to o= verride something else, or leave it out entirely. The only thing to be care= ful of is call style and name mangling (i.e. System vs C, etc.)<br> </blockquote></div><br></div> </div></div></blockquote></div><br> </div></div></blockquote></div><br></div> --000e0cd14f989ce4cc04a170473f--
Apr 21 2011
prev sibling next sibling parent "Robert Jacques" <sandford jhu.edu> writes:
On Thu, 21 Apr 2011 12:31:56 -0400, maarten van damme  
<maartenvd1994 gmail.com> wrote:

 according to dllexp.exe (a dll examiner) my dll does not export any
 functions.
 So there is something wrong in my declaration:

 pragma(lib,kernel33.lib);
 extern(C){
 export void * functionfromkernel33.lib () ;
 ...
 }

 How can one write this correctly?

You need a dll main function. Check out the dll example that comes with dmd (i.e. dmd2\samples\d\mydll) for the complete example.
Apr 21 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd242c4fe857004a17f3b35
Content-Type: text/plain; charset=ISO-8859-1

That example was a bit incomplete, preceding was the following code:

import std.c.windows.windows;
import core.dll_helper;

pragma(lib,"kernel33.lib");

__gshared HINSTANCE g_hInst;

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved)
{
    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;
}

--000e0cd242c4fe857004a17f3b35
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

That example was a bit incomplete, preceding was the following code:<div><b=
r></div><div><div>import std.c.windows.windows;</div><div>import core.dll_h=
elper;</div><div><br></div><div>pragma(lib,&quot;kernel33.lib&quot;);</div>
<div><br></div><div>__gshared HINSTANCE g_hInst;</div><div><br></div><div>e=
xtern (Windows)</div><div>BOOL DllMain(HINSTANCE hInstance, ULONG ulReason,=
 LPVOID pvReserved)</div><div>{</div><div>=A0=A0 =A0switch (ulReason)</div>=
<div>
=A0=A0 =A0{</div><div><span class=3D"Apple-tab-span" style=3D"white-space:p=
re">	</span>case DLL_PROCESS_ATTACH:</div><div><span class=3D"Apple-tab-spa=
n" style=3D"white-space:pre">	</span> =A0 =A0g_hInst =3D hInstance;</div><d=
iv><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span> =A0 =
=A0dll_process_attach( hInstance, true );</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span> =A0 =
=A0break;</div><div><br></div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">	</span>case DLL_PROCESS_DETACH:</div><div><span class=3D"=
Apple-tab-span" style=3D"white-space:pre">	</span> =A0 =A0dll_process_detac=
h( hInstance, true );</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span> =A0 =
=A0break;</div><div><br></div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">	</span>case DLL_THREAD_ATTACH:</div><div><span class=3D"A=
pple-tab-span" style=3D"white-space:pre">	</span> =A0 =A0dll_thread_attach(=
 true, true );</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span> =A0 =
=A0break;</div><div><br></div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">	</span>case DLL_THREAD_DETACH:</div><div><span class=3D"A=
pple-tab-span" style=3D"white-space:pre">	</span> =A0 =A0dll_thread_detach(=
 true, true );</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span> =A0 =
=A0break;</div><div>=A0=A0 =A0}</div><div>=A0=A0 =A0return true;</div><div>=
}</div><br><div class=3D"gmail_quote"><br></div></div>

--000e0cd242c4fe857004a17f3b35--
Apr 22 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--001636e0adb88220ae04a1ea98f9
Content-Type: text/plain; charset=ISO-8859-1

I'm afraid I've been a little unclear.
I've copied kernel32.dll from the windows dir, renamed it to kernel33.dll
and generated a .lib from it using implib.
Then I've created a d file with a correct dllmain(stolen from examples) and
between

system(C){
export void * exportedfunctionfromkernel33.dll();
export void * exportedfunction2fromkernel33.dll();
...
}

But it looks like you can't both declare a function from another lib and
export it at the same time.

--001636e0adb88220ae04a1ea98f9
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I&#39;m afraid I&#39;ve been a little unclear.<div>I&#39;ve copied kernel32=
.dll from the windows dir, renamed it to kernel33.dll and generated a .lib =
from it using implib.</div><div>Then I&#39;ve created a d file with a corre=
ct dllmain(stolen from examples) and between</div>
<div><br></div><div>system(C){</div><div>export void * exportedfunctionfrom=
kernel33.dll();</div><div>export void * exportedfunction2fromkernel33.dll()=
;</div><div>...</div><div>}</div><div><br></div><div>But it looks like you =
can&#39;t both declare a function from another lib and export it at the sam=
e time.</div>

--001636e0adb88220ae04a1ea98f9--
Apr 27 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--005045029bf6329d0804a21faf92
Content-Type: text/plain; charset=ISO-8859-1

I've changed this, I think I'm still kinda confused with lib files. They've
told me you can't do something with them without a .di file
So I went ahead and made a kernel33.di file. I now import it in kernel32.d
and my declaration is
System(C){
export void * exportedfunctionblablabal(){
   return exportedfunctionblablablal();
}
...
}

The file in the directory are:
kernel32.d : http://dl.dropbox.com/u/15024434/d/kernel32.d
kernel33.di : http://dl.dropbox.com/u/15024434/d/kernel33.di
kernel33.lib : http://dl.dropbox.com/u/15024434/d/kernel33.lib
kernel33.dll : http://dl.dropbox.com/u/15024434/d/kernel33.dll

I've tried to compile using dmd -d kernel32.d kernel33.di kernel33.lib but
it throws errors like
"Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
I have literally no clue why this is the case, can someone help me out or
look at the files?

2011/4/27 maarten van damme <maartenvd1994 gmail.com>

 I'm afraid I've been a little unclear.
 I've copied kernel32.dll from the windows dir, renamed it to kernel33.dll
 and generated a .lib from it using implib.
 Then I've created a d file with a correct dllmain(stolen from examples) and
 between

 system(C){
 export void * exportedfunctionfromkernel33.dll();
 export void * exportedfunction2fromkernel33.dll();
 ...
 }

 But it looks like you can't both declare a function from another lib and
 export it at the same time.

--005045029bf6329d0804a21faf92 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I&#39;ve changed this, I think I&#39;m still kinda confused with lib files.= They&#39;ve told me you can&#39;t do something with them without a .di fil= e<div>So I went ahead and made a kernel33.di file. I now import it in kerne= l32.d and my declaration is</div> <div>System(C){</div><div>export void * exportedfunctionblablabal(){</div><= div>=A0=A0 return exportedfunctionblablablal();</div><div>}</div><div>...</= div><div>}</div><div><br></div><div>The file in the directory are:</div><di= v> kernel32.d :=A0<a href=3D"http://dl.dropbox.com/u/15024434/d/kernel32.d">ht= tp://dl.dropbox.com/u/15024434/d/kernel32.d</a></div><div>kernel33.di :=A0<= a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.di">http://dl.dropbox= .com/u/15024434/d/kernel33.di</a></div> <div>kernel33.lib :=A0<a href=3D"http://dl.dropbox.com/u/15024434/d/kernel3= 3.lib">http://dl.dropbox.com/u/15024434/d/kernel33.lib</a></div><div>kernel= 33.dll :=A0<a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.dll">http= ://dl.dropbox.com/u/15024434/d/kernel33.dll</a></div> <div><br></div><div>I&#39;ve tried to compile using dmd -d kernel32.d kerne= l33.di kernel33.lib but it throws errors like</div><div>&quot;Error 42: Sym= bol undifined _Dkernel1336_hreadfzpV&quot;</div><div>I have literally no cl= ue why this is the case, can someone help me out or look at the files?</div=

=3D"ltr">&lt;<a href=3D"mailto:maartenvd1994 gmail.com">maartenvd1994 gmail= .com</a>&gt;</span><br><blockquote class=3D"gmail_quote" style=3D"margin:0 = 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> I&#39;m afraid I&#39;ve been a little unclear.<div>I&#39;ve copied kernel32= .dll from the windows dir, renamed it to kernel33.dll and generated a .lib = from it using implib.</div><div>Then I&#39;ve created a d file with a corre= ct dllmain(stolen from examples) and between</div> <div><br></div><div>system(C){</div><div>export void * exportedfunctionfrom= kernel33.dll();</div><div>export void * exportedfunction2fromkernel33.dll()= ;</div><div>...</div><div>}</div><div><br></div><div>But it looks like you = can&#39;t both declare a function from another lib and export it at the sam= e time.</div> </blockquote></div><br></div> --005045029bf6329d0804a21faf92--
Apr 30 2011
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme  
<maartenvd1994 gmail.com> wrote:

 I've changed this, I think I'm still kinda confused with lib files.  
 They've
 told me you can't do something with them without a .di file
 So I went ahead and made a kernel33.di file. I now import it in  
 kernel32.d
 and my declaration is
 System(C){
 export void * exportedfunctionblablabal(){
    return exportedfunctionblablablal();
 }
 ...
 }

 The file in the directory are:
 kernel32.d : http://dl.dropbox.com/u/15024434/d/kernel32.d
 kernel33.di : http://dl.dropbox.com/u/15024434/d/kernel33.di
 kernel33.lib : http://dl.dropbox.com/u/15024434/d/kernel33.lib
 kernel33.dll : http://dl.dropbox.com/u/15024434/d/kernel33.dll

 I've tried to compile using dmd -d kernel32.d kernel33.di kernel33.lib  
 but
 it throws errors like
 "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
 I have literally no clue why this is the case, can someone help me out or
 look at the files?

 2011/4/27 maarten van damme <maartenvd1994 gmail.com>

 I'm afraid I've been a little unclear.
 I've copied kernel32.dll from the windows dir, renamed it to  
 kernel33.dll
 and generated a .lib from it using implib.
 Then I've created a d file with a correct dllmain(stolen from examples)  
 and
 between

 system(C){
 export void * exportedfunctionfromkernel33.dll();
 export void * exportedfunction2fromkernel33.dll();
 ...
 }

 But it looks like you can't both declare a function from another lib and
 export it at the same time.


In your kernel33.di, try making it extern (C) export void* _hread(); etc. You functions get D mangling otherwise. I'd also suggest you to start with a less complex example, e.g. export only one function, make sure it works, then add the rest. If you think your .lib files doesn't do its job, try using .def file instead. I find them extremely helpful, and they are a lot easier to edit/extend. Hope that helps.
Apr 30 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--00151758865227e76704a2333e4e
Content-Type: text/plain; charset=ISO-8859-1

Wow, thanks for the help
The first thing I did was in the .di file adding extern(windows){ ... }
and now compiling doesn't give errors and when examining with dllexp I can
see that it exports the same functions as the real kernel32.dll :D

Now I'm going to implement all other suggested changes, thanks a lot

2011/4/30 Rainer Schuetze <r.sagitario gmx.de>

 I'm not sure your wrapping will work with kernel32.dll, but in general here
 are a few tips:

 - most functions in the windows API use the __stdcall calling convention in
 C/C++, which translates to D as "extern(Windows)"

 - this will usually add the number of bytes passed on the stack as a " NN"
 postfix to the function name. This postfix does not exist in kernel32.dll,
 but in the import library kernel32.lib that you find in the dmd lib folder.
 Maybe you can use the standard import library, or use the translation shown
 below.

 - as the exported function and the function you want to chain to have
 identical names, you have to change at least one of these and modify them in
 some build step. I'd suggest to do this in the def file:

 The symbols in the d-source file containing:

 ----
 extern(Windows) HANDLE imported_GetCurrentProcess();

 export extern(Windows) HANDLE internal_GetCurrentProcess()
 {
  return imported_GetCurrentProcess();
 }
 ----

 can be mapped to other symbols in the def file:

 ----
 EXPORTS
  GetCurrentProcess = internal_GetCurrentProcess

 IMPORTS
  imported_GetCurrentProcess = kernel33.GetCurrentProcess
 ----

 - if you don't know the number of arguments, you should not call the
 wrapped function, as this will change the callstack. Instead, you should
 just jump to it:

 void internal_hread()
 {
  asm
  {
    naked;
    jmp imported_hread;
  }
 }

 I haven't tried all that, though, so there might be some mistakes...

 Rainer



 Denis Koroskin wrote:

 On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  I've changed this, I think I'm still kinda confused with lib files.
 They've
 told me you can't do something with them without a .di file
 So I went ahead and made a kernel33.di file. I now import it in
 kernel32.d
 and my declaration is
 System(C){
 export void * exportedfunctionblablabal(){
   return exportedfunctionblablablal();
 }
 ...
 }

 The file in the directory are:
 kernel32.d : http://dl.dropbox.com/u/15024434/d/kernel32.d
 kernel33.di : http://dl.dropbox.com/u/15024434/d/kernel33.di
 kernel33.lib : http://dl.dropbox.com/u/15024434/d/kernel33.lib
 kernel33.dll : http://dl.dropbox.com/u/15024434/d/kernel33.dll

 I've tried to compile using dmd -d kernel32.d kernel33.di kernel33.lib
 but
 it throws errors like
 "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
 I have literally no clue why this is the case, can someone help me out or
 look at the files?

 2011/4/27 maarten van damme <maartenvd1994 gmail.com>

  I'm afraid I've been a little unclear.
 I've copied kernel32.dll from the windows dir, renamed it to
 kernel33.dll
 and generated a .lib from it using implib.
 Then I've created a d file with a correct dllmain(stolen from examples)
 and
 between

 system(C){
 export void * exportedfunctionfromkernel33.dll();
 export void * exportedfunction2fromkernel33.dll();
 ...
 }

 But it looks like you can't both declare a function from another lib and
 export it at the same time.


You functions get D mangling otherwise. I'd also suggest you to start with a less complex example, e.g. export only one function, make sure it works, then add the rest. If you think your .lib files doesn't do its job, try using .def file instead. I find them extremely helpful, and they are a lot easier to edit/extend. Hope that helps.


--00151758865227e76704a2333e4e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Wow, thanks for the help<div>The first thing I did was in the .di file addi= ng extern(windows){ ... }</div><div>and now compiling doesn&#39;t give erro= rs and when examining with dllexp I can see that it exports the same functi= ons as the real kernel32.dll :D</div> <div><br></div><div>Now I&#39;m going to implement all other suggested chan= ges, thanks a lot<br><br><div class=3D"gmail_quote">2011/4/30 Rainer Schuet= ze <span dir=3D"ltr">&lt;<a href=3D"mailto:r.sagitario gmx.de">r.sagitario = gmx.de</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;">I&#39;m not sure your wrapping will work wi= th kernel32.dll, but in general here are a few tips:<br> <br> - most functions in the windows API use the __stdcall calling convention in= C/C++, which translates to D as &quot;extern(Windows)&quot;<br> <br> - this will usually add the number of bytes passed on the stack as a &quot;= NN&quot; postfix to the function name. This postfix does not exist in kern= el32.dll, but in the import library kernel32.lib that you find in the dmd l= ib folder. Maybe you can use the standard import library, or use the transl= ation shown below.<br> <br> - as the exported function and the function you want to chain to have ident= ical names, you have to change at least one of these and modify them in som= e build step. I&#39;d suggest to do this in the def file:<br> <br> The symbols in the d-source file containing:<br> <br> ----<br> extern(Windows) HANDLE imported_GetCurrentProcess();<br> <br> export extern(Windows) HANDLE internal_GetCurrentProcess()<br> {<br> =A0return imported_GetCurrentProcess();<br> }<br> ----<br> <br> can be mapped to other symbols in the def file:<br> <br> ----<br> EXPORTS<br> =A0GetCurrentProcess =3D internal_GetCurrentProcess<br> <br> IMPORTS<br> =A0imported_GetCurrentProcess =3D kernel33.GetCurrentProcess<br> ----<br> <br> - if you don&#39;t know the number of arguments, you should not call the wr= apped function, as this will change the callstack. Instead, you should just= jump to it:<br> <br> void internal_hread()<br> {<br> =A0asm<br> =A0{<br> =A0 =A0naked;<br> =A0 =A0jmp imported_hread;<br> =A0}<br> }<br> <br> I haven&#39;t tried all that, though, so there might be some mistakes...<br=
<font color=3D"#888888">

Rainer</font><div><div></div><div class=3D"h5"><br> <br> <br> Denis Koroskin wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme &lt;<a href=3D"mailto= :maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a>&gt;= wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I&#39;ve changed this, I think I&#39;m still kinda confused with lib files.= They&#39;ve<br> told me you can&#39;t do something with them without a .di file<br> So I went ahead and made a kernel33.di file. I now import it in kernel32.d<= br> and my declaration is<br> System(C){<br> export void * exportedfunctionblablabal(){<br> =A0 return exportedfunctionblablablal();<br> }<br> ...<br> }<br> <br> The file in the directory are:<br> kernel32.d : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel32.d" targ= et=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel32.d</a><br> kernel33.di : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.di" ta= rget=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.di</a><br> kernel33.lib : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.lib" = target=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.lib</a><br> kernel33.dll : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.dll" = target=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.dll</a><br> <br> I&#39;ve tried to compile using dmd -d kernel32.d kernel33.di kernel33.lib = but<br> it throws errors like<br> &quot;Error 42: Symbol undifined _Dkernel1336_hreadfzpV&quot;<br> I have literally no clue why this is the case, can someone help me out or<b= r> look at the files?<br> <br> 2011/4/27 maarten van damme &lt;<a href=3D"mailto:maartenvd1994 gmail.com" = target=3D"_blank">maartenvd1994 gmail.com</a>&gt;<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I&#39;m afraid I&#39;ve been a little unclear.<br> I&#39;ve copied kernel32.dll from the windows dir, renamed it to kernel33.d= ll<br> and generated a .lib from it using implib.<br> Then I&#39;ve created a d file with a correct dllmain(stolen from examples)= and<br> between<br> <br> system(C){<br> export void * exportedfunctionfromkernel33.dll();<br> export void * exportedfunction2fromkernel33.dll();<br> ...<br> }<br> <br> But it looks like you can&#39;t both declare a function from another lib an= d<br> export it at the same time.<br> <br> </blockquote></blockquote> <br> In your kernel33.di, try making it extern (C) export void* _hread(); etc. Y= ou functions get D mangling otherwise.<br> <br> I&#39;d also suggest you to start with a less complex example, e.g. export = only one function, make sure it works, then add the rest.<br> <br> If you think your .lib files doesn&#39;t do its job, try using .def file in= stead. I find them extremely helpful, and they are a lot easier to edit/ext= end.<br> <br> Hope that helps.<br> </blockquote> </div></div></blockquote></div><br></div> --00151758865227e76704a2333e4e--
May 01 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0ce0aa8e75e63104a236e13b
Content-Type: text/plain; charset=ISO-8859-1

Number overflow?
So I implemented the suggested changes (you can check them out at
http://dl.dropbox.com/u/15024434/version2.zip)
But now I get when I compile it :
"kernel32.def(738) : Error 12: Number Overflow: (strange symbol over here)"

I do agree I should've picked a simpler example but I think the
statisfaction will be even bigger if I were to succeed :p

2011/5/1 maarten van damme <maartenvd1994 gmail.com>

 Wow, thanks for the help
 The first thing I did was in the .di file adding extern(windows){ ... }
 and now compiling doesn't give errors and when examining with dllexp I can
 see that it exports the same functions as the real kernel32.dll :D

 Now I'm going to implement all other suggested changes, thanks a lot


 2011/4/30 Rainer Schuetze <r.sagitario gmx.de>

 I'm not sure your wrapping will work with kernel32.dll, but in general
 here are a few tips:

 - most functions in the windows API use the __stdcall calling convention
 in C/C++, which translates to D as "extern(Windows)"

 - this will usually add the number of bytes passed on the stack as a " NN"
 postfix to the function name. This postfix does not exist in kernel32.dll,
 but in the import library kernel32.lib that you find in the dmd lib folder.
 Maybe you can use the standard import library, or use the translation shown
 below.

 - as the exported function and the function you want to chain to have
 identical names, you have to change at least one of these and modify them in
 some build step. I'd suggest to do this in the def file:

 The symbols in the d-source file containing:

 ----
 extern(Windows) HANDLE imported_GetCurrentProcess();

 export extern(Windows) HANDLE internal_GetCurrentProcess()
 {
  return imported_GetCurrentProcess();
 }
 ----

 can be mapped to other symbols in the def file:

 ----
 EXPORTS
  GetCurrentProcess = internal_GetCurrentProcess

 IMPORTS
  imported_GetCurrentProcess = kernel33.GetCurrentProcess
 ----

 - if you don't know the number of arguments, you should not call the
 wrapped function, as this will change the callstack. Instead, you should
 just jump to it:

 void internal_hread()
 {
  asm
  {
    naked;
    jmp imported_hread;
  }
 }

 I haven't tried all that, though, so there might be some mistakes...

 Rainer



 Denis Koroskin wrote:

 On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme <
 maartenvd1994 gmail.com> wrote:

  I've changed this, I think I'm still kinda confused with lib files.
 They've
 told me you can't do something with them without a .di file
 So I went ahead and made a kernel33.di file. I now import it in
 kernel32.d
 and my declaration is
 System(C){
 export void * exportedfunctionblablabal(){
   return exportedfunctionblablablal();
 }
 ...
 }

 The file in the directory are:
 kernel32.d : http://dl.dropbox.com/u/15024434/d/kernel32.d
 kernel33.di : http://dl.dropbox.com/u/15024434/d/kernel33.di
 kernel33.lib : http://dl.dropbox.com/u/15024434/d/kernel33.lib
 kernel33.dll : http://dl.dropbox.com/u/15024434/d/kernel33.dll

 I've tried to compile using dmd -d kernel32.d kernel33.di kernel33.lib
 but
 it throws errors like
 "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
 I have literally no clue why this is the case, can someone help me out
 or
 look at the files?

 2011/4/27 maarten van damme <maartenvd1994 gmail.com>

  I'm afraid I've been a little unclear.
 I've copied kernel32.dll from the windows dir, renamed it to
 kernel33.dll
 and generated a .lib from it using implib.
 Then I've created a d file with a correct dllmain(stolen from examples)
 and
 between

 system(C){
 export void * exportedfunctionfromkernel33.dll();
 export void * exportedfunction2fromkernel33.dll();
 ...
 }

 But it looks like you can't both declare a function from another lib
 and
 export it at the same time.


You functions get D mangling otherwise. I'd also suggest you to start with a less complex example, e.g. export only one function, make sure it works, then add the rest. If you think your .lib files doesn't do its job, try using .def file instead. I find them extremely helpful, and they are a lot easier to edit/extend. Hope that helps.



--000e0ce0aa8e75e63104a236e13b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Number overflow?<div>So I implemented the suggested changes (you can check = them out at=A0<a href=3D"http://dl.dropbox.com/u/15024434/version2.zip">htt= p://dl.dropbox.com/u/15024434/version2.zip</a>)</div><div>But now I get whe= n I compile it :=A0<br> &quot;kernel32.def(738) : Error 12: Number Overflow: (strange symbol over h= ere)&quot;</div><div><br></div><div>I do agree I should&#39;ve picked a sim= pler example but I think the statisfaction will be even bigger if I were to= succeed :p</div> <div><br><div class=3D"gmail_quote">2011/5/1 maarten van damme <span dir=3D= "ltr">&lt;<a href=3D"mailto:maartenvd1994 gmail.com">maartenvd1994 gmail.co= m</a>&gt;</span><br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0= .8ex;border-left:1px #ccc solid;padding-left:1ex;"> Wow, thanks for the help<div>The first thing I did was in the .di file addi= ng extern(windows){ ... }</div><div>and now compiling doesn&#39;t give erro= rs and when examining with dllexp I can see that it exports the same functi= ons as the real kernel32.dll :D</div> <div><br></div><div>Now I&#39;m going to implement all other suggested chan= ges, thanks a lot<div><div></div><div class=3D"h5"><br><br><div class=3D"gm= ail_quote">2011/4/30 Rainer Schuetze <span dir=3D"ltr">&lt;<a href=3D"mailt= o:r.sagitario gmx.de" target=3D"_blank">r.sagitario gmx.de</a>&gt;</span><b= r> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex">I&#39;m not sure your wrapping will work wit= h kernel32.dll, but in general here are a few tips:<br> <br> - most functions in the windows API use the __stdcall calling convention in= C/C++, which translates to D as &quot;extern(Windows)&quot;<br> <br> - this will usually add the number of bytes passed on the stack as a &quot;= NN&quot; postfix to the function name. This postfix does not exist in kern= el32.dll, but in the import library kernel32.lib that you find in the dmd l= ib folder. Maybe you can use the standard import library, or use the transl= ation shown below.<br> <br> - as the exported function and the function you want to chain to have ident= ical names, you have to change at least one of these and modify them in som= e build step. I&#39;d suggest to do this in the def file:<br> <br> The symbols in the d-source file containing:<br> <br> ----<br> extern(Windows) HANDLE imported_GetCurrentProcess();<br> <br> export extern(Windows) HANDLE internal_GetCurrentProcess()<br> {<br> =A0return imported_GetCurrentProcess();<br> }<br> ----<br> <br> can be mapped to other symbols in the def file:<br> <br> ----<br> EXPORTS<br> =A0GetCurrentProcess =3D internal_GetCurrentProcess<br> <br> IMPORTS<br> =A0imported_GetCurrentProcess =3D kernel33.GetCurrentProcess<br> ----<br> <br> - if you don&#39;t know the number of arguments, you should not call the wr= apped function, as this will change the callstack. Instead, you should just= jump to it:<br> <br> void internal_hread()<br> {<br> =A0asm<br> =A0{<br> =A0 =A0naked;<br> =A0 =A0jmp imported_hread;<br> =A0}<br> }<br> <br> I haven&#39;t tried all that, though, so there might be some mistakes...<br=
<font color=3D"#888888">

Rainer</font><div><div></div><div><br> <br> <br> Denis Koroskin wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme &lt;<a href=3D"mailto= :maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a>&gt;= wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I&#39;ve changed this, I think I&#39;m still kinda confused with lib files.= They&#39;ve<br> told me you can&#39;t do something with them without a .di file<br> So I went ahead and made a kernel33.di file. I now import it in kernel32.d<= br> and my declaration is<br> System(C){<br> export void * exportedfunctionblablabal(){<br> =A0 return exportedfunctionblablablal();<br> }<br> ...<br> }<br> <br> The file in the directory are:<br> kernel32.d : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel32.d" targ= et=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel32.d</a><br> kernel33.di : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.di" ta= rget=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.di</a><br> kernel33.lib : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.lib" = target=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.lib</a><br> kernel33.dll : <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.dll" = target=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.dll</a><br> <br> I&#39;ve tried to compile using dmd -d kernel32.d kernel33.di kernel33.lib = but<br> it throws errors like<br> &quot;Error 42: Symbol undifined _Dkernel1336_hreadfzpV&quot;<br> I have literally no clue why this is the case, can someone help me out or<b= r> look at the files?<br> <br> 2011/4/27 maarten van damme &lt;<a href=3D"mailto:maartenvd1994 gmail.com" = target=3D"_blank">maartenvd1994 gmail.com</a>&gt;<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I&#39;m afraid I&#39;ve been a little unclear.<br> I&#39;ve copied kernel32.dll from the windows dir, renamed it to kernel33.d= ll<br> and generated a .lib from it using implib.<br> Then I&#39;ve created a d file with a correct dllmain(stolen from examples)= and<br> between<br> <br> system(C){<br> export void * exportedfunctionfromkernel33.dll();<br> export void * exportedfunction2fromkernel33.dll();<br> ...<br> }<br> <br> But it looks like you can&#39;t both declare a function from another lib an= d<br> export it at the same time.<br> <br> </blockquote></blockquote> <br> In your kernel33.di, try making it extern (C) export void* _hread(); etc. Y= ou functions get D mangling otherwise.<br> <br> I&#39;d also suggest you to start with a less complex example, e.g. export = only one function, make sure it works, then add the rest.<br> <br> If you think your .lib files doesn&#39;t do its job, try using .def file in= stead. I find them extremely helpful, and they are a lot easier to edit/ext= end.<br> <br> Hope that helps.<br> </blockquote> </div></div></blockquote></div><br></div></div></div> </blockquote></div><br></div> --000e0ce0aa8e75e63104a236e13b--
May 01 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd2586cfa1ddd04a23aaeb8
Content-Type: text/plain; charset=ISO-8859-1

Great, now the error in kernel32.def is resolved but it gets the same
problem in kernel33.def.
here is the start of the exports from kernel33.def:
EXPORTS
_hread  1334
how can I change this to resolve that?

2011/5/1 Rainer Schuetze <r.sagitario gmx.de>

 It seems you have hit another of those dreaded optlink bugs.

 With less symbols, it works if you declare the imports like this (because
 of the described name mangling):

 IMPORTS
        _imported_hread 0 =  kernel33._hread

 2 more notes:
 - you don't need to import kernel33.di
 - you should not use "SINGLE" in the DATA statement of the def file, it
 will share the memory across processes.



 maarten van damme wrote:

 Number overflow?
 So I implemented the suggested changes (you can check them out at
 http://dl.dropbox.com/u/15024434/version2.zip)

 But now I get when I compile it : "kernel32.def(738) : Error 12: Number
 Overflow: (strange symbol over here)"

 I do agree I should've picked a simpler example but I think the
 statisfaction will be even bigger if I were to succeed :p

 2011/5/1 maarten van damme <maartenvd1994 gmail.com <mailto:
 maartenvd1994 gmail.com>>


    Wow, thanks for the help
    The first thing I did was in the .di file adding extern(windows){ ... }
    and now compiling doesn't give errors and when examining with dllexp
    I can see that it exports the same functions as the real kernel32.dll
 :D

    Now I'm going to implement all other suggested changes, thanks a lot


    2011/4/30 Rainer Schuetze <r.sagitario gmx.de
    <mailto:r.sagitario gmx.de>>


        I'm not sure your wrapping will work with kernel32.dll, but in
        general here are a few tips:

        - most functions in the windows API use the __stdcall calling
        convention in C/C++, which translates to D as "extern(Windows)"

        - this will usually add the number of bytes passed on the stack
        as a " NN" postfix to the function name. This postfix does not
        exist in kernel32.dll, but in the import library kernel32.lib
        that you find in the dmd lib folder. Maybe you can use the
        standard import library, or use the translation shown below.

        - as the exported function and the function you want to chain to
        have identical names, you have to change at least one of these
        and modify them in some build step. I'd suggest to do this in
        the def file:

        The symbols in the d-source file containing:

        ----
        extern(Windows) HANDLE imported_GetCurrentProcess();

        export extern(Windows) HANDLE internal_GetCurrentProcess()
        {
         return imported_GetCurrentProcess();
        }
        ----

        can be mapped to other symbols in the def file:

        ----
        EXPORTS
         GetCurrentProcess = internal_GetCurrentProcess

        IMPORTS
         imported_GetCurrentProcess = kernel33.GetCurrentProcess
        ----

        - if you don't know the number of arguments, you should not call
        the wrapped function, as this will change the callstack.
        Instead, you should just jump to it:

        void internal_hread()
        {
         asm
         {
           naked;
           jmp imported_hread;
         }
        }

        I haven't tried all that, though, so there might be some
 mistakes...

        Rainer



        Denis Koroskin wrote:

            On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme
            <maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>>

            wrote:

                I've changed this, I think I'm still kinda confused with
                lib files. They've
                told me you can't do something with them without a .di file
                So I went ahead and made a kernel33.di file. I now
                import it in kernel32.d
                and my declaration is
                System(C){
                export void * exportedfunctionblablabal(){
                  return exportedfunctionblablablal();
                }
                ....
                }

                The file in the directory are:
                kernel32.d : http://dl.dropbox.com/u/15024434/d/kernel32.d
                kernel33.di :
 http://dl.dropbox.com/u/15024434/d/kernel33.di
                kernel33.lib :
                http://dl.dropbox.com/u/15024434/d/kernel33.lib
                kernel33.dll :
                http://dl.dropbox.com/u/15024434/d/kernel33.dll

                I've tried to compile using dmd -d kernel32.d
                kernel33.di kernel33.lib but
                it throws errors like
                "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
                I have literally no clue why this is the case, can
                someone help me out or
                look at the files?

                2011/4/27 maarten van damme <maartenvd1994 gmail.com
                <mailto:maartenvd1994 gmail.com>>


                    I'm afraid I've been a little unclear.
                    I've copied kernel32.dll from the windows dir,
                    renamed it to kernel33.dll
                    and generated a .lib from it using implib.
                    Then I've created a d file with a correct
                    dllmain(stolen from examples) and
                    between

                    system(C){
                    export void * exportedfunctionfromkernel33.dll();
                    export void * exportedfunction2fromkernel33.dll();
                    ....
                    }

                    But it looks like you can't both declare a function
                    from another lib and
                    export it at the same time.


            In your kernel33.di, try making it extern (C) export void*
            _hread(); etc. You functions get D mangling otherwise.

            I'd also suggest you to start with a less complex example,
            e.g. export only one function, make sure it works, then add
            the rest.

            If you think your .lib files doesn't do its job, try using
            .def file instead. I find them extremely helpful, and they
            are a lot easier to edit/extend.

            Hope that helps.


--000e0cd2586cfa1ddd04a23aaeb8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Great, now the error in kernel32.def is resolved but it gets the same probl= em in kernel33.def.<div>here is the start of the exports from kernel33.def:= </div><div><div>EXPORTS<span class=3D"Apple-tab-span" style=3D"white-space:= pre"> </span></div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>_hrea= d 1334</div><div>how can I change this to resolve that?</div><br><div clas= s=3D"gmail_quote">2011/5/1 Rainer Schuetze <span dir=3D"ltr">&lt;<a href=3D= "mailto:r.sagitario gmx.de">r.sagitario gmx.de</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;">It seems you have hit another of those drea= ded optlink bugs.<br> <br> With less symbols, it works if you declare the imports like this (because o= f the described name mangling):<br> <br> IMPORTS <br> =A0 =A0 =A0 =A0_imported_hread 0 =3D =A0kernel33._hread<br> <br> 2 more notes:<br> - you don&#39;t need to import kernel33.di<br> - you should not use &quot;SINGLE&quot; in the DATA statement of the def fi= le, it will share the memory across processes.<br> <br> <br> <br> maarten van damme wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Number overflow?<br> So I implemented the suggested changes (you can check them out at <a href= =3D"http://dl.dropbox.com/u/15024434/version2.zip" target=3D"_blank">http:/= /dl.dropbox.com/u/15024434/version2.zip</a>)<div class=3D"im"><br> But now I get when I compile it : &quot;kernel32.def(738) : Error 12: Numbe= r Overflow: (strange symbol over here)&quot;<br> <br> I do agree I should&#39;ve picked a simpler example but I think the statisf= action will be even bigger if I were to succeed :p<br> <br></div> 2011/5/1 maarten van damme &lt;<a href=3D"mailto:maartenvd1994 gmail.com" t= arget=3D"_blank">maartenvd1994 gmail.com</a> &lt;mailto:<a href=3D"mailto:m= aartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a>&gt;&g= t;<div class=3D"im"> <br> <br> =A0 =A0Wow, thanks for the help<br> =A0 =A0The first thing I did was in the .di file adding extern(windows){ .= .. }<br> =A0 =A0and now compiling doesn&#39;t give errors and when examining with d= llexp<br> =A0 =A0I can see that it exports the same functions as the real kernel32.d= ll :D<br> <br> =A0 =A0Now I&#39;m going to implement all other suggested changes, thanks = a lot<br> <br> <br> =A0 =A02011/4/30 Rainer Schuetze &lt;<a href=3D"mailto:r.sagitario gmx.de"= target=3D"_blank">r.sagitario gmx.de</a><br></div> =A0 =A0&lt;mailto:<a href=3D"mailto:r.sagitario gmx.de" target=3D"_blank">= r.sagitario gmx.de</a>&gt;&gt;<div><div></div><div class=3D"h5"><br> <br> =A0 =A0 =A0 =A0I&#39;m not sure your wrapping will work with kernel32.dll,= but in<br> =A0 =A0 =A0 =A0general here are a few tips:<br> <br> =A0 =A0 =A0 =A0- most functions in the windows API use the __stdcall calli= ng<br> =A0 =A0 =A0 =A0convention in C/C++, which translates to D as &quot;extern(= Windows)&quot;<br> <br> =A0 =A0 =A0 =A0- this will usually add the number of bytes passed on the s= tack<br> =A0 =A0 =A0 =A0as a &quot; NN&quot; postfix to the function name. This pos= tfix does not<br> =A0 =A0 =A0 =A0exist in kernel32.dll, but in the import library kernel32.l= ib<br> =A0 =A0 =A0 =A0that you find in the dmd lib folder. Maybe you can use the<= br> =A0 =A0 =A0 =A0standard import library, or use the translation shown below= .<br> <br> =A0 =A0 =A0 =A0- as the exported function and the function you want to cha= in to<br> =A0 =A0 =A0 =A0have identical names, you have to change at least one of th= ese<br> =A0 =A0 =A0 =A0and modify them in some build step. I&#39;d suggest to do t= his in<br> =A0 =A0 =A0 =A0the def file:<br> <br> =A0 =A0 =A0 =A0The symbols in the d-source file containing:<br> <br> =A0 =A0 =A0 =A0----<br> =A0 =A0 =A0 =A0extern(Windows) HANDLE imported_GetCurrentProcess();<br> <br> =A0 =A0 =A0 =A0export extern(Windows) HANDLE internal_GetCurrentProcess()<= br> =A0 =A0 =A0 =A0{<br> =A0 =A0 =A0 =A0 return imported_GetCurrentProcess();<br> =A0 =A0 =A0 =A0}<br> =A0 =A0 =A0 =A0----<br> <br> =A0 =A0 =A0 =A0can be mapped to other symbols in the def file:<br> <br> =A0 =A0 =A0 =A0----<br> =A0 =A0 =A0 =A0EXPORTS<br> =A0 =A0 =A0 =A0 GetCurrentProcess =3D internal_GetCurrentProcess<br> <br> =A0 =A0 =A0 =A0IMPORTS<br> =A0 =A0 =A0 =A0 imported_GetCurrentProcess =3D kernel33.GetCurrentProcess<= br> =A0 =A0 =A0 =A0----<br> <br> =A0 =A0 =A0 =A0- if you don&#39;t know the number of arguments, you should= not call<br> =A0 =A0 =A0 =A0the wrapped function, as this will change the callstack.<br=

<br> =A0 =A0 =A0 =A0void internal_hread()<br> =A0 =A0 =A0 =A0{<br> =A0 =A0 =A0 =A0 asm<br> =A0 =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 naked;<br> =A0 =A0 =A0 =A0 =A0 jmp imported_hread;<br> =A0 =A0 =A0 =A0 }<br> =A0 =A0 =A0 =A0}<br> <br> =A0 =A0 =A0 =A0I haven&#39;t tried all that, though, so there might be som= e mistakes...<br> <br> =A0 =A0 =A0 =A0Rainer<br> <br> <br> <br> =A0 =A0 =A0 =A0Denis Koroskin wrote:<br> <br> =A0 =A0 =A0 =A0 =A0 =A0On Sat, 30 Apr 2011 13:47:53 +0400, maarten van dam= me<br></div></div> =A0 =A0 =A0 =A0 =A0 =A0&lt;<a href=3D"mailto:maartenvd1994 gmail.com" targ= et=3D"_blank">maartenvd1994 gmail.com</a> &lt;mailto:<a href=3D"mailto:maar= tenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a>&gt;&gt;<= div><div></div> <div class=3D"h5"><br> =A0 =A0 =A0 =A0 =A0 =A0wrote:<br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0I&#39;ve changed this, I think I&#39;m stil= l kinda confused with<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0lib files. They&#39;ve<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0told me you can&#39;t do something with the= m without a .di file<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0So I went ahead and made a kernel33.di file= . I now<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0import it in kernel32.d<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0and my declaration is<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0System(C){<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0export void * exportedfunctionblablabal(){<= br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return exportedfunctionblablablal();<br=

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0....<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}<br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0The file in the directory are:<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0kernel32.d : <a href=3D"http://dl.dropbox.c= om/u/15024434/d/kernel32.d" target=3D"_blank">http://dl.dropbox.com/u/15024= 434/d/kernel32.d</a><br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0kernel33.di : <a href=3D"http://dl.dropbox.= com/u/15024434/d/kernel33.di" target=3D"_blank">http://dl.dropbox.com/u/150= 24434/d/kernel33.di</a><br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0kernel33.lib :<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<a href=3D"http://dl.dropbox.com/u/15024434= /d/kernel33.lib" target=3D"_blank">http://dl.dropbox.com/u/15024434/d/kerne= l33.lib</a><br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0kernel33.dll :<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<a href=3D"http://dl.dropbox.com/u/15024434= /d/kernel33.dll" target=3D"_blank">http://dl.dropbox.com/u/15024434/d/kerne= l33.dll</a><br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0I&#39;ve tried to compile using dmd -d kern= el32.d<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0kernel33.di kernel33.lib but<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0it throws errors like<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0&quot;Error 42: Symbol undifined _Dkernel13= 36_hreadfzpV&quot;<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0I have literally no clue why this is the ca= se, can<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0someone help me out or<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0look at the files?<br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A02011/4/27 maarten van damme &lt;<a href=3D"= mailto:maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</= a><br></div></div> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0&lt;mailto:<a href=3D"mailto:maartenvd1994 = gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a>&gt;&gt;<div class= =3D"im"><br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0I&#39;m afraid I&#39;ve been a litt= le unclear.<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0I&#39;ve copied kernel32.dll from t= he windows dir,<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0renamed it to kernel33.dll<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0and generated a .lib from it using = implib.<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Then I&#39;ve created a d file with= a correct<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0dllmain(stolen from examples) and<b= r> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0between<br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0system(C){<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0export void * exportedfunctionfromk= ernel33.dll();<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0export void * exportedfunction2from= kernel33.dll();<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0....<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}<br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0But it looks like you can&#39;t bot= h declare a function<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0from another lib and<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0export it at the same time.<br> <br> <br> =A0 =A0 =A0 =A0 =A0 =A0In your kernel33.di, try making it extern (C) expor= t void*<br> =A0 =A0 =A0 =A0 =A0 =A0_hread(); etc. You functions get D mangling otherwi= se.<br> <br> =A0 =A0 =A0 =A0 =A0 =A0I&#39;d also suggest you to start with a less compl= ex example,<br> =A0 =A0 =A0 =A0 =A0 =A0e.g. export only one function, make sure it works, = then add<br> =A0 =A0 =A0 =A0 =A0 =A0the rest.<br> <br> =A0 =A0 =A0 =A0 =A0 =A0If you think your .lib files doesn&#39;t do its job= , try using<br> =A0 =A0 =A0 =A0 =A0 =A0.def file instead. I find them extremely helpful, a= nd they<br> =A0 =A0 =A0 =A0 =A0 =A0are a lot easier to edit/extend.<br> <br> =A0 =A0 =A0 =A0 =A0 =A0Hope that helps.<br> <br> <br> <br> </div></blockquote> </blockquote></div><br></div> --000e0cd2586cfa1ddd04a23aaeb8--
May 01 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--00151758865205805b04a23c94ab
Content-Type: text/plain; charset=ISO-8859-1

Yes, and i have a kernel32.def for my .d file and a kernel33.def for the
original kernel dll. Your not confused, I am. I thought i needed kerel33.def
so i could acces the dll from d, isnt this the case?
Op 1-mei-2011 22:10 schreef "Rainer Schuetze" <r.sagitario gmx.de> het
volgende:
 I must have completely misunderstood what you want to do. What do you
 want to build from kernel33.def? Isn't kernel33.dll the original DLL
 that you want to intercept by replacing it with the compiled DLL?

 maarten van damme wrote:
 Great, now the error in kernel32.def is resolved but it gets the same
 problem in kernel33.def.
 here is the start of the exports from kernel33.def:
 EXPORTS
 _hread  1334
 how can I change this to resolve that?

 2011/5/1 Rainer Schuetze <r.sagitario gmx.de <mailto:r.sagitario gmx.de>>

 It seems you have hit another of those dreaded optlink bugs.

 With less symbols, it works if you declare the imports like this
 (because of the described name mangling):

 IMPORTS
 _imported_hread 0 = kernel33._hread

 2 more notes:
 - you don't need to import kernel33.di
 - you should not use "SINGLE" in the DATA statement of the def file,
 it will share the memory across processes.



 maarten van damme wrote:

 Number overflow?
 So I implemented the suggested changes (you can check them out
 at http://dl.dropbox.com/u/15024434/version2.zip)

 But now I get when I compile it : "kernel32.def(738) : Error 12:
 Number Overflow: (strange symbol over here)"

 I do agree I should've picked a simpler example but I think the
 statisfaction will be even bigger if I were to succeed :p

 2011/5/1 maarten van damme <maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com> <mailto:maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com>>>


 Wow, thanks for the help
 The first thing I did was in the .di file adding
 extern(windows){ .... }
 and now compiling doesn't give errors and when examining with
 dllexp
 I can see that it exports the same functions as the real
 kernel32.dll :D

 Now I'm going to implement all other suggested changes,
 thanks a lot


 2011/4/30 Rainer Schuetze <r.sagitario gmx.de
 <mailto:r.sagitario gmx.de>
 <mailto:r.sagitario gmx.de <mailto:r.sagitario gmx.de>>>


 I'm not sure your wrapping will work with kernel32.dll,
 but in
 general here are a few tips:

 - most functions in the windows API use the __stdcall calling
 convention in C/C++, which translates to D as
 "extern(Windows)"

 - this will usually add the number of bytes passed on the
 stack
 as a " NN" postfix to the function name. This postfix
 does not
 exist in kernel32.dll, but in the import library kernel32.lib
 that you find in the dmd lib folder. Maybe you can use the
 standard import library, or use the translation shown below..

 - as the exported function and the function you want to
 chain to
 have identical names, you have to change at least one of
 these
 and modify them in some build step. I'd suggest to do this in
 the def file:

 The symbols in the d-source file containing:

 ----
 extern(Windows) HANDLE imported_GetCurrentProcess();

 export extern(Windows) HANDLE internal_GetCurrentProcess()
 {
 return imported_GetCurrentProcess();
 }
 ----

 can be mapped to other symbols in the def file:

 ----
 EXPORTS
 GetCurrentProcess = internal_GetCurrentProcess

 IMPORTS
 imported_GetCurrentProcess = kernel33.GetCurrentProcess
 ----

 - if you don't know the number of arguments, you should
 not call
 the wrapped function, as this will change the callstack.
 Instead, you should just jump to it:

 void internal_hread()
 {
 asm
 {
 naked;
 jmp imported_hread;
 }
 }

 I haven't tried all that, though, so there might be some
 mistakes...

 Rainer



 Denis Koroskin wrote:

 On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme
 <maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com> <mailto:maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com>>>

 wrote:

 I've changed this, I think I'm still kinda
 confused with
 lib files. They've
 told me you can't do something with them without
 a .di file
 So I went ahead and made a kernel33.di file.. I now
 import it in kernel32.d
 and my declaration is
 System(C){
 export void * exportedfunctionblablabal(){
 return exportedfunctionblablablal();
 }
 ....
 }

 The file in the directory are:
 kernel32.d :
 http://dl.dropbox.com/u/15024434/d/kernel32.d
 kernel33.di :
 http://dl.dropbox.com/u/15024434/d/kernel33.di
 kernel33.lib :
 http://dl.dropbox.com/u/15024434/d/kernel33.lib
 kernel33.dll :
 http://dl.dropbox.com/u/15024434/d/kernel33.dll

 I've tried to compile using dmd -d kernel32.d
 kernel33.di kernel33.lib but
 it throws errors like
 "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
 I have literally no clue why this is the case, can
 someone help me out or
 look at the files?

 2011/4/27 maarten van damme
 <maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>
 <mailto:maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com>>>


 I'm afraid I've been a little unclear.
 I've copied kernel32.dll from the windows dir,
 renamed it to kernel33.dll
 and generated a .lib from it using implib.
 Then I've created a d file with a correct
 dllmain(stolen from examples) and
 between

 system(C){
 export void * exportedfunctionfromkernel33.dll();
 export void *
 exportedfunction2fromkernel33.dll();
 ....
 }

 But it looks like you can't both declare a
 function
 from another lib and
 export it at the same time.


 In your kernel33.di, try making it extern (C) export
 void*
 _hread(); etc. You functions get D mangling otherwise.

 I'd also suggest you to start with a less complex
 example,
 e.g. export only one function, make sure it works,
 then add
 the rest.

 If you think your .lib files doesn't do its job, try
 using
 .def file instead. I find them extremely helpful, and
 they
 are a lot easier to edit/extend.

 Hope that helps.


--00151758865205805b04a23c94ab Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <p>Yes, and i have a kernel32.def for my .d file and a kernel33.def for the= original kernel dll. Your not confused, I am. I thought i needed kerel33.d= ef so i could acces the dll from d, isnt this the case?</p> <div class=3D"gmail_quote">Op 1-mei-2011 22:10 schreef &quot;Rainer Schuetz= e&quot; &lt;<a href=3D"mailto:r.sagitario gmx.de">r.sagitario gmx.de</a>&gt= ; het volgende:<br type=3D"attribution">&gt; I must have completely misunde= rstood what you want to do. What do you <br> &gt; want to build from kernel33.def? Isn&#39;t kernel33.dll the original D= LL <br>&gt; that you want to intercept by replacing it with the compiled DL= L?<br>&gt; <br>&gt; maarten van damme wrote:<br>&gt;&gt; Great, now the err= or in kernel32.def is resolved but it gets the same <br> &gt;&gt; problem in kernel33.def.<br>&gt;&gt; here is the start of the expo= rts from kernel33.def:<br>&gt;&gt; EXPORTS<br>&gt;&gt; _hread 1334<br>&gt;= &gt; how can I change this to resolve that?<br>&gt;&gt; <br>&gt;&gt; 2011/5= /1 Rainer Schuetze &lt;<a href=3D"mailto:r.sagitario gmx.de">r.sagitario gm= x.de</a> &lt;mailto:<a href=3D"mailto:r.sagitario gmx.de">r.sagitario gmx.d= e</a>&gt;&gt;<br> &gt;&gt; <br>&gt;&gt; It seems you have hit another of those dreaded op= tlink bugs.<br>&gt;&gt; <br>&gt;&gt; With less symbols, it works if you= declare the imports like this<br>&gt;&gt; (because of the described na= me mangling):<br> &gt;&gt; <br>&gt;&gt; IMPORTS<br>&gt;&gt; _imported_hread 0 = =3D kernel33._hread<br>&gt;&gt; <br>&gt;&gt; 2 more notes:<br>&gt;&gt;= - you don&#39;t need to import kernel33.di<br>&gt;&gt; - you shoul= d not use &quot;SINGLE&quot; in the DATA statement of the def file,<br> &gt;&gt; it will share the memory across processes.<br>&gt;&gt; <br>&gt= ;&gt; <br>&gt;&gt; <br>&gt;&gt; maarten van damme wrote:<br>&gt;&gt; <b= r>&gt;&gt; Number overflow?<br>&gt;&gt; So I implemented th= e suggested changes (you can check them out<br> &gt;&gt; at <a href=3D"http://dl.dropbox.com/u/15024434/version2.zi= p">http://dl.dropbox.com/u/15024434/version2.zip</a>)<br>&gt;&gt; <br>&gt;&= gt; But now I get when I compile it : &quot;kernel32.def(738) : Err= or 12:<br> &gt;&gt; Number Overflow: (strange symbol over here)&quot;<br>&gt;&= gt; <br>&gt;&gt; I do agree I should&#39;ve picked a simpler exampl= e but I think the<br>&gt;&gt; statisfaction will be even bigger if = I were to succeed :p<br> &gt;&gt; <br>&gt;&gt; 2011/5/1 maarten van damme &lt;<a href=3D"mai= lto:maartenvd1994 gmail.com">maartenvd1994 gmail.com</a><br>&gt;&gt; = &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com">maartenvd1994 gmail= .com</a>&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com">maartenv= d1994 gmail.com</a><br> &gt;&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com">maar= tenvd1994 gmail.com</a>&gt;&gt;&gt;<br>&gt;&gt; <br>&gt;&gt; <br>&gt;&gt; = Wow, thanks for the help<br>&gt;&gt; The first thing I= did was in the .di file adding<br> &gt;&gt; extern(windows){ .... }<br>&gt;&gt; and now com= piling doesn&#39;t give errors and when examining with<br>&gt;&gt; = dllexp<br>&gt;&gt; I can see that it exports the same functions = as the real<br> &gt;&gt; kernel32.dll :D<br>&gt;&gt; <br>&gt;&gt; Now I&= #39;m going to implement all other suggested changes,<br>&gt;&gt; t= hanks a lot<br>&gt;&gt; <br>&gt;&gt; <br>&gt;&gt; 2011/4/30 Rain= er Schuetze &lt;<a href=3D"mailto:r.sagitario gmx.de">r.sagitario gmx.de</a=
<br>

io gmx.de</a>&gt;<br>&gt;&gt; &lt;mailto:<a href=3D"mailto:r.sag= itario gmx.de">r.sagitario gmx.de</a> &lt;mailto:<a href=3D"mailto:r.sagita= rio gmx.de">r.sagitario gmx.de</a>&gt;&gt;&gt;<br> &gt;&gt; <br>&gt;&gt; <br>&gt;&gt; I&#39;m not sure your wra= pping will work with kernel32.dll,<br>&gt;&gt; but in<br>&gt;&gt; = general here are a few tips:<br>&gt;&gt; <br>&gt;&gt; = - most functions in the windows API use the __stdcall calling<br> &gt;&gt; convention in C/C++, which translates to D as<br>&g= t;&gt; &quot;extern(Windows)&quot;<br>&gt;&gt; <br>&gt;&gt; = - this will usually add the number of bytes passed on the<br>&gt;&g= t; stack<br> &gt;&gt; as a &quot; NN&quot; postfix to the function name. = This postfix<br>&gt;&gt; does not<br>&gt;&gt; exist = in kernel32.dll, but in the import library kernel32.lib<br>&gt;&gt; = that you find in the dmd lib folder. Maybe you can use the<br> &gt;&gt; standard import library, or use the translation sho= wn below..<br>&gt;&gt; <br>&gt;&gt; - as the exported functi= on and the function you want to<br>&gt;&gt; chain to<br>&gt;&gt; = have identical names, you have to change at least one of<br> &gt;&gt; these<br>&gt;&gt; and modify them in some b= uild step. I&#39;d suggest to do this in<br>&gt;&gt; the def= file:<br>&gt;&gt; <br>&gt;&gt; The symbols in the d-source = file containing:<br> &gt;&gt; <br>&gt;&gt; ----<br>&gt;&gt; extern= (Windows) HANDLE imported_GetCurrentProcess();<br>&gt;&gt; <br>&gt;&gt; = export extern(Windows) HANDLE internal_GetCurrentProcess()<br> &gt;&gt; {<br>&gt;&gt; return imported_GetCu= rrentProcess();<br>&gt;&gt; }<br>&gt;&gt; ---= -<br>&gt;&gt; <br>&gt;&gt; can be mapped to other symbols in= the def file:<br> &gt;&gt; <br>&gt;&gt; ----<br>&gt;&gt; EXPORT= S<br>&gt;&gt; GetCurrentProcess =3D internal_GetCurrentProc= ess<br>&gt;&gt; <br>&gt;&gt; IMPORTS<br>&gt;&gt; = imported_GetCurrentProcess =3D kernel33.GetCurrentProcess<br> &gt;&gt; ----<br>&gt;&gt; <br>&gt;&gt; - if y= ou don&#39;t know the number of arguments, you should<br>&gt;&gt; n= ot call<br>&gt;&gt; the wrapped function, as this will chang= e the callstack.<br> &gt;&gt; Instead, you should just jump to it:<br>&gt;&gt; <b= r>&gt;&gt; void internal_hread()<br>&gt;&gt; = {<br>&gt;&gt; asm<br>&gt;&gt; {<br>&gt;&gt;= naked;<br> &gt;&gt; jmp imported_hread;<br>&gt;&gt; = }<br>&gt;&gt; }<br>&gt;&gt; <br>&gt;&gt; I ha= ven&#39;t tried all that, though, so there might be some<br>&gt;&gt; = mistakes...<br> &gt;&gt; <br>&gt;&gt; Rainer<br>&gt;&gt; <br>&gt;&gt; <br>&g= t;&gt; <br>&gt;&gt; Denis Koroskin wrote:<br>&gt;&gt; <br>&g= t;&gt; On Sat, 30 Apr 2011 13:47:53 +0400, maarten van d= amme<br> &gt;&gt; &lt;<a href=3D"mailto:maartenvd1994 gmail.com">= maartenvd1994 gmail.com</a><br>&gt;&gt; &lt;mailto:<a href=3D"mailt= o:maartenvd1994 gmail.com">maartenvd1994 gmail.com</a>&gt; &lt;mailto:<a hr= ef=3D"mailto:maartenvd1994 gmail.com">maartenvd1994 gmail.com</a><br> &gt;&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com">maar= tenvd1994 gmail.com</a>&gt;&gt;&gt;<br>&gt;&gt; <br>&gt;&gt; = wrote:<br>&gt;&gt; <br>&gt;&gt; I&#39;ve change= d this, I think I&#39;m still kinda<br> &gt;&gt; confused with<br>&gt;&gt; lib files= . They&#39;ve<br>&gt;&gt; told me you can&#39;t do s= omething with them without<br>&gt;&gt; a .di file<br>&gt;&gt; = So I went ahead and made a kernel33.di file.. I now<br> &gt;&gt; import it in kernel32.d<br>&gt;&gt; = and my declaration is<br>&gt;&gt; Sy= stem(C){<br>&gt;&gt; export void * exportedfunctionb= lablabal(){<br> &gt;&gt; return exportedfunctionblablablal();<br>&= gt;&gt; }<br>&gt;&gt; ....<br=
&gt;&gt;                        }<br>&gt;&gt; <br>&gt;&gt;                =

&gt;&gt; kernel32.d :<br>&gt;&gt; <a href=3D= "http://dl.dropbox.com/u/15024434/d/kernel32.d">http://dl.dropbox.com/u/150= 24434/d/kernel32.d</a><br>&gt;&gt; kernel33.di :<br> &gt;&gt; <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.di"=
http://dl.dropbox.com/u/15024434/d/kernel33.di</a><br>&gt;&gt;            =

tp://dl.dropbox.com/u/15024434/d/kernel33.lib">http://dl.dropbox.com/u/1502= 4434/d/kernel33.lib</a><br> &gt;&gt; kernel33.dll :<br>&gt;&gt; = <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.dll">http://d= l.dropbox.com/u/15024434/d/kernel33.dll</a><br>&gt;&gt; <br>&gt;&gt; = I&#39;ve tried to compile using dmd -d kernel32.d<br> &gt;&gt; kernel33.di kernel33.lib but<br>&gt;&gt; = it throws errors like<br>&gt;&gt; = &quot;Error 42: Symbol undifined _Dkernel1336_hreadfzpV&quot;<br>&gt;&gt= ; I have literally no clue why this is the case, can= <br> &gt;&gt; someone help me out or<br>&gt;&gt; = look at the files?<br>&gt;&gt; <br>&gt;&gt; = 2011/4/27 maarten van damme<br>&gt;&gt; &lt;<a href=3D"mailt= o:maartenvd1994 gmail.com">maartenvd1994 gmail.com</a> &lt;mailto:<a href= =3D"mailto:maartenvd1994 gmail.com">maartenvd1994 gmail.com</a>&gt;<br> &gt;&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 = gmail.com">maartenvd1994 gmail.com</a><br>&gt;&gt; &lt;mailto:<a hr= ef=3D"mailto:maartenvd1994 gmail.com">maartenvd1994 gmail.com</a>&gt;&gt;&g= t;<br> &gt;&gt; <br>&gt;&gt; <br>&gt;&gt; I&#39;m afrai= d I&#39;ve been a little unclear.<br>&gt;&gt; I&= #39;ve copied kernel32.dll from the windows dir,<br>&gt;&gt; = renamed it to kernel33.dll<br> &gt;&gt; and generated a .lib from it using impl= ib.<br>&gt;&gt; Then I&#39;ve created a d file w= ith a correct<br>&gt;&gt; dllmain(stolen from ex= amples) and<br> &gt;&gt; between<br>&gt;&gt; <br>&gt;&gt; = system(C){<br>&gt;&gt; expo= rt void * exportedfunctionfromkernel33.dll();<br>&gt;&gt; = export void *<br> &gt;&gt; exportedfunction2fromkernel33.dll();<br>&gt;&gt; = ....<br>&gt;&gt; }<br>&gt;&gt;= <br>&gt;&gt; But it looks like you can&#39;t bo= th declare a<br> &gt;&gt; function<br>&gt;&gt; from anoth= er lib and<br>&gt;&gt; export it at the same tim= e.<br>&gt;&gt; <br>&gt;&gt; <br>&gt;&gt; In your kernel3= 3.di, try making it extern (C) export<br> &gt;&gt; void*<br>&gt;&gt; _hread(); etc. You fu= nctions get D mangling otherwise.<br>&gt;&gt; <br>&gt;&gt; = I&#39;d also suggest you to start with a less complex<br>&gt;&gt; = example,<br> &gt;&gt; e.g. export only one function, make sure it wor= ks,<br>&gt;&gt; then add<br>&gt;&gt; the rest.<b= r>&gt;&gt; <br>&gt;&gt; If you think your .lib files doe= sn&#39;t do its job, try<br> &gt;&gt; using<br>&gt;&gt; .def file instead. I = find them extremely helpful, and<br>&gt;&gt; they<br>&gt;&gt; = are a lot easier to edit/extend.<br>&gt;&gt; <br>&gt;&gt; = Hope that helps.<br> &gt;&gt; <br>&gt;&gt; <br>&gt;&gt; <br>&gt;&gt; <br></div> --00151758865205805b04a23c94ab--
May 01 2011
prev sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--000e0ce0aa8e4aadb304a23cfb8f
Content-Type: text/plain; charset=ISO-8859-1

To avoid any confusing on my end, the files I need are
kernel33.dll (original kernel32.dll)
kernel33.def (So d can acces those functions)
kernel32.def (renaming happens over here, contains a list of all
import-exported functions)
kernel32.d (the code)

kernel33.def can be seen as a substitute for the import libary generated by
implib?
and when you have an import library you also need a .di file to acces the
contents?

the compile commands are
dmd -d kernel32 kernel32.def kernel33.def
dmd -d kernel32.obj kernel32.def

Tomorow I can show the files (can't acces them from this laptop)
and I'll post some snipets from the .def files ?

Can someone correct me? I'm finding d promising as language but some things
seem to be overly complicated to me (I'm a java guy, you have .java for
source, .class compiled and .jar packaged Seems way simpler xd).


really apreciating your info here :D
2011/5/1 maarten van damme <maartenvd1994 gmail.com>

 Yes, and i have a kernel32.def for my .d file and a kernel33.def for the
 original kernel dll. Your not confused, I am. I thought i needed kerel33.def
 so i could acces the dll from d, isnt this the case?
 Op 1-mei-2011 22:10 schreef "Rainer Schuetze" <r.sagitario gmx.de> het
 volgende:

 I must have completely misunderstood what you want to do. What do you
 want to build from kernel33.def? Isn't kernel33.dll the original DLL
 that you want to intercept by replacing it with the compiled DLL?

 maarten van damme wrote:
 Great, now the error in kernel32.def is resolved but it gets the same
 problem in kernel33.def.
 here is the start of the exports from kernel33.def:
 EXPORTS
 _hread  1334
 how can I change this to resolve that?

 2011/5/1 Rainer Schuetze <r.sagitario gmx.de <mailto:r.sagitario gmx.de


 It seems you have hit another of those dreaded optlink bugs.

 With less symbols, it works if you declare the imports like this
 (because of the described name mangling):

 IMPORTS
 _imported_hread 0 = kernel33._hread

 2 more notes:
 - you don't need to import kernel33.di
 - you should not use "SINGLE" in the DATA statement of the def file,
 it will share the memory across processes.



 maarten van damme wrote:

 Number overflow?
 So I implemented the suggested changes (you can check them out
 at http://dl.dropbox.com/u/15024434/version2.zip)

 But now I get when I compile it : "kernel32.def(738) : Error 12:
 Number Overflow: (strange symbol over here)"

 I do agree I should've picked a simpler example but I think the
 statisfaction will be even bigger if I were to succeed :p

 2011/5/1 maarten van damme <maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com> <mailto:maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com>>>


 Wow, thanks for the help
 The first thing I did was in the .di file adding
 extern(windows){ .... }
 and now compiling doesn't give errors and when examining with
 dllexp
 I can see that it exports the same functions as the real
 kernel32.dll :D

 Now I'm going to implement all other suggested changes,
 thanks a lot


 2011/4/30 Rainer Schuetze <r.sagitario gmx.de
 <mailto:r.sagitario gmx.de>
 <mailto:r.sagitario gmx.de <mailto:r.sagitario gmx.de>>>


 I'm not sure your wrapping will work with kernel32.dll,
 but in
 general here are a few tips:

 - most functions in the windows API use the __stdcall calling
 convention in C/C++, which translates to D as
 "extern(Windows)"

 - this will usually add the number of bytes passed on the
 stack
 as a " NN" postfix to the function name. This postfix
 does not
 exist in kernel32.dll, but in the import library kernel32.lib
 that you find in the dmd lib folder. Maybe you can use the
 standard import library, or use the translation shown below..

 - as the exported function and the function you want to
 chain to
 have identical names, you have to change at least one of
 these
 and modify them in some build step. I'd suggest to do this in
 the def file:

 The symbols in the d-source file containing:

 ----
 extern(Windows) HANDLE imported_GetCurrentProcess();

 export extern(Windows) HANDLE internal_GetCurrentProcess()
 {
 return imported_GetCurrentProcess();
 }
 ----

 can be mapped to other symbols in the def file:

 ----
 EXPORTS
 GetCurrentProcess = internal_GetCurrentProcess

 IMPORTS
 imported_GetCurrentProcess = kernel33.GetCurrentProcess
 ----

 - if you don't know the number of arguments, you should
 not call
 the wrapped function, as this will change the callstack.
 Instead, you should just jump to it:

 void internal_hread()
 {
 asm
 {
 naked;
 jmp imported_hread;
 }
 }

 I haven't tried all that, though, so there might be some
 mistakes...

 Rainer



 Denis Koroskin wrote:

 On Sat, 30 Apr 2011 13:47:53 +0400, maarten van damme
 <maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com> <mailto:maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com>>>

 wrote:

 I've changed this, I think I'm still kinda
 confused with
 lib files. They've
 told me you can't do something with them without
 a .di file
 So I went ahead and made a kernel33.di file.. I now
 import it in kernel32.d
 and my declaration is
 System(C){
 export void * exportedfunctionblablabal(){
 return exportedfunctionblablablal();
 }
 ....
 }

 The file in the directory are:
 kernel32.d :
 http://dl.dropbox.com/u/15024434/d/kernel32.d
 kernel33.di :
 http://dl.dropbox.com/u/15024434/d/kernel33.di
 kernel33.lib :
 http://dl.dropbox.com/u/15024434/d/kernel33.lib
 kernel33.dll :
 http://dl.dropbox.com/u/15024434/d/kernel33.dll

 I've tried to compile using dmd -d kernel32.d
 kernel33.di kernel33.lib but
 it throws errors like
 "Error 42: Symbol undifined _Dkernel1336_hreadfzpV"
 I have literally no clue why this is the case, can
 someone help me out or
 look at the files?

 2011/4/27 maarten van damme
 <maartenvd1994 gmail.com <mailto:maartenvd1994 gmail.com>
 <mailto:maartenvd1994 gmail.com
 <mailto:maartenvd1994 gmail.com>>>


 I'm afraid I've been a little unclear.
 I've copied kernel32.dll from the windows dir,
 renamed it to kernel33.dll
 and generated a .lib from it using implib.
 Then I've created a d file with a correct
 dllmain(stolen from examples) and
 between

 system(C){
 export void * exportedfunctionfromkernel33.dll();
 export void *
 exportedfunction2fromkernel33.dll();
 ....
 }

 But it looks like you can't both declare a
 function
 from another lib and
 export it at the same time.


 In your kernel33.di, try making it extern (C) export
 void*
 _hread(); etc. You functions get D mangling otherwise.

 I'd also suggest you to start with a less complex
 example,
 e.g. export only one function, make sure it works,
 then add
 the rest.

 If you think your .lib files doesn't do its job, try
 using
 .def file instead. I find them extremely helpful, and
 they
 are a lot easier to edit/extend.

 Hope that helps.



--000e0ce0aa8e4aadb304a23cfb8f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable To avoid any confusing on my end, the files I need are<br>kernel33.dll (ori= ginal kernel32.dll)<br>kernel33.def (So d can acces those functions)<br>ker= nel32.def (renaming happens over here, contains a list of all import-export= ed functions)<br> kernel32.d (the code)<br><br>kernel33.def can be seen as a substitute for t= he import libary generated by implib?<br>and when you have an import librar= y you also need a .di file to acces the contents?<br><br>the compile comman= ds are<br> dmd -d kernel32 kernel32.def kernel33.def<br>dmd -d kernel32.obj kernel32.d= ef<br><br>Tomorow I can show the files (can&#39;t acces them from this lapt= op) <br>and I&#39;ll post some snipets from the .def files ?<br><br>Can som= eone correct me? I&#39;m finding d promising as language but some things se= em to be overly complicated to me (I&#39;m a java guy, you have .java for s= ource, .class compiled and .jar packaged Seems way simpler xd).<br> <br><br>really apreciating your info here :D<br><div class=3D"gmail_quote">= 2011/5/1 maarten van damme <span dir=3D"ltr">&lt;<a href=3D"mailto:maartenv= d1994 gmail.com">maartenvd1994 gmail.com</a>&gt;</span><br><blockquote clas= s=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px soli= d rgb(204, 204, 204); padding-left: 1ex;"> <p>Yes, and i have a kernel32.def for my .d file and a kernel33.def for the= original kernel dll. Your not confused, I am. I thought i needed kerel33.d= ef so i could acces the dll from d, isnt this the case?</p> <div class=3D"gmail_quote">Op 1-mei-2011 22:10 schreef &quot;Rainer Schuetz= e&quot; &lt;<a href=3D"mailto:r.sagitario gmx.de" target=3D"_blank">r.sagit= ario gmx.de</a>&gt; het volgende:<div><div></div><div class=3D"h5"><br type= =3D"attribution"> &gt; I must have completely misunderstood what you want to do. What do you = <br> &gt; want to build from kernel33.def? Isn&#39;t kernel33.dll the original D= LL <br>&gt; that you want to intercept by replacing it with the compiled DL= L?<br>&gt; <br>&gt; maarten van damme wrote:<br>&gt;&gt; Great, now the err= or in kernel32.def is resolved but it gets the same <br> &gt;&gt; problem in kernel33.def.<br>&gt;&gt; here is the start of the expo= rts from kernel33.def:<br>&gt;&gt; EXPORTS<br>&gt;&gt; _hread 1334<br>&gt;= &gt; how can I change this to resolve that?<br>&gt;&gt; <br>&gt;&gt; 2011/5= /1 Rainer Schuetze &lt;<a href=3D"mailto:r.sagitario gmx.de" target=3D"_bla= nk">r.sagitario gmx.de</a> &lt;mailto:<a href=3D"mailto:r.sagitario gmx.de"= target=3D"_blank">r.sagitario gmx.de</a>&gt;&gt;<br> &gt;&gt; <br>&gt;&gt; It seems you have hit another of those dreaded op= tlink bugs.<br>&gt;&gt; <br>&gt;&gt; With less symbols, it works if you= declare the imports like this<br>&gt;&gt; (because of the described na= me mangling):<br> &gt;&gt; <br>&gt;&gt; IMPORTS<br>&gt;&gt; _imported_hread 0 = =3D kernel33._hread<br>&gt;&gt; <br>&gt;&gt; 2 more notes:<br>&gt;&gt;= - you don&#39;t need to import kernel33.di<br>&gt;&gt; - you shoul= d not use &quot;SINGLE&quot; in the DATA statement of the def file,<br> &gt;&gt; it will share the memory across processes.<br>&gt;&gt; <br>&gt= ;&gt; <br>&gt;&gt; <br>&gt;&gt; maarten van damme wrote:<br>&gt;&gt; <b= r>&gt;&gt; Number overflow?<br>&gt;&gt; So I implemented th= e suggested changes (you can check them out<br> &gt;&gt; at <a href=3D"http://dl.dropbox.com/u/15024434/version2.zi= p" target=3D"_blank">http://dl.dropbox.com/u/15024434/version2.zip</a>)<br>= &gt;&gt; <br>&gt;&gt; But now I get when I compile it : &quot;kerne= l32.def(738) : Error 12:<br> &gt;&gt; Number Overflow: (strange symbol over here)&quot;<br>&gt;&= gt; <br>&gt;&gt; I do agree I should&#39;ve picked a simpler exampl= e but I think the<br>&gt;&gt; statisfaction will be even bigger if = I were to succeed :p<br> &gt;&gt; <br>&gt;&gt; 2011/5/1 maarten van damme &lt;<a href=3D"mai= lto:maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a><= br>&gt;&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com" t= arget=3D"_blank">maartenvd1994 gmail.com</a>&gt; &lt;mailto:<a href=3D"mail= to:maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a><b= r> &gt;&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com" targ= et=3D"_blank">maartenvd1994 gmail.com</a>&gt;&gt;&gt;<br>&gt;&gt; <br>&gt;&= gt; <br>&gt;&gt; Wow, thanks for the help<br>&gt;&gt; = The first thing I did was in the .di file adding<br> &gt;&gt; extern(windows){ .... }<br>&gt;&gt; and now com= piling doesn&#39;t give errors and when examining with<br>&gt;&gt; = dllexp<br>&gt;&gt; I can see that it exports the same functions = as the real<br> &gt;&gt; kernel32.dll :D<br>&gt;&gt; <br>&gt;&gt; Now I&= #39;m going to implement all other suggested changes,<br>&gt;&gt; t= hanks a lot<br>&gt;&gt; <br>&gt;&gt; <br>&gt;&gt; 2011/4/30 Rain= er Schuetze &lt;<a href=3D"mailto:r.sagitario gmx.de" target=3D"_blank">r.s= agitario gmx.de</a><br> &gt;&gt; &lt;mailto:<a href=3D"mailto:r.sagitario gmx.de" target=3D= "_blank">r.sagitario gmx.de</a>&gt;<br>&gt;&gt; &lt;mailto:<a hr= ef=3D"mailto:r.sagitario gmx.de" target=3D"_blank">r.sagitario gmx.de</a> &= lt;mailto:<a href=3D"mailto:r.sagitario gmx.de" target=3D"_blank">r.sagitar= io gmx.de</a>&gt;&gt;&gt;<br> &gt;&gt; <br>&gt;&gt; <br>&gt;&gt; I&#39;m not sure your wra= pping will work with kernel32.dll,<br>&gt;&gt; but in<br>&gt;&gt; = general here are a few tips:<br>&gt;&gt; <br>&gt;&gt; = - most functions in the windows API use the __stdcall calling<br> &gt;&gt; convention in C/C++, which translates to D as<br>&g= t;&gt; &quot;extern(Windows)&quot;<br>&gt;&gt; <br>&gt;&gt; = - this will usually add the number of bytes passed on the<br>&gt;&g= t; stack<br> &gt;&gt; as a &quot; NN&quot; postfix to the function name. = This postfix<br>&gt;&gt; does not<br>&gt;&gt; exist = in kernel32.dll, but in the import library kernel32.lib<br>&gt;&gt; = that you find in the dmd lib folder. Maybe you can use the<br> &gt;&gt; standard import library, or use the translation sho= wn below..<br>&gt;&gt; <br>&gt;&gt; - as the exported functi= on and the function you want to<br>&gt;&gt; chain to<br>&gt;&gt; = have identical names, you have to change at least one of<br> &gt;&gt; these<br>&gt;&gt; and modify them in some b= uild step. I&#39;d suggest to do this in<br>&gt;&gt; the def= file:<br>&gt;&gt; <br>&gt;&gt; The symbols in the d-source = file containing:<br> &gt;&gt; <br>&gt;&gt; ----<br>&gt;&gt; extern= (Windows) HANDLE imported_GetCurrentProcess();<br>&gt;&gt; <br>&gt;&gt; = export extern(Windows) HANDLE internal_GetCurrentProcess()<br> &gt;&gt; {<br>&gt;&gt; return imported_GetCu= rrentProcess();<br>&gt;&gt; }<br>&gt;&gt; ---= -<br>&gt;&gt; <br>&gt;&gt; can be mapped to other symbols in= the def file:<br> &gt;&gt; <br>&gt;&gt; ----<br>&gt;&gt; EXPORT= S<br>&gt;&gt; GetCurrentProcess =3D internal_GetCurrentProc= ess<br>&gt;&gt; <br>&gt;&gt; IMPORTS<br>&gt;&gt; = imported_GetCurrentProcess =3D kernel33.GetCurrentProcess<br> &gt;&gt; ----<br>&gt;&gt; <br>&gt;&gt; - if y= ou don&#39;t know the number of arguments, you should<br>&gt;&gt; n= ot call<br>&gt;&gt; the wrapped function, as this will chang= e the callstack.<br> &gt;&gt; Instead, you should just jump to it:<br>&gt;&gt; <b= r>&gt;&gt; void internal_hread()<br>&gt;&gt; = {<br>&gt;&gt; asm<br>&gt;&gt; {<br>&gt;&gt;= naked;<br> &gt;&gt; jmp imported_hread;<br>&gt;&gt; = }<br>&gt;&gt; }<br>&gt;&gt; <br>&gt;&gt; I ha= ven&#39;t tried all that, though, so there might be some<br>&gt;&gt; = mistakes...<br> &gt;&gt; <br>&gt;&gt; Rainer<br>&gt;&gt; <br>&gt;&gt; <br>&g= t;&gt; <br>&gt;&gt; Denis Koroskin wrote:<br>&gt;&gt; <br>&g= t;&gt; On Sat, 30 Apr 2011 13:47:53 +0400, maarten van d= amme<br> &gt;&gt; &lt;<a href=3D"mailto:maartenvd1994 gmail.com" = target=3D"_blank">maartenvd1994 gmail.com</a><br>&gt;&gt; &lt;mailt= o:<a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_blank">maartenvd199= 4 gmail.com</a>&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com" t= arget=3D"_blank">maartenvd1994 gmail.com</a><br> &gt;&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com" targ= et=3D"_blank">maartenvd1994 gmail.com</a>&gt;&gt;&gt;<br>&gt;&gt; <br>&gt;&= gt; wrote:<br>&gt;&gt; <br>&gt;&gt; = I&#39;ve changed this, I think I&#39;m still kinda<br> &gt;&gt; confused with<br>&gt;&gt; lib files= . They&#39;ve<br>&gt;&gt; told me you can&#39;t do s= omething with them without<br>&gt;&gt; a .di file<br>&gt;&gt; = So I went ahead and made a kernel33.di file.. I now<br> &gt;&gt; import it in kernel32.d<br>&gt;&gt; = and my declaration is<br>&gt;&gt; Sy= stem(C){<br>&gt;&gt; export void * exportedfunctionb= lablabal(){<br> &gt;&gt; return exportedfunctionblablablal();<br>&= gt;&gt; }<br>&gt;&gt; ....<br=
&gt;&gt;                        }<br>&gt;&gt; <br>&gt;&gt;                =

&gt;&gt; kernel32.d :<br>&gt;&gt; <a href=3D= "http://dl.dropbox.com/u/15024434/d/kernel32.d" target=3D"_blank">http://dl= .dropbox.com/u/15024434/d/kernel32.d</a><br>&gt;&gt; = kernel33.di :<br> &gt;&gt; <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.di"= target=3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.di</a><br>&g= t;&gt; kernel33.lib :<br>&gt;&gt; = <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.lib" target=3D"= _blank">http://dl.dropbox.com/u/15024434/d/kernel33.lib</a><br> &gt;&gt; kernel33.dll :<br>&gt;&gt; = <a href=3D"http://dl.dropbox.com/u/15024434/d/kernel33.dll" target= =3D"_blank">http://dl.dropbox.com/u/15024434/d/kernel33.dll</a><br>&gt;&gt;= <br> &gt;&gt; I&#39;ve tried to compile using dmd -d kern= el32.d<br> &gt;&gt; kernel33.di kernel33.lib but<br>&gt;&gt; = it throws errors like<br>&gt;&gt; = &quot;Error 42: Symbol undifined _Dkernel1336_hreadfzpV&quot;<br>&gt;&gt= ; I have literally no clue why this is the case, can= <br> &gt;&gt; someone help me out or<br>&gt;&gt; = look at the files?<br>&gt;&gt; <br>&gt;&gt; = 2011/4/27 maarten van damme<br>&gt;&gt; &lt;<a href=3D"mailt= o:maartenvd1994 gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a> &l= t;mailto:<a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_blank">maart= envd1994 gmail.com</a>&gt;<br> &gt;&gt; &lt;mailto:<a href=3D"mailto:maartenvd1994 = gmail.com" target=3D"_blank">maartenvd1994 gmail.com</a><br>&gt;&gt; = &lt;mailto:<a href=3D"mailto:maartenvd1994 gmail.com" target=3D"_blank">m= aartenvd1994 gmail.com</a>&gt;&gt;&gt;<br> &gt;&gt; <br>&gt;&gt; <br>&gt;&gt; I&#39;m afrai= d I&#39;ve been a little unclear.<br>&gt;&gt; I&= #39;ve copied kernel32.dll from the windows dir,<br>&gt;&gt; = renamed it to kernel33.dll<br> &gt;&gt; and generated a .lib from it using impl= ib.<br>&gt;&gt; Then I&#39;ve created a d file w= ith a correct<br>&gt;&gt; dllmain(stolen from ex= amples) and<br> &gt;&gt; between<br>&gt;&gt; <br>&gt;&gt; = system(C){<br>&gt;&gt; expo= rt void * exportedfunctionfromkernel33.dll();<br>&gt;&gt; = export void *<br> &gt;&gt; exportedfunction2fromkernel33.dll();<br>&gt;&gt; = ....<br>&gt;&gt; }<br>&gt;&gt;= <br>&gt;&gt; But it looks like you can&#39;t bo= th declare a<br> &gt;&gt; function<br>&gt;&gt; from anoth= er lib and<br>&gt;&gt; export it at the same tim= e.<br>&gt;&gt; <br>&gt;&gt; <br>&gt;&gt; In your kernel3= 3.di, try making it extern (C) export<br> &gt;&gt; void*<br>&gt;&gt; _hread(); etc. You fu= nctions get D mangling otherwise.<br>&gt;&gt; <br>&gt;&gt; = I&#39;d also suggest you to start with a less complex<br>&gt;&gt; = example,<br> &gt;&gt; e.g. export only one function, make sure it wor= ks,<br>&gt;&gt; then add<br>&gt;&gt; the rest.<b= r>&gt;&gt; <br>&gt;&gt; If you think your .lib files doe= sn&#39;t do its job, try<br> &gt;&gt; using<br>&gt;&gt; .def file instead. I = find them extremely helpful, and<br>&gt;&gt; they<br>&gt;&gt; = are a lot easier to edit/extend.<br>&gt;&gt; <br>&gt;&gt; = Hope that helps.<br> &gt;&gt; <br>&gt;&gt; <br>&gt;&gt; <br>&gt;&gt; <br></div></div></div> </blockquote></div><br> --000e0ce0aa8e4aadb304a23cfb8f--
May 01 2011