简体   繁体   English

使用动态链接从.dll库调用功能

[英]Calling fuctions from the .dll library using dynamic linking

Ok, so I', writting a C++ code for the MCP2221 converter. 好的,所以,我为MCP2221转换器编写了C ++代码。 In order to use it I try to implement the .dll file. 为了使用它,我尝试实现.dll文件。 Namely mcp2221_dll_m_dotnetv4_x86.dll which can be downloaded from: https://www.microchip.com/DevelopmentTools/ProductDetails/PartNo/ADM00559 即可以从以下地址下载mcp2221_dll_m_dotnetv4_x86.dllhttps : //www.microchip.com/DevelopmentTools/ProductDetails/PartNo/ADM00559

I've tried to implement it but I still cannot call the functions and I cannot find the reason why. 我已经尝试实现它,但是仍然不能调用函数,也找不到原因。

I use the following code: 我使用以下代码:

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string.h>

using namespace std;
typedef string(WINAPI *MYPROC)();

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

    // Get a handle to the DLL module.
    char a;

    hinstLib = LoadLibrary(TEXT("mcp2221_dll_m_dotnetv4_x86.dll"));

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

    if (hinstLib != NULL)
    {
        cout << "LIB\tloaded" << endl;

        MYPROC ProcAdd = (MYPROC)GetProcAddress(hinstLib, "M_Mcp2221_GetLibraryVersion");

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

        if (NULL != ProcAdd)
        {
            cout << "FUNC\tloaded" << endl;
            string s= ProcAdd();
            fRunTimeLinkSuccess = TRUE;
        }
        else
        {
            cout << "FUNC\tNOT loaded" << endl;
        }
        // Free the DLL module.

        fFreeResult = FreeLibrary(hinstLib);
    }
    else
    {
        cout << "LIB\tNOT loaded" << endl;
    }
    return 0;
}

The function is defined in the specification in the following way: 该功能通过以下方式在规范中定义:

String^ M_Mcp2221_GetLibraryVersion() 字符串^ M_Mcp2221_GetLibraryVersion()

Description: Returns the version number of the DLL Returns: String containing the library version Use Marshal.GetLastWin32Error() to determine the error code if the function fails. 说明:返回DLL的版本号返回:包含库版本的字符串如果函数失败,请使用Marshal.GetLastWin32Error()确定错误代码。

I still get the LIB loaded FUNC NOT loaded output. 我仍然得到LIB加载的FUNC NOT加载的输出。

Could anyone tell me what I keep doing wrong? 谁能告诉我我做错了什么? Will appreciate a lot. 将不胜感激。

Probably, you are using wrong dll and wrong function name. 可能是您使用了错误的dll和错误的函数名。

You are trying to load function from dll for .Net-managed code - MCP2221_DLL(v2.2.1)\\managed\\MCP2221DLL-M-dotNet4\\x86\\mcp2221_dll_m_dotnetv4_x86.dll . 您正在尝试从DLL加载.Net托管代码的功能MCP2221_DLL(v2.2.1)\\managed\\MCP2221DLL-M-dotNet4\\x86\\mcp2221_dll_m_dotnetv4_x86.dll With an utility that shows functions exported from dll, I haven't found any functions that are exported from this one. 有了一个显示从dll导出的功能的实用程序,我没有发现任何从dll导出的功能。

Try using unmanaged dll MCP2221_DLL(v2.2.1)\\unmanaged\\dll\\mcp2221_dll_um_x86.dll . 尝试使用非托管DLL MCP2221_DLL(v2.2.1)\\unmanaged\\dll\\mcp2221_dll_um_x86.dll It has function _Mcp2221_GetLibraryVersion@4 它具有函数_Mcp2221_GetLibraryVersion@4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM