GetProcAddress fro member function
-
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"
-
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"
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_
-
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_
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
-
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
Yeah Steve. Thanks for ur words. I do realize it. :-D
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
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
-
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"
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, -
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,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
-
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
Thanks. If we look at the MSDN help for
GetProcAddress
it says "TheGetProcAddress
function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL)." Hence, we cannot useGetProcAddress
in this scenario. Regards, Paresh. -
Thanks. If we look at the MSDN help for
GetProcAddress
it says "TheGetProcAddress
function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL)." Hence, we cannot useGetProcAddress
in this scenario. Regards, Paresh.It is possible, just a little messy.
Steve
-
It is possible, just a little messy.
Steve
Please let me know if you find a way. Regards, Paresh.
-
Please let me know if you find a way. Regards, Paresh.
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 callingGetProcAddress
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
-
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"
-
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 callingGetProcAddress
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
Thanks. Regards, Paresh.
-
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"
-
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"
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
-
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"
-
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"
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 ...