2016-09-22 11 views
0

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は同じダンプを返しましたが、何も変更されませんでした。

+1

プログラム?このような単純な間違いについて教えてくれるでしょう。それは機能ではなく、サブです。 –

+0

私はこのオプションをonxに切り替えました。私はDoSomething関数DoSomething Lib "M:\ VBRelate.dll"()をオブジェクトとして宣言し、Sub DoSomething Lib "M:\ VBRelate.dll"()も宣言しました。同じ問題 –

+0

dll関数の宣言と定義にextern "C"を追加すると機能しました。 –

答えて

0

問題はDLL /ネイティブC++コードの中にあるようには見えません。例外は、管理(VB)との間でデータを扱うに問題があると言ってとアンマネージ(C++)コードされていますMarshalDirectiveException on MSDN

MarshalAsAttribute()msdn

+0

下線を引かないでください...どの属性をマーシャリングする必要がありますか?関数はvoid型で引数はありません... void DoSomething() –

+0

関数名に何か問題がありますか?ダンプ内のDoSomething @@ YAXXZ = @ ILT + 660(?DoSomething @@ YAXXZ) –

+1

dll関数の宣言と定義にextern "C"を追加すると動作しました。 –

0

を使用して属性を変更することも可能である

を解決しました

dll関数の宣言と定義にextern "C"を追加すると機能しました。

私はこの問題が装飾された名前であったと思います。 extern「C」を追加した後、ダンプファイルはこのように見えた:

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 
    57E52794 time date stamp Fri Sep 23 16:01:08 2016 
     0.00 version 
      1 ordinal base 
      1 number of functions 
      1 number of names 

    ordinal hint RVA  name 

      1 0 000112C1 DoSomething = @ILT+700(_DoSomething) 

    Summary 

     1000 .00cfg 
     4000 .data 
     1000 .gfids 
     1000 .idata 
     4000 .rdata 
     1000 .reloc 
     1000 .rsrc 
     10000 .text 
     10000 .textbss 
     1000 .tls 

ので、関数名は今ですが、それがあったのdoSomething @@ YAXXZコンパイラので、しばらくの間、オプション厳格にして

関連する問題