【发布时间】:2018-07-10 12:35:15
【问题描述】:
好的,我为 MCP2221 转换器编写 C++ 代码。为了使用它,我尝试实现 .dll 文件。即mcp2221_dll_m_dotnetv4_x86.dll,可以从以下位置下载: https://www.microchip.com/DevelopmentTools/ProductDetails/PartNo/ADM00559
我已经尝试实现了,但是还是调用不了函数,也找不到原因。
我使用以下代码:
#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;
}
该功能在规范中是这样定义的:
字符串^ M_Mcp2221_GetLibraryVersion()
说明:返回DLL的版本号 返回:包含库版本的字符串 如果函数失败,使用 Marshal.GetLastWin32Error() 确定错误代码。
我仍然加载了 LIB FUNC 未加载输出。
谁能告诉我我一直做错什么?会很感激的。
【问题讨论】:
-
您正在加载函数
M_Mcp2221_GetLastError,并且在问题中您提到了函数M_Mcp2221_GetLibraryVersion()。他们有不同的名字 -
抱歉在这里粘贴了错误的代码。现在修复它。
-
你从哪里得到的函数名?
-
来自 pdf 文件。
标签: c++ windows dll dynamic-linking handle