次の例では、User32.dllで MessageBox 関数を定義して呼び出し、単純な文字列を引数として渡す方法を示します。 例では、ターゲット プラットフォームが文字幅と文字列マーシャリングを決定できるように、 DllImportAttribute.CharSet フィールドが Auto に設定されています。
using namespace System::Runtime::InteropServices;
typedef void* HWND;
[DllImport("user32", CharSet=CharSet::Auto)]
extern "C" IntPtr MessageBox(HWND hWnd,
String* pText,
String* pCaption,
unsigned int uType);
void main()
{
String* pText = L"Hello World!";
String* pCaption = L"Platform Invoke Sample";
MessageBox(0, pText, pCaption, 0);
}
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr MessageBox(int hWnd, String text,
String caption, uint type);
}
public class HelloWorld {
public static void Main() {
Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0);
}
}
Imports System.Runtime.InteropServices
Public Class Win32
Declare Auto Function MessageBox Lib "user32.dll" _
(ByVal hWnd As Integer, ByVal txt As String, _
ByVal caption As String, ByVal Typ As Integer) As IntPtr
End Class
Public Class HelloWorld
Public Shared Sub Main()
Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0)
End Sub
End Class
その他の例については、「 プラットフォーム呼び出しを使用したデータのマーシャリング」を参照してください。
こちらも参照ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET