2013-01-23 120 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

哎呀,我的坏。我想我应该学会阅读。嗯。我应该折腾这个答案,还是离开它,因为它包含一个欢迎? –

相关问题