平台叫用範例
下列範例示範如何在 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
如需其他範例,請參閱使用平台叫用封送處理資料。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應