⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 marshal.cs

📁 Windows Mobile平台应用与开发 书中c#源码
💻 CS
字号:
// Marshal.cs - Allocate and free various types of 
// unmanaged memory.
//
// Code from _Programming the .NET Compact Framework with C#_
// and _Programming the .NET Compact Framework with VB_
// (c) Copyright 2002-2004 Paul Yao and David Durant. 
// All rights reserved.

using System;
using System.Data;
using System.Runtime.InteropServices;

namespace YaoDurant.Allocator
{
   /// <summary>
   /// Summary description for Class1.
   /// </summary>
   public class Marshal
   {
      public Marshal()
      {
         //
         // TODO: Add constructor logic here
         //
      }

      //------------------------------------------------------------
      // Allocate / free COM memory
      //------------------------------------------------------------
      [DllImport("ole32.dll")]
      public static extern IntPtr CoTaskMemAlloc(int cb);

      [DllImport("ole32.dll")]
      public static extern void CoTaskMemFree(IntPtr pv);


      public static IntPtr AllocCoTaskMem(int cb)
      {
         return CoTaskMemAlloc(cb);
      }
      public static void FreeCoTaskMem(IntPtr ptr)
      {
         CoTaskMemFree(ptr);
      }

      //------------------------------------------------------------
      // Allocate / free regular heap memory
      //------------------------------------------------------------
      [DllImport("COREDLL.DLL")]
      public static extern IntPtr LocalAlloc (int fuFlags, int cbBytes);
      public const int LMEM_FIXED = 0x0000;
      public const int LMEM_ZEROINIT = 0x0040;
      public static IntPtr AllocHGlobal(int cb)
      {
         return LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, cb);
      }

      public static IntPtr AllocHGlobal(IntPtr cb)
      {
         return AllocHGlobal(cb.ToInt32());
      }

      [DllImport("COREDLL.DLL")]
      public static extern IntPtr LocalFree (IntPtr hMem);
      public static void FreeHGlobal(IntPtr hglobal)
      {
         LocalFree(hglobal);
      }

   } // class
} // namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -