0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語で作成したDLLをVisual Basic .NETで参照する

Posted at

WinFormsアプリケーションでDLLを参照する案件が稀にあり、都度試行錯誤するので忘れないようまとめる。

構造体を参照する

  • C言語側
/*
 * 構造体
 */
typdef struct
{
    int id;
    char name[50];
}Setting

/*
 * 情報を取得する
 * [引数]objSetting 構造体(呼び出し元で確保する)
 * [戻値]処理結果
 */
bool __stdcall GetSetting(Setting *objSetting);
  • Visual Basic .NET側
'''<summary>構造体(C言語側の定義)</summary>
<StructLayout(LayoutKind.Sequential)>
Public Structure Setting
    Public id As Integer
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)>
    Public name As String
End Function

'''<summary>情報を取得する(C言語側の定義)</summary>
<DllImport("xxx.dll")>
Public Shared Function GetSetting(ByRef objSetting As Setting) As Boolean
End Function

文字列を受け取る

  • C言語側
/*
 * 名前を取得する
 * [引数]name 名前(呼び出し元で確保する)
 * [戻値]処理結果
 */
bool __stdcall GetName(wchar_t **name);
  • Visual Basic .NET側
'''<summary>名前を取得する(C言語側の定義)</summary>
<DllImport("xxx.dll", CharSet:=CharSet.Unicode)>
Public Shared Function GetName(ByRef name As String) As Boolean
End Function
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?