我正在尝试使用C ++控制MotorBee,
问题是我正在使用MotorBee“ mtb.dll”随附的dll文件

我正在尝试将dll中的函数加载到C ++程序中,如下所示:

#include "stdafx.h"
#include <iostream>
#include "mt.h"
#include "windows.h"
using namespace std;

HINSTANCE BeeHandle= LoadLibrary((LPCWSTR) "mtb.dll");
Type_InitMotoBee InitMotoBee;
Type_SetMotors SetMotors;
Type_Digital_IO Digital_IO;

int main() {
InitMotoBee = (Type_InitMotoBee)GetProcAddress( BeeHandle, " InitMotoBee");
SetMotors =(Type_SetMotors)GetProcAddress(BeeHandle, " SetMotors");
Digital_IO =(Type_Digital_IO)GetProcAddress(BeeHandle, " Digital_IO ");     InitMotoBee();
SetMotors(0, 50, 0, 0, 0, 0, 0, 0, 0);
      system("pause");
return 0;
}


我收到一条错误消息,说我正在尝试读取内存中的地址0x00000000,
当我尝试提示BeeHandle时,它显示0x0地址(试图检查句柄值)
样本错误:

First-chance exception at 0x00000000 in 111111.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x6148f2b4 in 111111.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x6148f2b4 in 111111.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x6148f2b4 in 111111.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x6148f2b4 in 111111.exe: 0xC0000005: Access violation reading location 0x00000000.


感谢您的帮助,

最佳答案

此演员表不正确:

HINSTANCE BeeHandle= LoadLibrary((LPCWSTR) "mtb.dll");


因为它将字符串文字转换为宽字符串文字。只需使用宽字符串文字:

HINSTANCE BeeHandle = LoadLibrary(L"mtb.dll");



检查LoadLibrary()的结果:
尝试使用返回的函数指针之前,请检查GetProcAddress()的结果。在每个字符串文字中都有一个前导空格(由于在注释中使用了Roger,因此在尾部也有一个空格),用于指定函数名称,然后将其删除。
如果LoadLibrary()GetProcAddress()失败,请使用GetLastError()获取失败原因。


代码摘要:

HINSTANCE BeeHandle = LoadLibrary(L"mtb.dll");
if (BeeHandle)
{
    SetMotors = (Type_SetMotors)GetProcAddress(BeeHandle, "SetMotors");
    if (SetMotors)
    {
        // Use 'SetMotors'.
    }
    else
    {
        std::cerr << "Failed to locate SetMotors(): " << GetLastError() << "\n";
    }
    FreeLibrary(BeeHandle);
}
else
{
    std::cerr << "Failed to load mtb.dll: " << GetLastError() << "\n";
}

07-28 03:02
查看更多