0

C++ を使用して MotorBee を制御しようとしています。問題は、MotorBee に付属の dll ファイル「mtb.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.

お手伝いありがとう、

4

1 に答える 1

2

このキャストは正しくありません:

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

文字列リテラルをワイド文字列リテラルにキャストするためです。ワイド文字列リテラルを使用するだけです:

HINSTANCE BeeHandle = LoadLibrary(L"mtb.dll");
  • の結果を確認LoadLibrary():
  • GetProcAddress()返された関数ポインターを使用する前に、の結果を確認してください。関数名を指定するために使用される文字列リテラルのそれぞれに先頭のスペース (およびコメントのロジャーのおかげで末尾のスペースもあります) があり、それらを削除します。
  • 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";
}
于 2013-04-09T13:04:06.127 に答える