Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetProcAddress fro member function

GetProcAddress fro member function

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vibindia
    wrote on last edited by
    #1

    Hi All How can i get the Address of an Member function in Regular DLL using GetProcAddress(); I would be fine if i get an article for using an member function of an class in an dll. And what is the purpose of .def file and how to create it.

    VIBIN "Fool's run away,where angle's fear to tread"

    _ P S T 4 Replies Last reply
    0
    • V vibindia

      Hi All How can i get the Address of an Member function in Regular DLL using GetProcAddress(); I would be fine if i get an article for using an member function of an class in an dll. And what is the purpose of .def file and how to create it.

      VIBIN "Fool's run away,where angle's fear to tread"

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      See Here The .def file is a Module-definition (.def) file that provides the linker information no exports, attributes, and other information about the program to be linked. A .def file is most useful when building a DLL.(.def) file is useful when you are not using the __declspec(dllexport) keyword to export the functions from the DLL.

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      S 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        See Here The .def file is a Module-definition (.def) file that provides the linker information no exports, attributes, and other information about the program to be linked. A .def file is most useful when building a DLL.(.def) file is useful when you are not using the __declspec(dllexport) keyword to export the functions from the DLL.

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        He wants to get the address of a member function. This link isn't much help in this regard as it doesn't address issues such a name mangling which are required to get it to work.

        Steve

        V _ 2 Replies Last reply
        0
        • S Stephen Hewitt

          He wants to get the address of a member function. This link isn't much help in this regard as it doesn't address issues such a name mangling which are required to get it to work.

          Steve

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          Yeah Steve. Thanks for ur words. I do realize it. :-D

          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

          1 Reply Last reply
          0
          • S Stephen Hewitt

            He wants to get the address of a member function. This link isn't much help in this regard as it doesn't address issues such a name mangling which are required to get it to work.

            Steve

            V Offline
            V Offline
            vibindia
            wrote on last edited by
            #5

            Ya that link shwos how to access global function. I would like to access the member function's addtess from an dll.

            VIBIN "Fool's run away,where angle's fear to tread"

            1 Reply Last reply
            0
            • V vibindia

              Hi All How can i get the Address of an Member function in Regular DLL using GetProcAddress(); I would be fine if i get an article for using an member function of an class in an dll. And what is the purpose of .def file and how to create it.

              VIBIN "Fool's run away,where angle's fear to tread"

              P Offline
              P Offline
              Paresh Chitte
              wrote on last edited by
              #6

              HMODULE hMod = LoadLibrary(_T("C:\\YourDLL.dll")); if (hMod > (HINSTANCE)HINSTANCE_ERROR) { // Find the entry point. FARPROC lpDllEntryPoint = GetProcAddress(hMod, "YourFunction"); if (lpDllEntryPoint != NULL) { (*lpDllEntryPoint)(); } FreeLibrary(hMod); } Regards, Paresh,

              S 1 Reply Last reply
              0
              • P Paresh Chitte

                HMODULE hMod = LoadLibrary(_T("C:\\YourDLL.dll")); if (hMod > (HINSTANCE)HINSTANCE_ERROR) { // Find the entry point. FARPROC lpDllEntryPoint = GetProcAddress(hMod, "YourFunction"); if (lpDllEntryPoint != NULL) { (*lpDllEntryPoint)(); } FreeLibrary(hMod); } Regards, Paresh,

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                He wants to call member functions. Because of name mangling getting the name of the function is not trivial. For example, on MSVC6 the mangled name of the function void CMyClass::MyFunction() is “?MyFunction@CMyClass@@QAEXXZ”. Name mangling is also compiler specific.

                Steve

                P 1 Reply Last reply
                0
                • S Stephen Hewitt

                  He wants to call member functions. Because of name mangling getting the name of the function is not trivial. For example, on MSVC6 the mangled name of the function void CMyClass::MyFunction() is “?MyFunction@CMyClass@@QAEXXZ”. Name mangling is also compiler specific.

                  Steve

                  P Offline
                  P Offline
                  Paresh Chitte
                  wrote on last edited by
                  #8

                  Thanks. If we look at the MSDN help for GetProcAddress it says "The GetProcAddress function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL)." Hence, we cannot use GetProcAddress in this scenario. Regards, Paresh.

                  S 1 Reply Last reply
                  0
                  • P Paresh Chitte

                    Thanks. If we look at the MSDN help for GetProcAddress it says "The GetProcAddress function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL)." Hence, we cannot use GetProcAddress in this scenario. Regards, Paresh.

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    It is possible, just a little messy.

                    Steve

                    P 1 Reply Last reply
                    0
                    • S Stephen Hewitt

                      It is possible, just a little messy.

                      Steve

                      P Offline
                      P Offline
                      Paresh Chitte
                      wrote on last edited by
                      #10

                      Please let me know if you find a way. Regards, Paresh.

                      S 1 Reply Last reply
                      0
                      • P Paresh Chitte

                        Please let me know if you find a way. Regards, Paresh.

                        S Offline
                        S Offline
                        Stephen Hewitt
                        wrote on last edited by
                        #11

                        First you have to find the mangled name of the function you want to import. Probably the easiest way to do this is generate a linker error. For example:

                        class CMyClass
                        {
                        public:
                        void MyFunction();
                        };
                         
                        void main()
                        {
                        CMyClass mc;
                        mc.MyFunction();
                        }

                        Since the function CMyClass::MyFunction is not defined (it is declared) the linker produces the following error message: “error LNK2001: unresolved external symbol "public: void __thiscall CMyClass::MyFunction(void)" (?MyFunction@CMyClass@@QAEXXZ)” I have underlined the mangled name. Use this name when calling GetProcAddress Assume the following typedef:

                        typedef void (CMyClass::*PMyFunction)();

                        Then our code would look like this:

                        FARPROC pRaw = GetProcAddress(hModule, “?MyFunction@CMyClass@@QAEXXZ”);
                        PMyFunction pFun;
                        *(FARPROC*)&pFun = p;
                         
                        // Now call through the pointer.
                        CMyClass mc;
                        (mc.*pFun)(); // Use “->*” if you’ve got a pointer to the class.

                        All in all it's a bad design and should not be contemplated; the whole think stinks of hack. Use COM instead.

                        Steve

                        P 1 Reply Last reply
                        0
                        • V vibindia

                          Hi All How can i get the Address of an Member function in Regular DLL using GetProcAddress(); I would be fine if i get an article for using an member function of an class in an dll. And what is the purpose of .def file and how to create it.

                          VIBIN "Fool's run away,where angle's fear to tread"

                          S Offline
                          S Offline
                          Stephen Hewitt
                          wrote on last edited by
                          #12

                          http://www.codeproject.com/script/comments/forums.asp?msg=2012519&forumid=1647#xx2012519xx[^]

                          Steve

                          V 1 Reply Last reply
                          0
                          • S Stephen Hewitt

                            First you have to find the mangled name of the function you want to import. Probably the easiest way to do this is generate a linker error. For example:

                            class CMyClass
                            {
                            public:
                            void MyFunction();
                            };
                             
                            void main()
                            {
                            CMyClass mc;
                            mc.MyFunction();
                            }

                            Since the function CMyClass::MyFunction is not defined (it is declared) the linker produces the following error message: “error LNK2001: unresolved external symbol "public: void __thiscall CMyClass::MyFunction(void)" (?MyFunction@CMyClass@@QAEXXZ)” I have underlined the mangled name. Use this name when calling GetProcAddress Assume the following typedef:

                            typedef void (CMyClass::*PMyFunction)();

                            Then our code would look like this:

                            FARPROC pRaw = GetProcAddress(hModule, “?MyFunction@CMyClass@@QAEXXZ”);
                            PMyFunction pFun;
                            *(FARPROC*)&pFun = p;
                             
                            // Now call through the pointer.
                            CMyClass mc;
                            (mc.*pFun)(); // Use “->*” if you’ve got a pointer to the class.

                            All in all it's a bad design and should not be contemplated; the whole think stinks of hack. Use COM instead.

                            Steve

                            P Offline
                            P Offline
                            Paresh Chitte
                            wrote on last edited by
                            #13

                            Thanks. Regards, Paresh.

                            1 Reply Last reply
                            0
                            • S Stephen Hewitt

                              http://www.codeproject.com/script/comments/forums.asp?msg=2012519&forumid=1647#xx2012519xx[^]

                              Steve

                              V Offline
                              V Offline
                              vibindia
                              wrote on last edited by
                              #14

                              Or can put the following code in DLL to create object for the class MyClass in Client Application

                              extern "C" __declspec(dllexport) MyClass* CreateObjectofMyClass()
                              {
                              return new MyClass(); //Ctor
                              }

                              extern "C" __declspec(dllexport) MyClass* DeleteObjectofMyClass(MyClass *a)
                              {
                              delete a; //Dtor
                              }

                              VIBIN "Fool's run away,where angle's fear to tread"

                              S P 2 Replies Last reply
                              0
                              • V vibindia

                                Or can put the following code in DLL to create object for the class MyClass in Client Application

                                extern "C" __declspec(dllexport) MyClass* CreateObjectofMyClass()
                                {
                                return new MyClass(); //Ctor
                                }

                                extern "C" __declspec(dllexport) MyClass* DeleteObjectofMyClass(MyClass *a)
                                {
                                delete a; //Dtor
                                }

                                VIBIN "Fool's run away,where angle's fear to tread"

                                S Offline
                                S Offline
                                Stephen Hewitt
                                wrote on last edited by
                                #15

                                This will not help as it stands. The DLL contains the class’s functions and the EXE (what loads the DLL and wants to call into it) will contain the class definition only (no function definitions). You still need the addresses of the functions of the class in the DLL; you’ve only got the address of the class itself. You can get around this by using virtual functions however.

                                Steve

                                1 Reply Last reply
                                0
                                • V vibindia

                                  Or can put the following code in DLL to create object for the class MyClass in Client Application

                                  extern "C" __declspec(dllexport) MyClass* CreateObjectofMyClass()
                                  {
                                  return new MyClass(); //Ctor
                                  }

                                  extern "C" __declspec(dllexport) MyClass* DeleteObjectofMyClass(MyClass *a)
                                  {
                                  delete a; //Dtor
                                  }

                                  VIBIN "Fool's run away,where angle's fear to tread"

                                  P Offline
                                  P Offline
                                  Paresh Chitte
                                  wrote on last edited by
                                  #16

                                  Please refer this[^]. Please also have a look at this[^]. Regards, Paresh.

                                  1 Reply Last reply
                                  0
                                  • V vibindia

                                    Hi All How can i get the Address of an Member function in Regular DLL using GetProcAddress(); I would be fine if i get an article for using an member function of an class in an dll. And what is the purpose of .def file and how to create it.

                                    VIBIN "Fool's run away,where angle's fear to tread"

                                    T Offline
                                    T Offline
                                    twhall
                                    wrote on last edited by
                                    #17

                                    This works for me in Visual Studio 2008 / C++ for a static member function of a class, if there's only one class in the DLL: * Define the class .h and .cpp files in the usual way. __declspec(dllexport) seems neither necessary nor helpful. For example:

                                    class MyClass
                                    {
                                    public:
                                    static int func (int argc, char * argv []);
                                    };

                                    * Create a .def file for the DLL project: Project | Add New Item... | Module-Definition File (.def) * In the .def file, export the name(s) of the static member function(s) that you want to find through GetProcAddress(). For example:

                                    LIBRARY "DLLProjectName"
                                    EXPORTS func

                                    * Note that the .def file doesn't declare any MyClass:: scope. I get linker "undefined symbol" errors if I try to include the class scope. I get linker ambiguity errors if there are multiple definitions of func in the library, even if they're at different class or namespace scopes. * Set the project properties to use the .def file: Project | Properties | Linker | Input | Module Definition File * In the program that will use this library, load the DLL with LoadLibrary(). Note that the DLL file name needs to be passed as a wchar_t* (not just char*). * Get the function pointer with GetProcAddress with the un-scoped name you put in the .def file:

                                    HINSTANCE lib = LoadLibrary (...);
                                    typedef int (* FuncT) (int, char * []);
                                    FuncT func = FuncT (GetProcAddress (lib, "func"));
                                    func (argc, argv);

                                    * I don't know why this works without a class scope on func, or why it fails if I try to include the scope. * The .def file seems to handle the C++ name decoration issues; it's not necessary to know the decorated name. * I couldn't get this to work with only __declspec(dllexport) and no .def file. * With the .def file, __declspec(dllexport) doesn't seem to make any difference. * I haven't yet tried to create an instance of the class and invoke it's non-static functions from the loading program (outside of the DLL itself). I guess that's next on the agenda. Maybe I'll need to __declspec(dllexport) the class for that ...

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups