问题
I have this function in DLL:
CPPLIBRARY_API int fnCPPLibrary(int a, int b)
{
return a + b;
}
Main function:
int main(){
FARPROC myCppProc;
HINSTANCE hDll;
DWORD L;
int result;
hDll = LoadLibrary("CPPLibrary");
if (hDll != NULL){
myCppProc = GetProcAddress(hDll, "fnCPPLibrary");
if (myCppProc != NULL){
result = myCppProc();
cout <<"Result from library: " <<result;
int a;
}
}
}
I can easly call fnCPPLibrary when it doesn't have arguments but how to pass params from program to that dll function? Is it simple or it requires some complicated code?
回答1:
The function pointer must have a signature that matches the function you're calling.
Not knowing what CPPLIBRARY_API
is:
typedef int (*DLLFunc)(int, int);
DLLFunc myCppProc;
//...
myCppProc = (DLLFunc)GetProcAddress(hDll, "fnCPPLibrary"); // Cast to function pointer
myCppProc(1, 2); // call function
I don't know what the qualifier CPPLIBRARY_API consists of, and it may be important (could be a calling convention -- you have to post this information). But in general, this is how you declare a function pointer and use GetProcAddress
.
来源:https://stackoverflow.com/questions/27361479/how-to-pass-parameters-to-dll