>> . Can I call DLL functions that have different return types and parameter lists? <<
Realistically, you need to know the return type, calling convention and parameter list at compile time.
>> After I use GetProcAddress(), how do I call the function? <<
Here is an example of a function that uses dynamic loading of functions:
Code:
BOOL EnableProcessPrivilege(LPCTSTR lpPrivilege)
{
/* This function enables a privilege for the current process */
/* lpPrivilege can be either a privilege constant eg. SE_SHUTDOWN_NAME
* which is DEFINEd in the windows headers to the string
* "SeShutdownPrivilege" or the string can be used directly. */
typedef BOOL (WINAPI * fnptr_openprocesstoken)(HANDLE,DWORD,PHANDLE);
typedef BOOL (WINAPI * fnptr_adjusttokenprivileges)(HANDLE,BOOL,PTOKEN_PRIVILEGES,DWORD,PTOKEN_PRIVILEGES,PDWORD);
typedef BOOL (WINAPI * fnptr_lookupprivilegevalue)(LPCTSTR,LPCTSTR,PLUID);
TOKEN_PRIVILEGES tp = { 0 };
LUID luid = { 0 };
HANDLE hToken = NULL;
HMODULE hAdvapi = NULL;
if (!(hAdvapi = LoadLibrary(TEXT("Advapi32.dll"))))
{
return FALSE;
}
fnptr_openprocesstoken openprocesstoken = (fnptr_openprocesstoken) GetProcAddress(hAdvapi, "OpenProcessToken");
fnptr_adjusttokenprivileges adjusttokenprivileges = (fnptr_adjusttokenprivileges) GetProcAddress(hAdvapi, "AdjustTokenPrivileges");
fnptr_lookupprivilegevalue lookupprivilegevalue = (fnptr_lookupprivilegevalue) GetProcAddress(hAdvapi, "LookupPrivilegeValueA");
if (!openprocesstoken || !adjusttokenprivileges || !lookupprivilegevalue)
{
FreeLibrary(hAdvapi);
return FALSE;
}
/* Get the local id of our desired privilege */
if (!lookupprivilegevalue(NULL, lpPrivilege, &luid))
{
FreeLibrary(hAdvapi);
return FALSE;
}
/* Fill in TOKEN_PRIVILEGES structure */
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
/* Get the access token for the current process */
if (!openprocesstoken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
FreeLibrary(hAdvapi);
return FALSE;
}
/* Enable the privilege */
if (!adjusttokenprivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
{
FreeLibrary(hAdvapi);
CloseHandle(hToken);
return FALSE;
}
FreeLibrary(hAdvapi);
CloseHandle(hToken);
return TRUE;
}
Note, the syntax of the typedefs. See http://www.function-pointer.org/ for more details.