无法从控制台应用程序调用.DLL函数

时间:2013-01-23 07:36:32

标签: c dll import

我真的需要你的帮助,因为我在将一个.DLL函数集成到我的控制台应用程序中遇到了很多问题。这个.DLL包括获取2个字符(字符a和字符b)(输入)并将它们添加到一个字符。例如:     字符A = H.     Char B = I     输出= HI     但这就是问题所在。当我编译控制台应用程序时,当我运行它时,它表示该函数尚未被检测到。这是我的问题。为什么即使在.def中它也找不到该函数文件我列出了LIBRARY和唯一导出的函数?请帮帮我。     这是.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.**

1 个答案:

答案 0 :(得分:0)

好的,我查看了上面的内容,并想知道在代码中导入dll的位置。我自己也很陌生,所以我找到了this article如何做到这一点。

如果我理解正确,在您的控制台应用程序中,您需要有一行(假设您的dll的名称是“your.dll”:

[DllImport("your.Dll")]
顺便说一句,欢迎来到Stack Overflow,我注意到这是你的第一天!干杯!