How to pass parameters to dll?

跟風遠走 提交于 2019-12-13 07:42:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!