LoadLibrary+ GetporcAddress to call function form custom dll
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: LoadLibrary+ GetporcAddress to call function form custom dll

  1. #1
    Join Date
    May 2010
    Posts
    83

    Angry LoadLibrary+ GetporcAddress to call function form custom dll

    Hi,

    I want to call function "asdf" from dl "x.dll":


    Code:
    typedef int (*MYPROC)(LPVOID adr,BYTE* arg0,int arg2);
    
    
    HANDLE testh;
    LPVOID adr;
    MYPROC dllfunc;
    BYTE    arg0[] = {0x01, 0x01, 0x01, 0x01, 0x00};
    int arg2 = 5;
    int retval;
    
    testh = CreateFileA("C:\\asdf.exe",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    testh = CreateFileMappingA(testh,0,PAGE_READWRITE,0,filesize,NULL);
    addressx = ::MapViewOfFile(testh, FILE_MAP_WRITE,0,0,0);
    
    BYTE *arg0de = &arg0[0];
    
    testh = LoadLibraryA("x.dll");
    dllfunc = (MYPROC)GetProcAddress((HMODULE)testh,"asdf");
    
    retval = dllfunc(adr,arg0de,arg2);

    This code works perfectly fine when compiled in a Win32 console project. WHen compiled in a managed C++ form project (button click event) it throws a access violation exception when calling the function....?

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    Code:
    testh = LoadLibraryA("x.dll");
    assert(testh);
    dllfunc = (MYPROC)GetProcAddress((HMODULE)testh,"asdf");
    assert(dllfunc);
    retval = dllfunc(adr,arg0de,arg2);
    Do either of the asserts fail?

  3. #3
    Join Date
    May 2010
    Posts
    83

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    No.

  4. #4
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    is it a 64bit OS your running your code , i.e win 7 64 bit , if so go to project options make sure u use .NET 32bit , and then compile

  5. #5
    Join Date
    May 2010
    Posts
    83

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    Sorry, forgot to mention that. I am compiling & testing on a 32 bit XP SP3.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    Where exactly does the code crash? When calling the function, or within the function? Step through the code and watch your values.

    Are your function declaration and the actual function using the same calling standard? I think VC++ defaults to __stdcall, but most library interfaces use __cdecl

  7. #7
    Join Date
    May 2010
    Posts
    83

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    It crashes within the external dll. All the values in my code are correct.
    Not sure about these calling standards, but since the same code works fine in an win32 project, it should be ok... (anyways, where can i change that?).

    The dll is written in MASM.

    thx for you help so far!

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    My guess is that it's a calling convention incompatibility. Being an assembly programmer myself, I'm betting that the library uses cdecl convention.

    Honestly, I'm not sure how to change the typedef, and I think Microsoft it does it differently than GNU, but you can try this:

    Code:
    typedef int (*MYPROC)(LPVOID adr,BYTE* arg0,int arg2) __attribute__((cdecl));
    If that doesn't work then one of the Windows programmers around here might have to help.

  9. #9
    Join Date
    May 2010
    Posts
    83

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    I just emailed the author of the dll, it uses stdcall.

  10. #10
    Join Date
    May 2010
    Posts
    83

    Re: LoadLibrary+ GetporcAddress to call function form custom dll

    Hey, i GOT it!
    The masm dll is compiled for __stdcall, so you have to define your typedefas __stdcall:

    typedef int (__stdcall *MYPROC)(LPVOID adr,BYTE* arg0,int arg2);

    Now it works just fine!
    Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center