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.
  • 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