2012-10-16 25 views
-1

參數,我發現在MSDN上這個例子中,我試圖使用它,但我有得到它的問題與參數合作(http://msdn.microsoft.com/en-us/library/ms686944(VS.85).aspx動態調用外部DLL與CPP

下面是我想要的代碼使用和我試圖調用的方法有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; 
} 

任何想法我在做什麼錯?提前致謝!!!

+3

什麼不成?我懷疑調用LoadLibrary()作爲反斜槓不會在DLL的路徑中轉義。 – hmjd

+0

你的代碼在哪裏失敗? GetProcAddress是否返回NULL? – Abhineet

+0

我得到一個編譯錯誤C2197:'MYPROC':太多的調用參數 – spyter

回答

2

我現在的工作。我缺少的類型定義的額外參數定義:

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

'typedef'不在發佈的答案代碼中,因此任何人都無法協助。至少你找到了它! – hmjd

+0

恭喜,但您應該在發佈之前檢查整個代碼。 – Abhineet