Visual Basic(VBWpf)からC++ Dll(BVRelate.dll)から関数を呼び出す必要があります。VBからC++ DLL関数を呼び出す方法は?
C++ DLLコード:
//VBRelate.h
#ifdef VBRELATE_EXPORTS
#define VBRELATE_API __declspec(dllexport)
#else
#define VBRELATE_API __declspec(dllimport)
#endif
extern VBRELATE_API void DoSomething();
//VBRelate.cpp
#include <atlstr.h>
#include "VBRelate.h"
VBRELATE_API void DoSomething()
{
CString strOutput("Hello world");
MessageBox(NULL, strOutput, L"Got a message", MB_OK);
}
は、その後、私はVB(WPFプロジェクト)
Imports System.Runtime.InteropServices
Class MainWindow
Declare Function DoSomething Lib "M:\VBRelate.dll"()
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
DoSomething()
End Sub
End Class
からこの関数を呼び出そうと、私は例外が発生しました:
"MarshalDirectiveExceptionは、未処理でした" 。
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file M:\VBRelate.dll
File Type: DLL
Section contains the following exports for VBRelate.dll
00000000 characteristics
57E3DDA6 time date stamp Thu Sep 22 16:33:26 2016
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00011299 [email protected]@YAXXZ = @ILT+660([email protected]@YAXXZ)
Summary
1000 .00cfg
4000 .data
1000 .gfids
1000 .idata
4000 .rdata
1000 .reloc
1000 .rsrc
10000 .text
10000 .textbss
1000 .tls
:
dumpbin /exports "M:\VBRelate.dll">M:\VBRelate.txt
とVBRelate.txtには、このでした:型 'System.Runtime.InteropServices.MarshalDirectiveException' の未処理の例外は、その後、私はDUMPBINを使用VBWpf.exe
で発生しましたそれから、私はdefファイルを使用しようとしていましたが、それを使用する方法を完全には理解していませんでした(dllファイル、プロジェクトファイル、どこか他の場所)。そこで、defファイルをdllファイルとdllプロジェクトファイルで置きます。
; VBRelate.def - defines the exports for VBRelate.dll
LIBRARY VBRelate.dll
DESCRIPTION 'A C++ dll that can be called from VB'
EXPORTS
DoSomething
次に、私はdllを再構築しました。 動作しませんでした。同じ例外が発生しました。 dumpbinは同じダンプを返しましたが、何も変更されませんでした。
プログラム?このような単純な間違いについて教えてくれるでしょう。それは機能ではなく、サブです。 –
私はこのオプションをonxに切り替えました。私はDoSomething関数DoSomething Lib "M:\ VBRelate.dll"()をオブジェクトとして宣言し、Sub DoSomething Lib "M:\ VBRelate.dll"()も宣言しました。同じ問題 –
dll関数の宣言と定義にextern "C"を追加すると機能しました。 –