Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Run-Time Dynamic Linking

Status
Not open for further replies.

kshea

Technical User
May 31, 2002
60
CA
Hi All:

I'm trying to use Run-Time Dynamic Linking. I defined a function type and a function variable. Then I load the library into memory, and set up the above function for run time loading, like the following:
//Function example: int count()

typedef int (*FUNC)()
...
HINSTANCE hinstLib;
hinstLib=LoadLibrary("mydll.dll");
FUNC func1;
if(hinstLib!=NULL)
{
func1=(func1type)GetProcAddress(hinstLib,"func1");
};

But if the function is an interface function, such as
STDMETHOD(Count)(/*[out]*/long *plCount)..., I used

typedef HRESULT (*FUNC)(Long); and got the error message that the specified procedure could not be found.

Does anyone have a clue to define this kind of function?

Thanks in advance.

Kay
 
I don't think it's a problem with the typedef definition - it would give a different error.

The error message means that GetProcAddress cannot find the function name you're passing in - check the dll's exported functions to ensure you've got the right function name.
 
An example: Function gethostbyaddr() is in DLL ws2_32.dll. To dynamic link it, You can use that:

typedef HOSTENT* (__stdcall *PFNGETHOSTBYADDR)(char*, int, int);

PFNGETHOSTBYADDR pgethostbyaddr = NULL;
HINSTANCE hinst = LoadLibrary("ws2_32.dll");
if(hinst) {
//Sockets 2 - import
pgethostbyaddr = (PFNGETHOSTBYADDR)GetProcAddress(hinst, "gethostbyaddr");
if(pgethostbyaddr) {
//Call it
HOSTENT *pHostEnt= pgethostbyaddr( ...'Your arguments'...);
}
FreeLibrary(hinst);
}
 
Thank you both. [Temps] sir, you are right.
Helpful example, tchouch.

kay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top