C# System.Runtime.InteropServices 相关学习总结
生活随笔
收集整理的這篇文章主要介紹了
C# System.Runtime.InteropServices 相关学习总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
System.Runtime.InteropServices.COMException的解決方法
http://www.jb51.net/article/17629.htm完美解決“換另一臺電腦上用VS2008繼續開發web項目時出現“System.Runtime.InteropServices.COMException”,然后是加載不了項目。”?
只需要打開項目配置文件*.csproj,將<UseIIS>True</UseIIS> 改為 False,然后可以正常加載項目,接著重新配置為正常的IIS.?
========
異常詳細信息: System.Runtime.InteropServices.COMException
http://www.cnblogs.com/star250/archive/2008/03/06/1093720.html無效的類別字符串
說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以了解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。?
異常詳細信息: System.Runtime.InteropServices.COMException: 無效的類別字符串
源錯誤:?
?行 88: ? ? ? ? ? ? ? ?String ss=Server.MapPath("rpt\\S_CardTradeDayDetail.rpt").ToString();
? ? ? ? ? ? 行 89: ? ? ? ? ? ? ? ?CrystalReportSource1.ReportDocument.Load(Server.MapPath("rpt\\S_CardTradeDayDetail.rpt"), OpenReportMethod.OpenReportByTempCopy);
? ? ? ? ? ??
有一臺服務器部署時出現這樣的問題
權限問題?
解決方法有幾種?
第1種.給asp.net帳號分配好訪問com的權限,配置好com+?
第2種.在web.config的system.web節加入?
<identity impersonate="true" userName="具有管理員權限的帳號名" password="相應的密碼"/>?
來模擬一個具有管理權限的身份來運行你的代碼?
我使用了第二種方法,為什么在其他服務器上沒有這個問題,可能是這個服務器權限設置過高,請知道的朋友解答一下。謝謝!?
2008-4-11 我發現在用第二種方法也不行,第3種:給C:\WINDOWS\Temp 文件夾增加用戶 NETWORK SERVICE ,并擁有修改權限。 //我想這是因為水晶報表要在C:\WINDOWS\Temp 產生臨時文件的原因
========
System.Runtime.InteropServices 命名空間
https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices(v=vs.80).aspx.NET Framework 2.0 其他版本?
System.Runtime.InteropServices 命名空間提供各種各樣支持 COM interop 及平臺調用服務的成員。如果您對這些服務不熟悉,請參見 與非托管代碼交互操作。
此命名空間提供了多種類別的功能,如下表所示。屬性可控制封送行為,例如如何安排結構或表示字符串。其中最重要的屬性有 DllImportAttribute(可以用來定義用于訪問非托管 API 的平臺調用方法)和 MarshalAsAttribute(可以用來指定如何在托管內存與非托管內存之間封送數據)。
類
? 類 說明
公共類 AutomationProxyAttribute 指定是否應該使用自動化封送拆收器或自定義代理及存根 (Stub) 對該類型進行封送處理。
公共類 BestFitMappingAttribute 控制是否將 Unicode 字符轉換為最接近的匹配 ANSI 字符。
公共類 BStrWrapper 將 VT_BSTR 類型的數據從托管代碼封送到非托管代碼。無法繼承此類。
公共類 ClassInterfaceAttribute 為公開給 COM 的類指定要生成的類接口的類型(如果有接口生成)。
公共類 CoClassAttribute 指定從類型庫中導入的 coclass 的類標識符。
公共類 ComAliasNameAttribute 指示參數或字段類型的 COM 別名。
公共類 ComCompatibleVersionAttribute 向 COM 客戶端指示程序集當前版本中的所有類與該程序集早期版本中的類兼容。
公共類 ComConversionLossAttribute 指示從類型庫向程序集導入類或接口時丟失了有關信息。
公共類 ComDefaultInterfaceAttribute 指定要向 COM 公開的默認接口。無法繼承此類。
公共類 ComEventInterfaceAttribute 標識源接口和實現事件接口(從 COM 類型庫導入 coclass 時生成)的方法的類。
公共類 COMException 當從 COM 方法調用返回無法識別的 HRESULT 時引發的異常。
公共類 ComImportAttribute 指示該屬性化類型是以前在 COM 中定義的。
公共類 ComRegisterFunctionAttribute 指定當注冊用于 COM 的程序集時調用的方法;這可以用于注冊過程中用戶編寫代碼的執行。
公共類 ComSourceInterfacesAttribute 為屬性化類標識公開為 COM 事件源的一組接口。
公共類 ComUnregisterFunctionAttribute 指定當注銷用于 COM 的程序集時調用的方法;這可以用于注銷過程中用戶編寫代碼的執行。
公共類 ComVisibleAttribute 控制程序集中個別托管類型、成員或所有類型對 COM 的可訪問性。
公共類 CriticalHandle 表示句柄資源的包裝類。
公共類 CurrencyWrapper 對封送拆收器應該將其作為 VT_CY 封送的對象進行包裝。
公共類 DefaultCharSetAttribute 指定 CharSet 枚舉的值。無法繼承此類。
公共類 DefaultParameterValueAttribute 當從支持默認參數的語言執行調用時,設置參數的默認值。無法繼承此類。
公共類 DispatchWrapper 對封送拆收器應該將其作為 VT_DISPATCH 封送的對象進行包裝。
公共類 DispIdAttribute 指定方法、字段或屬性的 COM 調度標識符 (DISPID)。
公共類 DllImportAttribute 指示該屬性化方法由非托管動態鏈接庫 (DLL) 作為靜態入口點公開。
公共類 ErrorWrapper 對封送拆收器應該將其作為 VT_ERROR 封送的對象進行包裝。
公共類 ExtensibleClassFactory 在創建過程中啟用從非托管對象擴展的托管對象的自定義。
公共類 ExternalException 所有 COM Interop 異常和結構化異常處理 (SEH) 異常的異常基類型。
公共類 FieldOffsetAttribute 指示字段在類或結構的非托管表示形式內的物理位置。
公共類 GuidAttribute 當不需要自動 GUID 時提供顯式的 System.Guid。
公共類 HandleCollector 跟蹤未處理的句柄,并在達到指定閾值時強制執行垃圾回收。
公共類 IDispatchImplAttribute 指示當向 COM 公開雙重接口和調度接口時公共語言運行庫使用何種 IDispatch 實現。
公共類 ImportedFromTypeLibAttribute 指示在程序集內定義的類型原來在類型庫中定義。
公共類 InAttribute 指示應將數據從調用方封送到被調用方,而不返回到調用方。
公共類 InterfaceTypeAttribute 指示向 COM 公開時,托管接口是雙重的、僅支持調度的、還是僅支持 IUnknown 的。
公共類 InvalidComObjectException 當使用無效的 COM 對象時引發的異常。
公共類 InvalidOleVariantTypeException 封送拆收器在遇到不能封送到托管代碼的 Variant 類型的參數時引發的異常。
公共類 LCIDConversionAttribute 指示方法的非托管簽名需要區域設置標識符 (LCID) 參數。
公共類 Marshal 提供了一個方法集,這些方法用于分配非托管內存、復制非托管內存塊、將托管類型轉換為非托管類型,此外還提供了在與非托管代碼交互時使用的其他雜項方法。
公共類 MarshalAsAttribute 指示如何在托管代碼和非托管代碼之間封送數據。
公共類 MarshalDirectiveException 當封送拆收器遇到它不支持的 MarshalAsAttribute 時引發的異常。
公共類 OptionalAttribute 指示參數是可選的。
公共類 OutAttribute 指示應將數據從被調用方封送回調用方。
公共類 PreserveSigAttribute 指示應取消在 COM interop 調用期間發生的 HRESULT 或 retval 簽名轉換。
公共類 PrimaryInteropAssemblyAttribute 指示該屬性化的程序集是主 Interop 程序集。
公共類 ProgIdAttribute 允許用戶指定類的 ProgID。
公共類 RegistrationServices 提供一組用于注冊和注銷托管程序集以供從 COM 使用的服務。
公共類 RuntimeEnvironment 提供一個返回有關公共語言運行庫環境的信息的 static 方法的集合。
公共類 SafeArrayRankMismatchException 當傳入的 SAFEARRAY 的秩與托管簽名中指定的秩不匹配時引發的異常。
公共類 SafeArrayTypeMismatchException 當傳入的 SAFEARRAY 的類型與托管簽名中指定的類型不匹配時引發的異常。
公共類 SafeHandle 表示操作系統句柄的包裝類。
公共類 SEHException 表示結構化異常處理程序 (SEH) 錯誤。
公共類 SetWin32ContextInIDispatchAttribute 此屬性已被否決。
公共類 StandardOleMarshalObject 用標準 OLE STA 封送拆收器替換標準公共語言運行庫 (CLR) 自由線程封送拆收器。
公共類 StructLayoutAttribute StructLayoutAttribute 類使用戶可以控制類或結構的數據字段的物理布局。
公共類 TypeLibConverter 提供一組服務,將托管程序集轉換為 COM 類型庫或進行反向轉換。
公共類 TypeLibFuncAttribute 包含最初從 COM 類型庫為此方法導入的 FUNCFLAGS。
公共類 TypeLibImportClassAttribute 指定哪個 Type 獨占使用接口。無法繼承此類。
公共類 TypeLibTypeAttribute 包含最初為此類型從 COM 類型庫導入的 TYPEFLAGS。
公共類 TypeLibVarAttribute 包含最初為此字段從 COM 類型庫導入的 VARFLAGS。
公共類 TypeLibVersionAttribute 指定導出類型庫的版本號。
公共類 UnknownWrapper 對封送拆收器應該將其作為 VT_UNKNOWN 封送的對象進行包裝。
公共類 UnmanagedFunctionPointerAttribute 控制作為非托管函數指針傳入或傳出非托管代碼的委托簽名的封送行為。無法繼承此類。
公共類 VariantWrapper 將 VT_VARIANT | VT_BYREF 類型的數據從托管代碼封送到非托管代碼。無法繼承此類。
接口
? 接口 說明
公共接口 _Activator 向非托管代碼公開 System.Activator 類。
公共接口 _Assembly 向非托管代碼公開 System.Reflection.Assembly 類的公共成員。
公共接口 _AssemblyBuilder 向非托管代碼公開 System.Reflection.Emit.AssemblyBuilder 類。
公共接口 _AssemblyName 向非托管代碼公開 AssemblyName 類。
公共接口 _Attribute 向非托管代碼公開 System.Attribute 類。
公共接口 _ConstructorBuilder 向非托管代碼公開 System.Reflection.Emit.ConstructorBuilder 類。
公共接口 _ConstructorInfo 向非托管代碼公開 System.Reflection.ConstructorInfo 類的公共成員。
公共接口 _CustomAttributeBuilder 向非托管代碼公開 System.Reflection.Emit.CustomAttributeBuilder 類。
公共接口 _EnumBuilder 向非托管代碼公開 System.Reflection.Emit.EnumBuilder 類。
公共接口 _EventBuilder 向非托管代碼公開 System.Reflection.Emit.EventBuilder 類。
公共接口 _EventInfo 向非托管代碼公開 System.Reflection.EventInfo 類的公共成員。
公共接口 _Exception 向非托管代碼公開 System.Exception 類的公共成員。
公共接口 _FieldBuilder 向非托管代碼公開 System.Reflection.Emit.FieldBuilder 類。
公共接口 _FieldInfo 向非托管代碼公開 System.Reflection.FieldInfo 類的公共成員。
公共接口 _ILGenerator 向非托管代碼公開 System.Reflection.Emit.ILGenerator 類。
公共接口 _LocalBuilder 向非托管代碼公開 System.Reflection.Emit.LocalBuilder 類。
公共接口 _MemberInfo 向非托管代碼公開 System.Reflection.MemberInfo 類的公共成員。
公共接口 _MethodBase 向非托管代碼公開 System.Reflection.MethodBase 類的公共成員。
公共接口 _MethodBuilder 向非托管代碼公開 MethodBuilder 類。
公共接口 _MethodInfo 向非托管代碼公開 System.Reflection.MethodInfo 類的公共成員。
公共接口 _MethodRental 向非托管代碼公開 System.Reflection.Emit.MethodRental 類。
公共接口 _Module 向非托管代碼公開 Module 類。
公共接口 _ModuleBuilder 向非托管代碼公開 System.Reflection.Emit.ModuleBuilder 類。
公共接口 _ParameterBuilder 向非托管代碼公開 System.Reflection.Emit.ParameterBuilder 類。
公共接口 _ParameterInfo 向非托管代碼公開 System.Reflection.ParameterInfo 類。
公共接口 _PropertyBuilder 向非托管代碼公開 System.Reflection.Emit.PropertyBuilder 類。
公共接口 _PropertyInfo 向非托管代碼公開 System.Reflection.PropertyInfo 類的公共成員。
公共接口 _SignatureHelper 向非托管代碼公開 System.Reflection.Emit.SignatureHelper 類。
公共接口 _Thread 向非托管代碼公開 System.Threading.Thread 類。
公共接口 _Type 向非托管代碼公開 System.Type 類的公共成員。
公共接口 _TypeBuilder 向非托管代碼公開 System.Reflection.Emit.TypeBuilder 類。
公共接口 ICustomAdapter 為客戶端訪問實際對象(而不是自定義封送拆收器分發的適配器對象)提供了一種方式。
公共接口 ICustomFactory 使用戶能夠為擴展 MarshalByRefObject 的托管對象編寫激活代碼。
公共接口 ICustomMarshaler 旨在提供用于處理方法調用的自定義包裝。
公共接口 IRegistrationServices 提供一組用于注冊和注銷托管程序集以供從 COM 使用的服務。
公共接口 ITypeLibConverter 提供一組服務,將托管程序集轉換為 COM 類型庫或進行反向轉換。
公共接口 ITypeLibExporterNameProvider 當導出到類型庫時提供名稱大小寫控制。
公共接口 ITypeLibExporterNotifySink 提供回調機制,以供程序集轉換器向調用方通知轉換的狀態,并在轉換過程本身之中涉及調用方。
公共接口 ITypeLibImporterNotifySink 提供回調機制,以供類型庫轉換器向調用方通知轉換的狀態,并在轉換過程本身之中涉及調用方。
公共接口 UCOMIBindCtx 應改用 BIND_OPTS。
公共接口 UCOMIConnectionPoint 應改用 IConnectionPoint。
公共接口 UCOMIConnectionPointContainer 應改用 IConnectionPointContainer。
公共接口 UCOMIEnumConnectionPoints 應改用 IEnumConnectionPoints。
公共接口 UCOMIEnumConnections 應改用 IEnumConnections。
公共接口 UCOMIEnumMoniker 應改用 IEnumMoniker。
公共接口 UCOMIEnumString 應改用 IEnumString。
公共接口 UCOMIEnumVARIANT 應改用 IEnumVARIANT。
公共接口 UCOMIMoniker 應改用 IMoniker。
公共接口 UCOMIPersistFile 注意:此接口現在已過時。應改用 IPersistFile。
公共接口 UCOMIRunningObjectTable 應改用 IRunningObjectTable。
公共接口 UCOMIStream 應改用 IStream。
公共接口 UCOMITypeComp 應改用 ITypeComp。
公共接口 UCOMITypeInfo 應改用 ITypeInfo。
公共接口 UCOMITypeLib 應改用 ITypeLib。
結構
? 結構 說明
公共結構 ArrayWithOffset 在指定的數組中封裝數組和偏移量。
公共結構 BIND_OPTS 應改用 BIND_OPTS。
公共結構 BINDPTR 應改用 BINDPTR。
公共結構 CONNECTDATA 應改用 CONNECTDATA。
公共結構 DISPPARAMS 應改用 DISPPARAMS。
公共結構 ELEMDESC 應改用 ELEMDESC。
公共結構 ELEMDESC.DESCUNION 應改用 ELEMDESC.DESCUNION。
公共結構 EXCEPINFO 應改用 EXCEPINFO。
公共結構 FILETIME 應改用 FILETIME。
公共結構 FUNCDESC 應改用 FUNCDESC。
公共結構 GCHandle 提供從非托管內存訪問托管對象的方法。
公共結構 HandleRef 包裝一個托管對象,該對象保存使用平臺 invoke(調用)傳遞給非托管代碼的資源句柄。
公共結構 IDLDESC 應改用 IDLDESC。
公共結構 PARAMDESC 應改用 PARAMDESC。
公共結構 STATSTG 應改用 STATSTG。
公共結構 TYPEATTR 應改用 TYPEATTR。
公共結構 TYPEDESC 應改用 TYPEDESC。
公共結構 TYPELIBATTR 應改用 TYPELIBATTR。
公共結構 VARDESC 應改用 VARDESC。
公共結構 VARDESC.DESCUNION 注意:此結構已過時。應改用 VARDESC.DESCUNION。
委托
? 委托 說明
公共委托 ObjectCreationDelegate 創建 COM 對象。
枚舉
? 枚舉 說明
公共枚舉 AssemblyRegistrationFlags 定義當注冊程序集時使用的一組標志。
公共枚舉 CALLCONV 應改用 CALLCONV。
公共枚舉 CallingConvention 指定調用在非托管代碼中實現的方法所需的調用約定。
公共枚舉 CharSet 規定封送字符串應使用何種字符集。
公共枚舉 ClassInterfaceType 標識為某個類生成的類接口的類型。
公共枚舉 ComInterfaceType 標識如何向 COM 公開接口。
公共枚舉 ComMemberType 描述 COM 成員的類型。
公共枚舉 DESCKIND 應改用 DESCKIND。
公共枚舉 ExporterEventKind 描述類型庫導出程序在導出類型庫時生成的回調。
公共枚舉 FUNCFLAGS 應改用 FUNCFLAGS。
公共枚舉 FUNCKIND 應改用 FUNCKIND。
公共枚舉 GCHandleType 表示 GCHandle 類可以分配的句柄的類型。
公共枚舉 IDispatchImplType 指示對特定類使用何種 IDispatch 實現。
公共枚舉 IDLFLAG 應改用 IDLFLAG。
公共枚舉 IMPLTYPEFLAGS 應改用 IMPLTYPEFLAGS。
公共枚舉 ImporterEventKind 描述類型庫導入程序在導入類型庫時生成的回調。
公共枚舉 INVOKEKIND 應改用 INVOKEKIND。
公共枚舉 LayoutKind 控制當導出到非托管代碼時對象的布局。
公共枚舉 LIBFLAGS 應改用 LIBFLAGS。
公共枚舉 PARAMFLAG 應改用 PARAMFLAG。
公共枚舉 RegistrationClassContext 指定執行上下文集,類對象將在這些上下文中對請求構造實例的請求可用。
公共枚舉 RegistrationConnectionType 定義到類對象的連接的類型。
公共枚舉 SYSKIND 應改用 SYSKIND。
公共枚舉 TYPEFLAGS 應改用 TYPEFLAGS。
公共枚舉 TYPEKIND 應改用 TYPEKIND。
公共枚舉 TypeLibExporterFlags 指示應該如何生成類型庫。
公共枚舉 TypeLibFuncFlags 描述從中導入該方法的 COM 類型庫中 FUNCFLAGS 的原始設置。
公共枚舉 TypeLibImporterFlags 指示應該如何生成程序集。
公共枚舉 TypeLibTypeFlags 描述從中導入該類型的 COM 類型庫中 TYPEFLAGS 的原始設置。
公共枚舉 TypeLibVarFlags 描述從中導入此變量的 COM 類型庫中 VARFLAGS 的原始設置。
公共枚舉 UnmanagedType 指定如何將參數或字段封送到非托管代碼。
公共枚舉 VarEnum 指示當數組作為 UnmanagedType.SafeArray 從托管代碼封送到非托管代碼時,如何封送該數組的元素。
公共枚舉 VARFLAGS 應改用 VARFLAGS。
========
GCHandle Structure
https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle(v=vs.110).aspx
.NET Framework (current version) Other Versions?
?
Provides a way to access a managed object from unmanaged memory.
Namespace: ? System.Runtime.InteropServices
Assembly: ?mscorlib (in mscorlib.dll)
Syntax
C#C++F#VB
[ComVisibleAttribute(true)]
public struct GCHandle
Properties
Name Description
System_CAPS_pubproperty IsAllocated
Gets a value indicating whether the handle is allocated.
System_CAPS_pubproperty Target
Gets or sets the object this handle represents.
Methods
Name Description
System_CAPS_pubmethod AddrOfPinnedObject()
Retrieves the address of an object in a GCHandleType.Pinned handle.
System_CAPS_pubmethodSystem_CAPS_static Alloc(Object)
Allocates a Normal handle for the specified object.
System_CAPS_pubmethodSystem_CAPS_static Alloc(Object,?GCHandleType)
Allocates a handle of the specified type for the specified object.
System_CAPS_pubmethod Equals(Object)
Determines whether the specified GCHandle object is equal to the current GCHandle object.(Overrides ValueType.Equals(Object).)
System_CAPS_pubmethod Free()
Releases a GCHandle.
System_CAPS_pubmethodSystem_CAPS_static FromIntPtr(IntPtr)
Returns a new GCHandle object created from a handle to a managed object.
System_CAPS_pubmethod GetHashCode()
Returns an identifier for the current GCHandle object.(Overrides ValueType.GetHashCode().)
System_CAPS_pubmethod GetType()
Gets the Type of the current instance.(Inherited from Object.)
System_CAPS_pubmethodSystem_CAPS_static ToIntPtr(GCHandle)
Returns the internal integer representation of a GCHandle object.
System_CAPS_pubmethod ToString()
Returns the fully qualified type name of this instance.(Inherited from ValueType.)
Operators
Name Description
System_CAPS_puboperatorSystem_CAPS_static Equality(GCHandle,?GCHandle)
Returns a value indicating whether two GCHandle objects are equal.
System_CAPS_puboperatorSystem_CAPS_static Explicit(GCHandle?to?IntPtr)
A GCHandle is stored using an internal integer representation.
System_CAPS_puboperatorSystem_CAPS_static Explicit(IntPtr?to?GCHandle)
A GCHandle is stored using an internal integer representation.
System_CAPS_puboperatorSystem_CAPS_static Inequality(GCHandle,?GCHandle)
Returns a value indicating whether two GCHandle objects are not equal.
Remarks
The GCHandle structure is used with the GCHandleType enumeration to create a handle corresponding to any managed object. This handle can be one of four types: Weak, WeakTrackResurrection, Normal, or Pinned. When the handle has been allocated, you can use it to prevent the managed object from being collected by the garbage collector when an unmanaged client holds the only reference. Without such a handle, the object can be collected by the garbage collector before completing its work on behalf of the unmanaged client.
You can also use GCHandle to create a pinned object that returns a memory address to prevent the garbage collector from moving the object in memory.
When the handle goes out of scope you must explicitly release it by calling the Free method; otherwise, memory leaks may occur. When you free a pinned handle, the associated object will be unpinned and will become eligible for garbage collection, if there are no other references to it.
Examples
The following example shows an App class that creates a handle to a managed object using the GCHandle.Alloc method, which prevents the managed object from being collected. A call to the EnumWindows method passes a delegate and a managed object (both declared as managed types, but not shown), and casts the handle to an IntPtr. The unmanaged function passes the type back to the caller as a parameter of the callback function.
C#VB
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Permissions;
public delegate bool CallBack(int handle, IntPtr param);
public class LibWrap
{
// passing managed object as LPARAM
// BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
[DllImport("user32.dll")]
public static extern bool EnumWindows(CallBack cb, IntPtr param);
}
public class App
{
public static void Main()
{
Run();
}
? ? ? ? [SecurityPermission(SecurityAction.Demand, UnmanagedCode=true)]
public static void Run()
? ? ? ? {
TextWriter tw = System.Console.Out;
GCHandle gch = GCHandle.Alloc(tw);
CallBack cewp = new CallBack(CaptureEnumWindowsProc);
// platform invoke will prevent delegate to be garbage collected
// before call ends
LibWrap.EnumWindows(cewp, GCHandle.ToIntPtr(gch));
gch.Free();
? ? ? ? }
private static bool CaptureEnumWindowsProc(int handle, IntPtr param)
{
GCHandle gch = GCHandle.FromIntPtr(param);
TextWriter tw = (TextWriter)gch.Target;
tw.WriteLine(handle);
return true;
}
}
Version Information
Universal Windows Platform?
Available since 8
.NET Framework?
Available since 1.1
Portable Class Library?
Supported in: portable .NET platforms?
Silverlight?
Available since 2.0
Windows Phone Silverlight?
Available since 7.0
Windows Phone?
Available since 8.1
Thread Safety
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also
GCHandleType
System.Runtime.InteropServices Namespace
The Truth About GCHandles
========
GuidAttribute 類
https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.guidattribute.aspx.NET Framework (current version) 其他版本?
?
提供一個顯式 System.Guid 出現自動 GUID 時不需要。
命名空間: ? System.Runtime.InteropServices
程序集: ?mscorlib(位于 mscorlib.dll)
繼承層次結構
System.Object
??System.Attribute
????System.Runtime.InteropServices.GuidAttribute
語法
C#C++F#VB
[AttributeUsageAttribute(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate,?
Inherited = false)]
[ComVisibleAttribute(true)]
public sealed class GuidAttribute : Attribute
構造函數
名稱 說明
System_CAPS_pubmethod GuidAttribute(String)
新實例初始化 GuidAttribute 帶有指定 GUID 的類。
屬性
名稱 說明
System_CAPS_pubproperty TypeId
在派生類中實現時,獲取此 Attribute 的唯一標識符。(繼承自 Attribute。)
System_CAPS_pubproperty Value
獲取Guid的類。
方法
名稱 說明
System_CAPS_pubmethod Equals(Object)
此 API 支持 產品 基礎結構,不應從代碼直接使用。 返回一個值,該值指示此實例是否與指定的對象相等。(繼承自 Attribute。)
System_CAPS_pubmethod GetHashCode()
返回此實例的哈希代碼。(繼承自 Attribute。)
System_CAPS_pubmethod GetType()
獲取當前實例的 Type。(繼承自 Object。)
System_CAPS_pubmethod IsDefaultAttribute()
在派生類中重寫時,指示此實例的值是否是派生類的默認值。(繼承自 Attribute。)
System_CAPS_pubmethod Match(Object)
當在派生類中重寫,則返回一個值,該值指示此實例是否等于指定的對象。(繼承自 Attribute。)
System_CAPS_pubmethod ToString()
返回表示當前對象的字符串。(繼承自 Object。)
顯式接口實現
名稱 說明
System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetIDsOfNames(Guid,?IntPtr,?UInt32,?UInt32,?IntPtr)
將一組名稱映射為對應的一組調度標識符。(繼承自 Attribute。)
System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetTypeInfo(UInt32,?UInt32,?IntPtr)
檢索對象的類型信息,然后可以使用該信息獲取接口的類型信息。(繼承自 Attribute。)
System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetTypeInfoCount(UInt32)
檢索對象提供的類型信息接口的數量(0 或 1)。(繼承自 Attribute。)
System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.Invoke(UInt32,?Guid,?UInt32,?Int16,?IntPtr,?IntPtr,?IntPtr,?IntPtr)
提供對某一對象公開的屬性和方法的訪問。(繼承自 Attribute。)
備注
可以將此特性應用到程序集、 接口、 類、 枚舉、 結構或委托,盡管 Tlbimp.exe(類型庫導入程序) 可以將其應用為您導入類型庫時。
傳遞給該屬性的字符串必須是該類型的可接受的構造函數參數的格式 Guid。 若要避免與該類型的沖突 Guid, ,使用長名稱 GuidAttribute 顯式。 類型必須具有特定的 GUID 時,只能使用一個顯式 GUID。 如果省略該屬性,則會自動分配一個 GUID。
示例
下面的示例演示如何將應用 GuidAttribute 于導出到具有固定的 GUID 的 COM 類。
C#C++VB
using System;
using System.Runtime.InteropServices;
[GuidAttribute("9ED54F84-A89D-4fcd-A854-44251E925F09")]
public class SampleClass?
{
? ? // Insert class members here.
}
版本信息
通用 Windows 平臺?
自 8 起可用
.NET Framework?
自 1.1 起可用
可移植類庫?
在 可移植 .NET 平臺 中受支持
Silverlight?
自 2.0 起可用
Windows Phone Silverlight?
自 7.0 起可用
Windows Phone?
自 8.1 起可用
線程安全
此類型的所有公共靜態(Visual Basic 中的 已共享 在 Visual Basic 中)成員都是線程安全的。不保證所有實例成員都是線程安全的。
另請參閱
Guid
System.Runtime.InteropServices 命名空間
Tlbimp.exe(類型庫導入程序)
========
用System.Runtime.InteropServices服務的DllImport方法引入非托管代碼程序集 ?
http://blog.163.com/yuyi_84225505@126/blog/static/36297389201271602234279/extern 主要用于聲明在外部實現的方法,什么叫外部實用的方法呢,一般說來就是用System.Runtime.InteropServices服務的DllImport方法引入非托管代碼程序集。例如調用系統API,C語言寫的方法等等。在這種情況下,聲明必須為static
同時,extern 關鍵字還可以定義外部程序集別名,使得可以從單個程序集中引用同一組件的不同版本。
下面是一個改寫自MSDN上的簡單的例子,調用系統winmm.DLL播放wav文件:
//系統API的調用的聲明
? ? ? ? [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
? ? ? ? public static extern void PlaySound(string path,System.IntPtr hMod,PlaySoundFlags flags);
//調用該方法
? ? string path = @"c:\111.wav";
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? PlaySound(path, new System.IntPtr(), PlaySoundFlags.SND_SYNC);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw (ex);
? ? ? ? ? ? }?
C#讀寫ini文件的類,調用kernel32.dll中的api:WritePrivateProfileString,GetPrivateProfileString
using ?System.Runtime.InteropServices;
using ?System.Text;?
namespace ?INIFile
{
? ? ?/// ?<summary>
? ? ?/// ?讀寫ini文件的類
? ? ?/// ?調用kernel32.dll中的兩個api:WritePrivateProfileString,GetPrivateProfileString來實現對ini ?文件的讀寫。
? ? ?///
? ? ?/// ?INI文件是文本文件,
? ? ?/// ?由若干節(section)組成,
? ? ?/// ?在每個帶括號的標題下面,
? ? ?/// ?是若干個關鍵詞(key)及其對應的值(value)
? ? ?///?
///[Section]
///Key=value
? ? ?///
? ? ?/// ?</summary>
? ? ?public ?class ?IniFile
? ? ?{
? ? ? ? ?/// ?<summary>
? ? ? ? ?/// ?ini文件名稱(帶路徑)
? ? ? ? ?/// ?</summary>
? ? ? ? ?public ?string ?filePath;?
? ? ? ? ? //聲明讀寫INI文件的API函數
? ? ? ? ?[DllImport("kernel32")]?
? ? ? ? ?private ?static ?extern ?long ?WritePrivateProfileString(string ?section,string ?key,string ?val,string ?filePath);
?
? ? ? ? ?[DllImport("kernel32")]
? ? ? ? ?private ?static ?extern ?int ?GetPrivateProfileString(string ?section,string ?key,string ?def,StringBuilder ?retVal,int ?size,string ?filePath);
?
? ? ? ? ?/// ?<summary>
? ? ? ? ?/// ?類的構造函數
? ? ? ? ?/// ?</summary>
? ? ? ? ?/// ?<param ?name="INIPath">INI文件名</param>?
? ? ? ? ?public ?IniFile(string ?INIPath) ??
? ? ? ? ?{?
? ? ? ? ? ? ? filePath ?= ?INIPath;?
? ? ? ? ?}
?
? ? ? ? ?/// ?<summary>
? ? ? ? ?/// ? 寫INI文件
? ? ? ? ?/// ?</summary>
? ? ? ? ?/// ?<param ?name="Section">Section</param>
? ? ? ? ?/// ?<param ?name="Key">Key</param>
? ? ? ? ?/// ?<param ?name="value">value</param>
? ? ? ? ?public ?void ?WriteInivalue(string ?Section,string ?Key,string ?value) ??
? ? ? ? ?{ ? ??
? ? ? ? ? ? ? WritePrivateProfileString(Section,Key,value,this.filePath);?
? ? ? ? ?}
? ? ? ? ?/// ?<summary>
? ? ? ? ?/// ? ?讀取INI文件指定部分
? ? ? ? ?/// ?</summary>
? ? ? ? ?/// ?<param ?name="Section">Section</param>
? ? ? ? ?/// ?<param ?name="Key">Key</param>
? ? ? ? ?/// ?<returns>String</returns>?
? ? ? ? ?public ?string ?ReadInivalue(string ?Section,string ?Key)?
? ? ? ? ?{ ??
? ? ? ? ? ? ? StringBuilder ?temp ?= ?new ?StringBuilder(255);
? ? ? ? ? ? ? int ?i ?= ?GetPrivateProfileString(Section,Key,"",temp,255,this.filePath);?
? ? ? ? ? ? ? return ?temp.ToString();
? ? ? ? ?}?
? ? ?}?
}
========
與非托管代碼交互操作
https://msdn.microsoft.com/zh-cn/library/sd10k43k(v=vs.80).aspx.NET Framework 2.0 其他版本?
Microsoft .NET Framework 將促進與 COM 組件、COM+ 服務、外部類型庫和許多操作系統服務的互操作。在托管和非托管對象模型之間,數據類型、方法簽名和錯誤處理機制都存在差異。為了簡化 .NET Framework 組件和非托管代碼之間的互用并便于進行移植,公共語言運行庫將從客戶端和服務器中隱藏這兩種對象模型之間的差異。
在運行庫的控制下執行的代碼稱作托管代碼。相反,在運行庫之外運行的代碼稱作非托管代碼。COM 組件、ActiveX 接口和 Win32 API 函數都是非托管代碼的示例。
本節內容
與非托管代碼交互操作幫助主題
提供指向在有關與非托管代碼進行互操作的概念性文檔中找到的所有幫助主題的鏈接。
向 .NET Framework 公開 COM 組件
描述如何從基于 .NET 的應用程序使用 COM 組件。
向 COM 公開 .NET Framework 組件
描述如何從 COM 應用程序使用基于 .NET 的組件。
使用非托管 DLL 函數
描述如何使用平臺調用來調用非托管的 DLL 函數。
互操作的設計注意事項
提供有關編寫集成 COM 組件的提示。
互操作封送處理
描述如何對 COM Interop 和平臺調用進行封送處理。
如何:映射 HRESULT 和異常
描述異常和 HRESULT 之間的映射。
使用泛型類型進行交互操作
描述泛型類型在用于 COM interop 時的行為。
相關章節
高級開發技術
提供指向關于 .NET Framework 中的高級開發任務和技術的更多信息的鏈接。
========
高級開發技術
https://msdn.microsoft.com/zh-cn/library/a493kdy6(v=vs.80).aspx本主題提供指向有關 .NET Framework 中復雜開發任務和技術的信息的鏈接。
本節內容
管理
提供有關使用目錄服務和 Windows Management Instrumentation (WMI) 的信息。
設計環境的組件創作
描述如何創建自定義組件以及如何擴展設計時支持。
異步編程設計模式
討論 .NET Framework 中的異步編程功能。
動態源代碼生成和編譯
解釋代碼文檔對象模型 (CodeDOM),該模型實現了多種編程語言的源代碼的輸出。
發出動態方法和程序集
描述 System.Reflection.Emit 命名空間中的一組托管類型,它們允許編譯器或工具在運行時發出元數據和 Microsoft 中間語言 (MSIL),或者也可以允許它們在磁盤上生成可移植可執行 (PE) 文件。
垃圾回收
討論垃圾回收器如何管理內存以及您如何可以通過編程來更有效地使用內存。
承載公共語言運行庫
解釋運行庫宿主概念,將運行庫加載到進程中,在該進程內創建應用程序域,并且加載和執行用戶代碼。
互操作性
描述由公共語言運行庫提供的互操作性服務。還提供一些鏈接,指向有關將 COM 組件集成到 .NET Framework 應用程序中的信息。
網絡編程
演示如何使用 Internet 訪問類來實現基于 Web 的應用程序和基于 Internet 的應用程序。
反射
解釋如何通過使用反射在運行時訪問類型信息。
可靠性
描述面向可靠性的屬性(如 HPA 和 ReliabilityContract)的用法,討論編寫可靠代碼的最佳做法。
遠程對象
描述 .NET Framework 中為遠程通信提供的多種通信方法。
序列化
討論 XML 序列化。
托管線程處理
解釋對進程處理的運行時支持以及如何使用各種同步技術編程。
編寫服務組件
描述如何配置和注冊服務組件以訪問 COM+ 服務。
參考
System.Reflection.Emit
說明 Emit 命名空間的主要功能。
相關章節
核心開發技術
提供指向有關 .NET Framework 中標準應用程序開發任務的信息的鏈接。
.NET Framework 中的安全性
提供一些鏈接,指向有關 .NET Framework 中的類和服務的信息,這些類和服務可以幫助確保應用程序開發的安全。
========
總結
以上是生活随笔為你收集整理的C# System.Runtime.InteropServices 相关学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。