VB.NET 2017で書いた クラスライブラリを C++ で読み込む実験

DllExport を使ってみたけどうまくいかなかった話
これに関連して、VB.NET で作ったクラスライブラリで実験してみた。

Public Class Class1

<ExportDllAttribute("ExportDemoFunction">
    Public Shared Function Func(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
        Return num1 + num2
    End Function
End Class

単純な足し算の関数 int ExportDemoFunction(int a,int b); 

1>------ すべてのリビルド開始: プロジェクト:ClassLibrary2, 構成: Debug x86 ------
1>  ClassLibrary2 -> ExportDll\ClassLibrary2\bin\x86\Debug\ClassLibrary2.dll
1>  Deassebly file with arguments '/nobar /linenum /out:ClassLibrary2.il ClassLibrary2.dll'
1>
1>  # GitHub/3F Edition specially for .NET DllExport: https://github.com/3F
1>    v4.700.2.0+4fde65a56
1>
1>
1>  Deleting ExportDllAttribute dependency.
1>  Found class: ClassLibrary2.My.MyApplication
1>  Found class: ClassLibrary2.My.MyComputer
1>  Found class: ClassLibrary2.My.MyProject
1>  Found class: ClassLibrary2.My.MyProject+MyWebServices
1>  Found class: ClassLibrary2.My.MyProject+T>
1>  Found class: ClassLibrary2.My.Resources.Resources
1>  Found class: ClassLibrary2.My.MySettings
1>  Found class: ClassLibrary2.My.MySettingsProperty
1>  Found class: ClassLibrary2.Class1
1>  Found method: .ctor
1>  Found method: Func
1>  	Changing calling convention: System.Runtime.CompilerServices.CallConvStdcall
1>  	Adding .vtentry:1 .export:ExportDemoFunction
1>  Compiling file with arguments '/nologo /out:ClassLibrary2.dll ClassLibrary2.il /DLL /resource=ClassLibrary2.res /Debug'
1>
1>  Assembling 'ClassLibrary2.il'  to DLL --> 'ClassLibrary2.dll'
1>  Source file is ANSI
1>
1>  Assembled method ClassLibrary2.My.MyApplication::.ctor
1>  Assembled method ClassLibrary2.My.MyComputer::.ctor
1>  Assembled method ClassLibrary2.My.MyProjectyWebServices::Equals
1>  Assembled method ClassLibrary2.My.MyProjectyWebServices::GetHashCode
1>  Assembled method ClassLibrary2.My.MyProjectyWebServices::GetType
1>  Assembled method ClassLibrary2.My.MyProjectyWebServices::ToString
1>  Assembled method ClassLibrary2.My.MyProjectyWebServices::Create__Instance__
1>  Assembled method ClassLibrary2.My.MyProjectyWebServices::Dispose__Instance__
1>  Assembled method ClassLibrary2.My.MyProjectyWebServices::.ctor
1>  Assembled method ClassLibrary2.My.MyProjecthreadSafeObjectProvider`1::get_GetInstance
1>  Assembled method ClassLibrary2.My.MyProjecthreadSafeObjectProvider`1::.ctor
1>  Assembled method ClassLibrary2.My.MyProject::.cctor
1>  Assembled method ClassLibrary2.My.MyProject::get_Computer
1>  Assembled method ClassLibrary2.My.MyProject::get_Application
1>  Assembled method ClassLibrary2.My.MyProject::get_User
1>  Assembled method ClassLibrary2.My.MyProject::get_WebServices
1>  Assembled method ClassLibrary2.My.Resources.Resources::get_ResourceManager
1>  Assembled method ClassLibrary2.My.Resources.Resources::get_Culture
1>  Assembled method ClassLibrary2.My.Resources.Resources::set_Culture
1>  Assembled method ClassLibrary2.My.MySettings::.cctor
1>  Assembled method ClassLibrary2.My.MySettings::.ctor
1>  Assembled method ClassLibrary2.My.MySettings::get_Default
1>  Assembled method ClassLibrary2.My.MySettingsProperty::get_Settings
1>  Assembled method ClassLibrary2.Class1::.ctor
1>  Assembled method ClassLibrary2.Class1::Func
1>  Creating PE file
1>
1>  Emitting classes:
1>  Class 1:	ClassLibrary2.My.MyApplication
1>  Class 2:	ClassLibrary2.My.MyComputer
1>  Class 3:	ClassLibrary2.My.MyProject
1>  Class 4:	ClassLibrary2.My.MyProjectyWebServices
1>  Class 5:	ClassLibrary2.My.MyProjecthreadSafeObjectProvider`1
1>  Class 6:	ClassLibrary2.My.Resources.Resources
1>  Class 7:	ClassLibrary2.My.MySettings
1>  Class 8:	ClassLibrary2.My.MySettingsProperty
1>  Class 9:	ClassLibrary2.Class1
1>
1>  Emitting fields and methods:
1>  Global
1>  Class 1	Methods: 1;
1>  Class 2	Methods: 1;
1>  Class 3	Fields: 4;	Methods: 5;
1>  Class 4	Methods: 7;
1>  Class 5	Fields: 1;	Methods: 2;
1>  Class 6	Fields: 2;	Methods: 3;
1>  Class 7	Fields: 1;	Methods: 3;
1>  Class 8	Methods: 1;
1>  Class 9	Methods: 2;
1>  Resolving local member refs: 27 -> 27 defs, 0 refs, 0 unresolved
1>
1>  Emitting events and properties:
1>  Global
1>  Class 1
1>  Class 2
1>  Class 3	Props: 4;
1>  Class 4
1>  Class 5	Props: 1;
1>  Class 6	Props: 2;
1>  Class 7	Props: 1;
1>  Class 8	Props: 1;
1>  Class 9
1>  Resolving local member refs: 0 -> 0 defs, 0 refs, 0 unresolved
1>  Writing PE file
1>  Operation completed successfully
1>
========== すべてリビルド: 1 正常終了、0 失敗、0 スキップ ==========

うまく動いてますね・ω・
How to Automate Exporting .NET Function to Unmanaged Programs - CodeProject

#include
#include
#include
int main()
{
  HINSTANCE hDll;
  char store[256];

  int ret;
  typedef int (WINAPI* ExportDemoFunction)(int, int);
  ExportDemoFunction exportDemoFunction;
  hDll =     LoadLibraryA("ClassLibrary2.dll");
  exportDemoFunction = (ExportDemoFunction)GetProcAddress(hDll, "ExportDemoFunction");
  ret = exportDemoFunction(123, 456);
  wsprintfA(store, "%d", ret);
  MessageBoxA(0, store, "", 0);
}

こんな感じ。
result
実行結果。いけました・ω・

Public Class Class1

<ExportDllAttribute("ExportDemoFunction">
    Public Shared Function Func(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
        InputBox(Str(num1) & "+" & Str(num2), "計算", Str(num1 + num2))
        Return num1 + num2
    End Function
End Class

こんな風にしても行ける

result2

おすすめ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です