Thread: HOW TO Find right names for GetProcAddress

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    HOW TO Find right names for GetProcAddress

    Hi guys, i made a .dll file ,and tried to use LoadLibrary() and GetProcAddress() to get the functions in .dll, but due to the calling conventions ,i failed to do this with all the functions,pls help

    [tag]some snippets in mydll.cpp for mydll.dll[/tag]
    Code:
    .
    ...
    ....
    ......
    extern "C" _declspec(dllexport) void echo1(){};
    extern "C" _declspec(dllexport) void _stdcall echo2(){};
    
     _declspec(dllexport) void echo3(){};
     _declspec(dllexport) void _stdcall echo4(){};
    ......
    ....
    ..
    .
    i built it and got a mydll.dll file, then tried to use it in anoter .cpp file, but some wrong happened in calling GetProcAddress(),listing below:

    [tag].cpp using functions in mydll.dll [/tag]
    Code:
    #include <windows.h>
    #include "stdio.h"
    int main(int argc, char** argv)
    {
        HMODULE hDll;
        hDll = (HMODULE)LoadLibrary("my.dll");
    
        typedef void(FAR *MyEcho1)();
        MyEcho1 myecho1;
        myecho1 = (MyEcho1)GetProcAddress(hDll,"echo1");
            if(!myecho1)
            {printf("Can't find func!");}
            else
            myecho1();       //   function called successful
    
        typedef void(FAR *MyEcho2)();
        MyEcho2 myecho2;
        myecho2= (MyEcho2)GetProcAddress(hDll,"echo2");
            if(!myecho2)
            {printf("Can't find func!");}
            else
            myecho2();             //  function called successful
    
    
        typedef void  (FAR _cdecl *MyEcho3)();  // here is the problem, i think
        MyEcho3 myecho3;
        myecho3= (MyEcho3)GetProcAddress(hDll,"echo3");// Problem happened here        
            if(!myecho3)
            {printf("Can't find func!\n");}  //so printed error!
            else
            myecho3();
    
        typedef void  (FAR _stdcall *MyEcho4)();  //this block has same problem with upper one    
        MyEcho4 myecho4;
        myecho4= (MyEcho4)GetProcAddress(hDll,"echo4");
            if(!myecho4)
            {printf("Can't find func!\n")};
         else
            myecho4();
    
    
        FreeLibrary(hDll);
        return 0;
        }
    [tag]wrong code,how can i do it in a right way?[/tag]
    Code:
    typedef void  (FAR _cdecl *MyEcho3)();  
    typedef void  (FAR _stdcall *MyEcho4)();
    i know that without extern "C", c++ complier(using code::blocks, but compliser is MS VC++2005/2008) changed the name of echo3 and echo4. i don't want a .DEF file, how can i make it and call it with LoadLibrary() and GetProcAddress(), thanks!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    VS has utility called dumpbin

    using option /EXPORTS you can find out names exported by the dll
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37
    thanks vart, i konw dumpbin, but can i add "_cdecl" or "_stdcall" to the declaration(such as tpyedef void(_stdcall *MyFunc)() ) so that the machine will gengerate the right name according?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's __cdecl and __stdcall (note the two underscores).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Quote Originally Posted by Elysia View Post
    It's __cdecl and __stdcall (note the two underscores).
    which is WINAPI u can either use __cdecl or WINAPI

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    WINAPI is __stdcall.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    oh yes your right its been time that i checked calling conventions.

  8. #8
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37
    Originally Posted by Elysia
    It's __cdecl and __stdcall (note the two underscores).
    is there any differences between one(_stdcall) an two(__stdcall) underscores? i tried and neither of them works...

    i doubt that we cann't instruct them to find out the right name properly by telling the complier that it's a __stdcall or __cdecl convention, unless we define and output the name by a .DEF file.

    and another question, when i use a DEF file, all the funcs work well and i notice that it doesn't nessary to indicate the calling convention, even the funcs have arguments with them... is it true?

    it is said that calling a function by wrong convention may destroy the stack,
    Code:
    .dll
     __declspec(dllexport) int func(int a,int b);// use __cdecl convention
       
    .cpp
      typedef int (*__stdcallDLLFUNC)func(int a,int b);  //changed the calling convetion to __stdcall
      hLib = LoadLibrary(...);
    
      DLLFUNC func = (DLLFUNC)GetProcAddress(...)//changeing convention
      result = func(1,2);//Error occurs here
    so why my applicaton works well and ends normaly?(ps: i've tried both functions with argument and without arguments)

  9. #9
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37
    Quote Originally Posted by albert3721 View Post
    is there any differences between one(_stdcall) an two(__stdcall) underscores? i tried and neither of them works...

    i doubt that we cann't instruct them to find out the right name properly by telling the complier that it's a __stdcall or __cdecl convention, unless we define and output the name by a .DEF file.

    and another question, when i use a DEF file, all the funcs work well and i notice that it doesn't nessary to indicate the calling convention, even the funcs have arguments with them... is it true?

    it is said that calling a function by wrong convention may destroy the stack,
    Code:
    .dll
     __declspec(dllexport) int func(int a,int b);// use __cdecl convention
       
    .cpp
      typedef int (*__stdcallDLLFUNC)func(int a,int b);  //changed the calling convetion to __stdcall
      hLib = LoadLibrary(...);
    
      DLLFUNC func = (DLLFUNC)GetProcAddress(...)//changeing convention
      result = func(1,2);//Error occurs here
    so why my applicaton works well and ends normaly?(ps: i've tried both functions with argument and without arguments)
    well, it finally crashes, a wrong calling convention does destroy the stack!
    and sth. flashed through my brain,
    Code:
    typedef void (__stdcall *MyFucn())
    "__stdcall" in the snippet only indicates the calling convention. and GetProcAddress("xxx") only tries to find "xxx" in a .dll , it never converts "xxx" to" _xxx@nubmer" according to the convention indicator"__stdcall" and it never checks if xxx is built by C or C++(or it will,in c++ case, change xxx to xxx@@YG...Z)


    so it's impossiable to "GetProcAddress()" a function in a .dll if one only knows the orginal name, because the original name(i.e. void echo() ) in .dll has been changed(?echo@@YAXXZ) under different circumstances (extern "C" or __stdcall,__fastcall,etc..)after being exported . is it correct?

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by albert3721 View Post
    so it's impossiable to "GetProcAddress()" a function in a .dll if one only knows the orginal name, because the original name(i.e. void echo() ) in .dll has been changed(?echo@@YAXXZ) under different circumstances (extern "C" or __stdcall,__fastcall,etc..)after being exported . is it correct?
    Not impossible, just a bit more complicated.
    You can examine the dll's export table and scan for the function name there. Or if you know the ordinal value of the exported function you can pass that to GetProcAddress.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Determining Which Exporting Method to Use

    The client and DLL need to use the same linkage and calling convention. In a C file, you have C linkage by default. In a C++ file, you have C++ linkage by default. A C++ file can specify C linkage via 'extern "C"'.

    In MSVC, 32 and 64 bit, when ever you dllexport a cdecl function with C linkage, the name is not decorated.

    In MSVC 64 bit, when ever you dllexport a function with C linkage, using any calling convention, the name is not decorated.

    gg

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Codeplug View Post
    In MSVC 64 bit, when ever you dllexport a function with C linkage, using any calling convention, the name is not decorated.
    I take it this is because x64 (I have no idea how IA64 works) only has 1 calling convention so there's no need for name decoration?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Where did you get IA64 from? x64 is a synonym for x86-64, AMD's 64-bit architecture.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Elysia View Post
    Where did you get IA64 from? x64 is a synonym for x86-64, AMD's 64-bit architecture.
    I know, but codeplug just said "64 bit" without mentioning an architecture. And I know that x64 has just 1 calling convention. But since I don't know how IA64 does it I thought I should mention that in case someone wants to enlighten me

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I meant "x64". Turns out IA64 has the same behavior as x64. All confirmed with VS2008.

    >> ... only has 1 calling convention so there's no need for name decoration?
    A reasonable assumption I suppose.

    Another point to take away from this: Name decoration is compiler specific - even compiler version specific.
    Quote Originally Posted by MSDN
    Pros and Cons of Using __declspec(dllexport)
    ... If you rebuild the DLL with new exports, you also have to rebuild the application because the decorated names for exported C++ functions might change if you recompile with a different version of the compiler.
    So avoid name decorations if you want to use LoadLibrary/GetProcAddress.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  3. ld.exe: cannot find -l-lstdc++
    By Tonto in forum Tech Board
    Replies: 3
    Last Post: 04-10-2007, 11:20 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM