網(wǎng)站首頁(yè) 編程語言 正文
C# Marshal類基本概念及入門
-
marshal
:直譯為“編排”, 在計(jì)算機(jī)中特 指將數(shù)據(jù)按某種描述格式編排出來,通常來說一般是從非文本格式到文本格式的數(shù)據(jù)轉(zhuǎn)化。 -
unmarshal
是指marshal的逆過程。比如在WebService中,我們需要把java對(duì)象以xml方式表示并在網(wǎng)絡(luò)間傳輸,把java對(duì)象轉(zhuǎn)化成xml片段的過程就是marshal.
微軟對(duì)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.
提供一個(gè)方法集合,分配非托管內(nèi)存,拷貝非托管內(nèi)存塊,轉(zhuǎn)換托管和非托管類型,以及一些和非托管代碼交互的雜類方法;
這是Marshal類的基本功能;.net一共包含四個(gè)Marshal類,每個(gè)都有一些方法;
下面來看一個(gè)Marshal類基本程序;程序運(yùn)行結(jié)果如下;
代碼如下:
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實(shí)現(xiàn)非托管指針和數(shù)組之間的轉(zhuǎn)換
挑戰(zhàn)
VectorFileIO套件里面 讀寫文件是 IntPtr類型的指針操作。
原始數(shù)據(jù)是Int32數(shù)組(Int[采樣點(diǎn),通道]),要寫入文件。
讀出文件要轉(zhuǎn)換成反映的物理變量是采集的double波形 (double[通道,采樣點(diǎn)])。
寫文件
//初始化緩存指針對(duì)應(yīng)的非托管內(nèi)存 IntPtr IntPtr變量 = Marshal.AllocHGlobal(緩存Byte長(zhǎng)度); 采集循環(huán) { ?? ?//講采集得到的Int一維數(shù)組拷貝到緩存 ?? ?Marshal.Copy(采集一維數(shù)組, 0, IntPtr變量, 數(shù)組sample長(zhǎng)度); ?? ?//將緩存寫入矢量文件 ?? ?vectorFile.Write(IntPtr變量, 緩存Byte長(zhǎng)度); } //釋放緩存 Marshal.FreeHGlobal(IntPtr變量); //關(guān)閉文件 vectorFile.Close();
讀文件
//初始化緩存指針對(duì)應(yīng)的非托管內(nèi)存 IntPtr IntPtr變量 = Marshal.AllocHGlobal(緩存Byte長(zhǎng)度); 讀文件循環(huán) { ?? ?//矢量文件讀入緩存 ?? ?vectorFile.read(IntPtr變量, 緩存Byte長(zhǎng)度); ?? ?//緩存拷貝入一維數(shù)組, 數(shù)組長(zhǎng)度=緩存Byte長(zhǎng)度 /4 (因?yàn)? bytes per int32) ?? ?Marshal.Copy(IntPtr變量, 一維數(shù)組, 0, 數(shù)組sample長(zhǎng)度); ?? ?//由Int32 一維數(shù)組 轉(zhuǎn)換為2維 double數(shù)組使用 } //釋放緩存 Marshal.FreeHGlobal(IntPtr變量); //關(guān)閉文件 vectorFile.Close();
總結(jié)
原文鏈接:https://blog.csdn.net/bcbobo21cn/article/details/116512451
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-11-11 C#實(shí)現(xiàn)控制電腦注銷,關(guān)機(jī)和重啟_C#教程
- 2022-04-01 Failed to execute goal com.github.eirslettfrontend
- 2022-06-09 ASP.NET?Core中的Options選項(xiàng)模式_基礎(chǔ)應(yīng)用
- 2024-02-27 credentials to a set of origins, list them explici
- 2022-05-13 C語言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)解析_C 語言
- 2022-09-06 Golang實(shí)現(xiàn)http重定向https_Golang
- 2022-08-18 Git多人協(xié)同開發(fā)緊急修復(fù)線上bug操作指南_相關(guān)技巧
- 2022-07-10 使用Docker安裝RabbitMQ
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支