[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);
[C++, JScript] No example is available in C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button in the upper-left corner of the page.
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);
[C++, JScript] No example is available in C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button in the upper-left corner of the page.
.NET Framework Developer's Guide Passing Structures [C#]See Also
Calling a DLL Function | StructLayoutAttribute Class | StructLayoutAttribute Class | FieldOffsetAttribute Class
语言
C#Visual Basic全部显示
Many unmanaged DLL functions expect you to pass, as a parameter to the function, members of structures (user-defined types in Visual Basic) or members of classes that are defined in managed code. When using platform invoke to pass a structure or class members, you must provide additional information to format the type, preserving its original layout and alignment.This topic introduces the StructLayoutAttribute Class, which you use to define formatted types. In managed code, a formatted type is a structure or class member annotated with the StructLayoutAttribute to ensure predictable layout information to its members.Member layout of formatted types can be defined as automatic, sequential, or explicit. The following table describes each layout option.Layout Option Description
LayoutKind.Automatic Enables the runtime to reorder type members for efficiency.
CAUTION Never use this option to call unmanaged DLL functions through platform invoke.
LayoutKind.Explicit Orders type members according to the FieldOffset attribute applied to each field.
LayoutKind.Sequential Orders type members in unmanaged memory as they appear in the managed type definition. For reference information on these attributes, see the StructLayoutAttribute Class and FieldOffsetAttribute Class.Passing Structure Members
The following example shows how to define the Point and Rect types in managed code, and pass these types as arguments to the PtInRect function in the User32.dll file. PtInRect has the following unmanaged signature:BOOL PtInRect(const RECT *lprc, POINT pt);
Notice that you must pass the Rect structure by reference, since the function expects a pointer to a RECT type.[Visual Basic]
Imports System.Runtime.InteropServices<StructLayout(LayoutKind.Sequential)> Public Structure Point
Public x As Integer
Public y As Integer
End StructurePublic Structure <StructLayout(LayoutKind.Explicit)> Rect
Public <FieldOffset(0)> left As Integer
Public <FieldOffset(4)> top As Integer
Public <FieldOffset(8)> right As Integer
Public <FieldOffset(12)> bottom As Integer
End StructureClass Win32API
Declare Auto Function PtInRect Lib "user32.dll" _
(ByRef r As Rect, p As Point) As Boolean
End Class
[C#]
using System.Runtime.InteropServices;[StructLayout(LayoutKind.Sequential)]
public struct Point {
public int x;
public int y;
} [StructLayout(LayoutKind.Explicit]
public struct Rect {
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
} class Win32API {
[DllImport("User32.dll")]
public static extern Bool PtInRect(ref Rect r, Point p);
}
Passing Class Members
You can pass members of a class to an unmanaged DLL function, as long as the class has a fixed member layout. The following example demonstrates how to pass members of the MySystemTime class, which are defined in sequential order, to the GetSystemTime in the User32.dll file. GetSystemTime has the following unmanaged signature:void GetSystemTime(SYSTEMTIME* SystemTime);
Unlike value types, classes are always passed by reference.[Visual Basic]
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic<StructLayout(LayoutKind.Sequential)> Public Class MySystemTime
Public wYear As Short
Public wMonth As Short
Public wDayOfWeek As Short
Public wDay As Short
Public wHour As Short
Public wMinute As Short
Public wSecond As Short
Public wMiliseconds As Short
End ClassPublic Class Win32
Declare Auto Sub GetSystemTime Lib "Kernel32.dll"(ByRef sysTime _
As MySystemTime)
Declare Auto Function MessageBox Lib "User32.dll"(hWnd As Integer, _
txt As String, caption As String, Typ As Integer) As Integer
End ClassPublic Class TestPlatformInvoke
Public Shared Sub Main()
Dim sysTime As New MySystemTime()
Win32.GetSystemTime(sysTime) Dim dt As String
dt = "System time is:" & ControlChars.CrLf & _
"Year: " & sysTime.wYear & _
ControlChars.CrLf & "Month: " & sysTime.wMonth & _
ControlChars.CrLf & "DayOfWeek: " & sysTime.wDayOfWeek & _
ControlChars.CrLf & "Day: " & sysTime.wDay
Win32.MessageBox(0, dt, "Platform Invoke Sample", 0)
End Sub
End Class
[C#]
[StructLayout(LayoutKind.Sequential)]
public class MySystemTime {
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
class Win32API {
[DllImport("User32.dll")]
public static extern void GetSystemTime(MySystemTime st);
}
See Also
Calling a DLL Function | StructLayoutAttribute Class | StructLayoutAttribute Class | FieldOffsetAttribute Class--------------------------------------------------------------------------------向 Visual Studio 发送反馈
using System;
using System.Runtime.InteropServices;class test
{
[DllImport("user32.dll")]
public static extern int MessageBox(
int hWnd, // handle to owner window
string lpText, // text in message box
string lpCaption, // message box title
int uType // message box style
); static void Main()
{
MessageBox(0,"OK!","Test",0);
}
}