本文整理汇总了C#中System.Runtime.InteropServices.GCHandle结构体的典型用法代码示例。如果您正苦于以下问题:C# GCHandle结构体的具体用法?C# GCHandle怎么用?C# GCHandle使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
GCHandle结构体属于System.Runtime.InteropServices命名空间,在下文中一共展示了GCHandle结构体的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CallBack
//引入命名空间
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);
internal static class NativeMethods
{
// passing managed object as LPARAM
// BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
[DllImport("user32.dll")]
internal 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 = Console.Out;
GCHandle gch = GCHandle.Alloc(tw);
CallBack cewp = new CallBack(CaptureEnumWindowsProc);
// platform invoke will prevent delegate to be garbage collected
// before call ends
NativeMethods.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;
}
}