FARPROC GetProcAddress(hinst, lpszProcName) | |||||
HINSTANCE hinst; | /* handle of module, */ | ||||
LPCSTR lpszProcName; | /* address of function | */ |
The GetProcAddress function retrieves the address of the given module function.
hinst
Identifies the module that contains the function.
lpszProcName
Points to a null-terminated string containing the function name, or specifies the ordinal value of the function. If it is an ordinal value, the value must be in the low-order word and the high-order word must be zero.
The return value is the address of the module function's entry point if the GetProcAddress function is successful. Otherwise, it is NULL.
If the lpszProcName parameter is an ordinal value and a function with the specified ordinal does not exist in the module, GetProcAddress can still return a non-NULL value. In cases where the function may not exist, specify the function by name rather than ordinal value.
Use the GetProcAddress function to retrieve addresses of exported functions in dynamic-link libraries (DLLs). The MakeProcInstance function can be used to access functions within different instances of the current module.
The spelling of the function name (pointed to by the lpszProcName parameter) must be identical to the spelling as it appears in the EXPORTS section of the source DLL's module-definition (.DEF) file.
The following example uses the GetProcAddress function to retrieve the address of the TimerCount function in TOOLHELP.DLL:
char szBuf[80];
TIMERINFO timerinfo;
HINSTANCE hinstToolHelp;
BOOL (FAR *lpfnTimerCount) (TIMERINFO FAR*);
/* Turn off the "File not found" error box. */
SetErrorMode(SEM_NOOPENFILEERRORBOX);
/* Load the TOOLHELP.DLL library module. */
hinstToolHelp = LoadLibrary("TOOLHELP.DLL");
if (hinstToolHelp > HINSTANCE_ERROR) { /* loaded successfully */
/* Retrieve the address of the TimerCount function. */
(FARPROC) lpfnTimerCount =
GetProcAddress(hinstToolHelp, "TimerCount");
if (lpfnTimerCount != NULL) {
/* Call the TimerCount function. */
timerinfo.dwSize = sizeof(TIMERINFO);
if ((*lpfnTimerCount) ((TIMERINFO FAR *) &timerinfo)) {
sprintf(szBuf, "task: %lu seconds\nVM: %lu seconds",
timerinfo.dwmsSinceStart / 1000,
timerinfo.dwmsThisVM / 1000);
}
else {
strcpy(szBuf, "TimerCount failed");
}
}
else {
strcpy(szBuf, "GetProcAddress failed");
}
/* Free the TOOLHELP.DLL library module. */
FreeLibrary(hinstToolHelp);
}
else {
strcpy(szBuf, "LoadLibrary failed");
}
MessageBox(NULL, szBuf, "Library Functions", MB_ICONHAND);