[英]MotorBee dll and c++, memory access violation
我試圖用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,當我嘗試cout 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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.