UnmanagedType 枚举
指定如何将参数或字段封送到非托管代码。
命名空间: System.Runtime.InteropServices
程序集: mscorlib(在 mscorlib.dll 中)
[ComVisibleAttribute(true)]
public enum UnmanagedType
成员名称 | 说明 |
---|---|
Bool | 4 字节布尔值(true != 0、false = 0)。这是 Win32 BOOL 类型。 |
I1 | bool(true = 1、false = 0)。 |
U1 | 1 字节无符号整数。 |
I2 | 2 字节带符号整数。 |
U2 | 2 字节无符号整数。 |
I4 | 4 字节有符号整数。 |
U4 | 4 字节无符号整数。 |
I8 | 8 字节有符号整数。 |
U8 | 8 字节无符号整数。 |
R4 | 4 字节浮点数。 |
R8 | 8 字节浮点数。 |
Currency | 在 System.Decimal 上使用,以将十进制数值作为 COM 货币类型而不是 Decimal 封送。 |
LPStr | 单字节、空终止的 ANSI 字符串。可在 System.String 或 System.Text.StringBuilder 数据类型上使用此成员。 |
LPWStr | 一个 2 字节、空终止的 Unicode 字符串。 请注意,如果非托管字符串不是使用非托管的 CoTaskMemAlloc 函数创建的,则不能在此非托管字符串中使用 LPWStr 值。 |
LPTStr | 与平台相关的字符串:在 Windows 98 上为 ANSI,在 Windows NT 和 Windows XP 上为 Unicode。该值仅对平台调用受支持,而对 COM 互操作则不受支持,原因是不支持导出 LPTStr 类型的字符串。 |
ByValTStr | 用于在结构中出现的内联定长字符数组。与 ByValTStr 一起使用的字符类型由应用于包含结构的 System.Runtime.InteropServices.StructLayoutAttribute 的System.Runtime.InteropServices.CharSet 参数确定。应始终使用 MarshalAsAttribute.SizeConst 字段来指示数组的大小。 .NET Framework 的 ByValTStr 类型的行为类似于结构中的 C 样式、固定大小的字符串(例如,char s[5])。托管代码中的行为与 Microsoft Visual Basic 6.0 中的行为不同,后者不是空终止(例如,MyString As String * 5)。 |
IUnknown | COMIUnknown 指针。可以在 Object 数据类型上使用此成员。 |
Struct | 一个用于封送托管格式化类和值类型的 VARIANT。 |
ByValArray | 当 MarshalAsAttribute.Value 设置为 ByValArray 时,必须设置 SizeConst 以指示数组中的元素数。当需要区分字符串类型时,ArraySubType 字段可以选择包含数组元素的 UnmanagedType。此 UnmanagedType 只可用于作为结构中的字段的数组。 |
SysInt | 与平台相关的有符号整数。在 32 位 Windows 上为 4 字节,在 64 位 Windows 上为 8 字节。 |
SysUInt | 与平台相关的无符号整数。在 32 位 Windows 上为 4 字节,在 64 位 Windows 上为 8 字节。 |
FunctionPtr | 一个可用作 C 样式函数指针的整数。可将此成员用于 Delegate 数据类型或从 Delegate 继承的类型。 |
AsAny | 一个动态类型,将在运行时确定对象的类型,并将该对象作为所确定的类型进行封送处理。仅对平台调用方法有效。 |
LPArray | 指向 C 样式数组的第一个元素的指针。当从托管到非托管进行封送处理时,该数组的长度由托管数组的长度确定。当从非托管到托管进行封送处理时,将根据 MarshalAsAttribute.SizeConst 和 MarshalAsAttribute.SizeParamIndex 字段确定该数组的长度,当需要区分字符串类型时,还可以后跟数组中元素的非托管类型。 |
LPStruct | 一个指针,它指向用于封送托管格式化类的 C 样式结构。仅对平台调用方法有效。 |
Error | 此与 I4 或 U4 关联的本机类型将导致参数作为导出类型库中的 HRESULT 导出。 |
BStr | 长度前缀为双字节的 Unicode 字符串。可以在 String 数据类型上使用此成员(它是 COM 中的默认字符串)。 |
IDispatch | 一个 COM IDispatch 指针(在 Microsoft Visual Basic 6.0 中为 Object)。 |
Interface | COM 接口指针。从类元数据获得接口的 Guid。如果将此成员应用于类,则可以使用该成员指定确切的接口类型或默认的接口类型。当应用于 Object 数据类型时,此成员将产生 UnmanagedType.IUnknown 行为。 |
SafeArray | SafeArray 是自我描述的数组,它带有关联数组数据的类型、秩和界限。 |
VBByRefStr | 允许 Visual Basic 2005 在非托管代码中更改字符串,并将结果在托管代码中反映出来。该值仅对平台调用受支持。 |
AnsiBStr | 长度前缀为单字节的 ANSI 字符串。可以在 String 数据类型上使用此成员。 |
TBStr | 一个有长度前缀的与平台相关的 char 字符串。在 Windows 98 上为 ANSI,在 Windows NT 上为 Unicode。很少用到这个类似于 BSTR 的成员。 |
VariantBool | 2 字节、OLE 定义的 VARIANT_BOOL 类型(true = -1、false = 0)。 |
CustomMarshaler | 当与 MarshalAsAttribute.MarshalType 或 MarshalAsAttribute.MarshalTypeRef 一起使用时,指定自定义封送拆收器类。MarshalAsAttribute.MarshalCookie 字段可用于将附加信息传递给自定义封送拆收器。可以在任何引用类型上使用此成员。 |
using System;
using System.Runtime.InteropServices;
using System.Reflection;
public class HHInterop
{
// Constants
const int HH_MAX_TABS = 19; // maximum number of tabs
// commands
protected const int HH_DISPLAY_TOPIC = 0x0000;
protected const int HH_SET_WIN_TYPE = 0x0004; // [Use HtmlHelp_SetWinType()]
protected const int HH_GET_WIN_TYPE = 0x0005; //
// parameter info used with HH_WINTYPE struct
public const int HHWIN_PARAM_PROPERTIES = (1 << 1); // valid fsWinProperties
public const int HHWIN_PARAM_STYLES = (1 << 2); // valid dwStyles
public const int HHWIN_PARAM_EXSTYLES = (1 << 3); // valid dwExStyles
public const int HHWIN_PARAM_RECT = (1 << 4); // valid rcWindowPos
public const int HHWIN_PARAM_NAV_WIDTH = (1 << 5); // valid iNavWidth
public const int HHWIN_PARAM_SHOWSTATE = (1 << 6); // valid nShowState
public const int HHWIN_PARAM_INFOTYPES = (1 << 7); // valid apInfoTypes
public const int HHWIN_PARAM_TB_FLAGS = (1 << 8); // valid fsToolBarFlags
public const int HHWIN_PARAM_EXPANSION = (1 << 9); // valid fNotExpanded
public const int HHWIN_PARAM_TABPOS = (1 << 10); // valid tabpos
public const int HHWIN_PARAM_TABORDER = (1 << 11); // valid taborder
public const int HHWIN_PARAM_HISTORY_COUNT = (1 << 12); // valid cHistory
public const int HHWIN_PARAM_CUR_TAB = (1 << 13); // valid curNavType
// property values used with HH_WINTYPE struct
public const int HHWIN_PROP_TAB_AUTOHIDESHOW = (1 << 0); // Automatically hide/show tri-pane window
public const int HHWIN_PROP_ONTOP = (1 << 1); // Topmost window
public const int HHWIN_PROP_NOTITLEBAR = (1 << 2); // no title bar
public const int HHWIN_PROP_NODEF_STYLES = (1 << 3); // no default window styles (only HH_WINTYPE.dwStyles)
public const int HHWIN_PROP_NODEF_EXSTYLES = (1 << 4); // no default extended window styles (only HH_WINTYPE.dwExStyles)
public const int HHWIN_PROP_TRI_PANE = (1 << 5); // use a tri-pane window
public const int HHWIN_PROP_NOTB_TEXT = (1 << 6); // no text on toolbar buttons
public const int HHWIN_PROP_POST_QUIT = (1 << 7); // post WM_QUIT message when window closes
public const int HHWIN_PROP_AUTO_SYNC = (1 << 8); // automatically ssync contents and index
public const int HHWIN_PROP_TRACKING = (1 << 9); // send tracking notification messages
public const int HHWIN_PROP_TAB_SEARCH = (1 << 10); // include search tab in navigation pane
public const int HHWIN_PROP_TAB_HISTORY = (1 << 11); // include history tab in navigation pane
public const int HHWIN_PROP_TAB_FAVORITES = (1 << 12); // include favorites tab in navigation pane
public const int HHWIN_PROP_CHANGE_TITLE = (1 << 13); // Put current HTML title in title bar
public const int HHWIN_PROP_NAV_ONLY_WIN = (1 << 14); // Only display the navigation window
public const int HHWIN_PROP_NO_TOOLBAR = (1 << 15); // Don’t display a toolbar
public const int HHWIN_PROP_MENU = (1 << 16); // Menu
public const int HHWIN_PROP_TAB_ADVSEARCH = (1 << 17); // Advanced FTS UI.
public const int HHWIN_PROP_USER_POS = (1 << 18); // After initial creation, user controls window size/position
public const int HHWIN_PROP_TAB_CUSTOM1 = (1 << 19); // Use custom tab #1
public const int HHWIN_PROP_TAB_CUSTOM2 = (1 << 20); // Use custom tab #2
public const int HHWIN_PROP_TAB_CUSTOM3 = (1 << 21); // Use custom tab #3
public const int HHWIN_PROP_TAB_CUSTOM4 = (1 << 22); // Use custom tab #4
public const int HHWIN_PROP_TAB_CUSTOM5 = (1 << 23); // Use custom tab #5
public const int HHWIN_PROP_TAB_CUSTOM6 = (1 << 24); // Use custom tab #6
public const int HHWIN_PROP_TAB_CUSTOM7 = (1 << 25); // Use custom tab #7
public const int HHWIN_PROP_TAB_CUSTOM8 = (1 << 26); // Use custom tab #8
public const int HHWIN_PROP_TAB_CUSTOM9 = (1 << 27); // Use custom tab #9
public const int HHWIN_TB_MARGIN = (1 << 28); // the window type has a margin
public const int HHWIN_BUTTON_EXPAND = (1 << 1); // Expand/contract button
public const int HHWIN_BUTTON_BACK = (1 << 2); // Back button
public const int HHWIN_BUTTON_FORWARD = (1 << 3); // Forward button
public const int HHWIN_BUTTON_STOP = (1 << 4); // Stop button
public const int HHWIN_BUTTON_REFRESH = (1 << 5); // Refresh button
public const int HHWIN_BUTTON_HOME = (1 << 6); // Home button
public const int HHWIN_BUTTON_SYNC = (1 << 11); // Sync button
public const int HHWIN_BUTTON_OPTIONS = (1 << 12); // Options button
public const int HHWIN_BUTTON_PRINT = (1 << 13); // Print button
public const int HHWIN_BUTTON_JUMP1 = (1 << 18);
public const int HHWIN_BUTTON_JUMP2 = (1 << 19);
public const int HHWIN_BUTTON_ZOOM = (1 << 20);
public const int HHWIN_BUTTON_TOC_NEXT = (1 << 21);
public const int HHWIN_BUTTON_TOC_PREV = (1 << 22);
public const int HHWIN_DEF_BUTTONS = HHWIN_BUTTON_EXPAND | HHWIN_BUTTON_BACK |
HHWIN_BUTTON_OPTIONS | HHWIN_BUTTON_PRINT;
// Structures
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int x;
public int y;
};
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
};
[StructLayout(LayoutKind.Sequential)]
public struct HH_WINTYPE
{
public int cbStruct; // IN: size of this structure including all Information Types
[MarshalAs(UnmanagedType.Bool)]
public bool fUniCodeStrings; // IN/OUT: TRUE if all strings are in Unicode
[MarshalAs(UnmanagedType.LPStr)]
public String pszType; // IN/OUT: Name of a type of window
public uint fsValidMembers; // IN: Bit flag of valid members (HHWIN_PARAM_)
public uint fsWinProperties; // IN/OUT: Properties/attributes of the window (HHWIN_)
[MarshalAs(UnmanagedType.LPStr)]
public String pszCaption; // IN/OUT: Window title
public uint dwStyles; // IN/OUT: Window styles
public uint dwExStyles; // IN/OUT: Extended Window styles
public Rect rcWindowPos; // IN: Starting position, OUT: current position
public int nShowState; // IN: show state (for example, SW_SHOW)
public int hwndHelp; // OUT: window handle
public int hwndCaller; // OUT: who called this window
// HH_INFOTYPE* paInfoTypes; // IN: Pointer to an array of Information Types
public int paInfoTypes; // WARNING: this array is not marshalled!
// The following members are only valid if HHWIN_PROP_TRI_PANE is set:
public int hwndToolBar; // OUT: toolbar window in tri-pane window
public int hwndNavigation; // OUT: navigation window in tri-pane window
public int hwndHTML; // OUT: window displaying HTML in tri-pane window
public int iNavWidth; // IN/OUT: width of navigation window
public Rect rcHTML; // OUT: HTML window coordinates
[MarshalAs(UnmanagedType.LPStr)]
public String pszToc; // IN: Location of the table of contents file
[MarshalAs(UnmanagedType.LPStr)]
public String pszIndex; // IN: Location of the index file
[MarshalAs(UnmanagedType.LPStr)]
public String pszFile; // IN: Default location of the html file
[MarshalAs(UnmanagedType.LPStr)]
public String pszHome; // IN/OUT: html file to display when Home button is clicked
public uint fsToolBarFlags; // IN: flags controlling the appearance of the toolbar
[MarshalAs(UnmanagedType.Bool)]
public bool fNotExpanded; // IN: TRUE/FALSE to contract or expand, OUT: current state
public int curNavType; // IN/OUT: UI to display in the navigational pane
public int tabpos; // IN/OUT: HHWIN_NAVTAB_TOP, HHWIN_NAVTAB_LEFT, or HHWIN_NAVTAB_BOTTOM
public int idNotify; // IN: ID to use for WM_NOTIFY messages
[MarshalAs(UnmanagedType.ByValArray, SizeConst= HH_MAX_TABS + 1, ArraySubType = UnmanagedType.U1)]
public byte [] tabOrder; // IN/OUT: tab order: Contents, Index, Search, History, Favorites, Reserved 1-5, Custom tabs
public int cHistory; // IN/OUT: number of history items to keep (default is 30)
[MarshalAs(UnmanagedType.LPStr)]
public String pszJump1; // Text for HHWIN_BUTTON_JUMP1
[MarshalAs(UnmanagedType.LPStr)]
public String pszJump2; // Text for HHWIN_BUTTON_JUMP2
[MarshalAs(UnmanagedType.LPStr)]
public String pszUrlJump1; // URL for HHWIN_BUTTON_JUMP1
[MarshalAs(UnmanagedType.LPStr)]
public String pszUrlJump2; // URL for HHWIN_BUTTON_JUMP2
public Rect rcMinSize; // Minimum size for window (ignored in version 1)
public int cbInfoTypes; // size of paInfoTypes;
// WARNING: this undocumented field is not marshalled
public int pszCustomTabs; // multiple zero-terminated strings
};
// Function calls
// internal interop helpers
public static int HtmlHelp_DisplayTopic(
int caller,
String file)
{
return HtmlHelp(caller, file, HH_DISPLAY_TOPIC, 0);
}
// This helper is for getting a ptr to an HH_WINTYPE struct OUT as the dwData parameter. This is
// used with the HH_GET_WIN_TYPE command.
[DllImport("hhctrl.ocx", CharSet=CharSet.Unicode, EntryPoint="HtmlHelpW")]
protected static extern int HtmlHelp_IntPtr_Helper(
int caller,
String file,
uint command,
ref IntPtr ps
);
// This overload is for performing the HH_SET_WIN_TYPE command, which passes an
// HH_WINTYPE value IN as the dwData parameter.
[DllImport("hhctrl.ocx", CharSet=CharSet.Unicode, EntryPoint="HtmlHelpW")]
protected static extern int HtmlHelp_SetWinType_Helper(
int caller,
String file,
uint command,
ref HH_WINTYPE wintype
);
// This overload is for passing a single uint value as the dwData parameter.
[DllImport("hhctrl.ocx", CharSet=CharSet.Unicode, EntryPoint="HtmlHelpW")]
protected static extern int HtmlHelp(
int caller,
String file,
uint command,
uint data
);
// This overload is for passing a string as the dwData parameter (for example, for the HH_DISPLAY_INDEX command)
[DllImport("hhctrl.ocx", CharSet=CharSet.Unicode, EntryPoint="HtmlHelpW")]
protected static extern int HtmlHelp(
int caller,
String file,
uint command,
String str
);
// public entrypoints
// This overload is for performing the HH_SET_WIN_TYPE command, which passes an
// HH_WINTYPE value IN as the dwData parameter.
public static int HtmlHelp_SetWinType(
int caller,
String file,
ref HH_WINTYPE wintype
)
{
wintype.cbStruct = Marshal.SizeOf(wintype);
wintype.fUniCodeStrings = false; // NOTE: this should be set to zero for proper 2-way marshalling
return HtmlHelp_SetWinType_Helper(
caller,
file,
HH_SET_WIN_TYPE,
ref wintype);
}
public static int HtmlHelp_GetWinType(
int caller,
String file,
ref HH_WINTYPE wintype)
{
IntPtr pwt = new IntPtr(0);
int retval = HtmlHelp_IntPtr_Helper(
caller,
file,
HH_GET_WIN_TYPE,
ref pwt);
// otherwise, let’s try to marshal it
wintype = (HH_WINTYPE)Marshal.PtrToStructure(pwt, typeof(HH_WINTYPE));
return retval;
}
}