溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#?Marshal類基本概念和入門實例代碼分析

發(fā)布時間:2023-02-27 09:51:16 來源:億速云 閱讀:141 作者:iii 欄目:開發(fā)技術

本文小編為大家詳細介紹“C# Marshal類基本概念和入門實例代碼分析”,內容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“C# Marshal類基本概念和入門實例代碼分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

    C# Marshal類基本概念及入門

    • marshal:直譯為“編排”, 在計算機中特 指將數(shù)據(jù)按某種描述格式編排出來,通常來說一般是從非文本格式到文本格式的數(shù)據(jù)轉化。

    • unmarshal是指marshal的逆過程。比如在WebService中,我們需要把java對象以xml方式表示并在網(wǎng)絡間傳輸,把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類基本程序;程序運行結果如下;

    C#?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.Copy實現(xiàn)非托管指針和數(shù)組之間的轉換

    挑戰(zhàn)

    VectorFileIO套件里面 讀寫文件是 IntPtr類型的指針操作。

    原始數(shù)據(jù)是Int32數(shù)組(Int[采樣點,通道]),要寫入文件。

    讀出文件要轉換成反映的物理變量是采集的double波形 (double[通道,采樣點])。

    寫文件

    //初始化緩存指針對應的非托管內存
    IntPtr IntPtr變量 = Marshal.AllocHGlobal(緩存Byte長度);
    采集循環(huán)
    {
        //講采集得到的Int一維數(shù)組拷貝到緩存
        Marshal.Copy(采集一維數(shù)組, 0, IntPtr變量, 數(shù)組sample長度);
        //將緩存寫入矢量文件
        vectorFile.Write(IntPtr變量, 緩存Byte長度);
    }
    //釋放緩存
    Marshal.FreeHGlobal(IntPtr變量);
    //關閉文件
    vectorFile.Close();

    讀文件

    //初始化緩存指針對應的非托管內存
    IntPtr IntPtr變量 = Marshal.AllocHGlobal(緩存Byte長度);
    讀文件循環(huán)
    {
        //矢量文件讀入緩存
        vectorFile.read(IntPtr變量, 緩存Byte長度);
        //緩存拷貝入一維數(shù)組, 數(shù)組長度=緩存Byte長度 /4 (因為4 bytes per int32)
        Marshal.Copy(IntPtr變量, 一維數(shù)組, 0, 數(shù)組sample長度);
        //由Int32 一維數(shù)組 轉換為2維 double數(shù)組使用
    }
    //釋放緩存
    Marshal.FreeHGlobal(IntPtr變量);
    //關閉文件
    vectorFile.Close();

    讀到這里,這篇“C# Marshal類基本概念和入門實例代碼分析”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業(yè)資訊頻道。

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

    AI