クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
Visual Studio 2005
Visual Studio ドキュメント
.NET Framework の拡張開発
相互運用性
 プラットフォーム呼び出しの例

  低帯域幅での表示をオンにする
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2005/.NET Framework 2.0

その他のバージョンについては、以下の情報を参照してください。
.NET Framework 開発者ガイド
プラットフォーム呼び出しの例

User32.dll 内の MessageBox 関数を定義し、引数として単純な文字列を渡して呼び出す方法の例を次に示します。この例では、対象となるプラットフォームが文字のバイト幅と文字列のマーシャリング方法を決定できるように、DllImportAttribute.CharSet Field フィールドを Auto に設定しています。

同じ内容の例を Visual Basic、C#、および C++ で示します。すべての例を表示するには、このページの左上隅にある [言語のフィルタ] ボタン をクリックしてください。その他の例については、「プラットフォーム呼び出しによるデータのマーシャリング」を参照してください。

Visual Basic
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
C#
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);
    }
}      
C++
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(void) {
     String* pText = L"Hello World!";
     String* pCaption = L"Platform Invoke Sample";
     MessageBox(0, pText, pCaption, 0);
}

参照

コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件  |  商標  |  プライバシー
Page view tracker