日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# Marshal类基本概念和入门示例程序

發布時間:2025/4/14 C# 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# Marshal类基本概念和入门示例程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

marshal:直譯為“編排”, 在計算機中特 指將數據按某種描述格式編排出來,通常來說一般是從非文本格式到文本格式的數據轉化。
unmarshal是指marshal的逆過程。比如在WebService中,我們需要把java對象以xml方式表示并在網絡間傳輸,把java對象轉化成xml片段的過程就是marshal.

?

微軟對C#中Marshal類描述的鏈接在此;

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal?view=net-5.0

Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types, as well as other miscellaneous methods used when interacting with unmanaged code.
提供一個方法集合,分配非托管內存,拷貝非托管內存塊,轉換托管和非托管類型,以及一些和非托管代碼交互的雜類方法;

??? 這是Marshal類的基本功能;.net一共包含四個Marshal類,每個都有一些方法;

?

下面來看一個Marshal類基本程序;程序運行結果如下;

?

代碼如下;

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace marshalDemo1 {public struct Point{public Int32 x, y;}public partial class Form1 : Form{[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]static extern Boolean CloseHandle(IntPtr h);public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){textBox1.Text = Marshal.SystemDefaultCharSize.ToString();textBox2.Text = Marshal.SystemMaxDBCSCharSize.ToString();textBox3.Text = Marshal.SizeOf(typeof(Point)).ToString();Point p = new Point(); textBox4.Text = Marshal.SizeOf(p).ToString();IntPtr hglobal = Marshal.AllocHGlobal(100);textBox5.Text = hglobal.ToString();Marshal.FreeHGlobal(hglobal);Boolean f = CloseHandle(new IntPtr(-1));if (!f){Console.WriteLine("CloseHandle call failed with an error code of: {0}",Marshal.GetLastWin32Error());}}} }

?

總結

以上是生活随笔為你收集整理的C# Marshal类基本概念和入门示例程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。