用CPP中的参数动态调用外部DLL

calling an external dll dynamically with parameters in cpp

本文关键字:调用 外部 DLL 动态 参数 CPP      更新时间:2023-10-16

我在MSDN上找到了此示例,我正在尝试使用它,但是我有问题使其与参数一起使用(http://msdn.microsoft.com/en-us/Library/MS686944(vs.85).aspx)

以下是我要使用的代码,我试图调用的方法具有4个参数(cstring a,cstring b,cstring c,bool d)。

extern "C" __declspec(dllexport) int __stdcall 
    ImportFile(CString a, CString b, CString c, BOOL d)
{
    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
    // Get a handle to the DLL module.
    hinstLib = LoadLibrary(TEXT("C:MyDll.dll")); 
    // If the handle is valid, try to get the function address.
    if (hinstLib != NULL) 
    { 
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "TestFunction"); 
        // If the function address is valid, call the function.
        if (NULL != ProcAdd) 
        {
            fRunTimeLinkSuccess = TRUE;
            (ProcAdd) (a, b, c, d); 
        }
        // Free the DLL module.
        fFreeResult = FreeLibrary(hinstLib); 
    } 
    // If unable to call the DLL function
    if (!fRunTimeLinkSuccess) 
        return -1;
    return 0;
}

有什么想法我做错了什么?预先感谢!

我现在正常工作..我缺少定义的typedef中的额外参数:

typedef int (__cdecl *MYPROC)(CString a, CString b, CString c, BOOL d);