2013-01-23 174 views
0

我真的需要你的幫助,因爲我在將一個.DLL函數集成到我的控制檯應用程序中時遇到了很多問題。這個.DLL包含獲取2個字符(Char a和Char b) (輸入)並將它們添加到一個字符中。例如: 字符A = H 字符B = I 輸出= HI 但問題出在這裏。當我編譯控制檯應用程序並運行它時,它說函數沒有被檢測到。這裏是我的問題。爲什麼地獄不找到函數,即使在.def文件中我已經列出了LIBRARY和唯一的導出函數?請幫助我。 這是.DLL SOURCE無法通過控制檯應用程序調用.DLL函數

 #include "stdafx.h" 
    char concatenazione(char a,char b) 
    { 
    return a+b; 
    } 

**THIS IS THE .DEF FILE OF THE .DLL** 

    LIBRARY Concatenazione 
    EXPORTS 
    concatenazione @1 

**And this is the dllmain.cpp** 


#include "stdafx.h" 

BOOL APIENTRY DllMain(HMODULE hModule, 
         DWORD ul_reason_for_call, 
         LPVOID lpReserved) 
{ 
    switch (ul_reason_for_call) 
    { 
    case DLL_PROCESS_ATTACH: 
    case DLL_THREAD_ATTACH: 
    case DLL_THREAD_DETACH: 
    case DLL_PROCESS_DETACH: 
     break; 
    } 
    return TRUE; 
} 
**This is the Console Application partial source(For now it just includes the functions import part)** 

#include <iostream> 
#include <windows.h> 
#include <stdio.h> 

typedef int (__cdecl *MYPROC)(LPWSTR); 

int main(void) 
{ 
    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

    // Get a handle to the DLL module. 

    hinstLib = LoadLibrary(TEXT("C:\\ConcatenazioneDiAyoub.dll")); 

    // If the handle is valid, try to get the function address. 

    if (hinstLib != NULL) 
    { 
     ProcAdd = (MYPROC) GetProcAddress(hinstLib, "concatenazione"); 

     // If the function address is valid, call the function. 

     if (NULL != ProcAdd) 
     { 
      fRunTimeLinkSuccess = TRUE; 
      (ProcAdd) (L"Message sent to the DLL function\n"); 
     } 
     // Free the DLL module. 

     fFreeResult = FreeLibrary(hinstLib); 
    } 

    // If unable to call the DLL function, use an alternative. 
    if (! fRunTimeLinkSuccess) 
     printf("Message printed from executable\n"); 

    return 0; 

} 
**The Console Applications runs fine but the output is "Message printed from executable".This means that the function hasn't been detected.** 
+0

我會告訴你[閱讀此](http://msdn.microsoft.com/en-us/library/z4zxe9k8(v = vs.80).aspx),然後在你的代碼中加入更好的錯誤處理和報告,以確定是加載DLL失敗還是找不到發生的proc失敗 – WhozCraig

+0

你可能想將DLL的路徑添加到PATH環境變量PRI或運行到嘗試使用DLL的應用程序。 – alk

+0

現在它不會給我那個消息 – NTAuthority

回答

0

好吧,我看着你有什麼上面,不知道哪裏在你的代碼導入的dll。我對這件事很感興趣,所以我找到了this article關於如何做到這一點。

如果我理解正確的話,在你的控制檯應用程序,你需要有一個像線(假設你的DLL的名稱是「your.dll」:

[DllImport("your.Dll")] 

的方式,歡迎堆棧溢出,我注意到這是你的第一天!CHEERS!

+0

我已經使用LoadLibrary命令加載.DLL ... btw..Thanks歡迎:) – NTAuthority

+0

注意:這不是一個C#控制檯應用程序;這是C. – WhozCraig

+0

哎呀,我的壞。我想我應該學會閱讀。嗯。我應該折騰這個答案,還是離開它,因爲它包含一個歡迎? –

相關問題