C里使用CopyMemory

C里使用CopyMemorySocket 接收到的 byte 要转换成自定义的 struct 自定义 Struct 转换成 byte 都相当麻烦用循环去转换太浪费时间了 于是想到用 CopyMemory Google 一圈终于搞定下面的代码是在 SnippetCompi 里编译通过的 C 代码 region nbsp Imports nbsp nbsp using nbsp System nbsp nbsp using nbsp S

C#代码
  1. #region Imports   
  2. using System;   
  3. using System.IO;   
  4. using System.Net;   
  5. using System.Xml;   
  6. using System.Text;   
  7. using System.Data;   
  8. using System.Drawing;   
  9. using System.Threading;   
  10. using System.Reflection;   
  11. using System.Collections;   
  12. using System.Net.Sockets;   
  13. using System.Windows.Forms;   
  14. using System.ComponentModel;   
  15. using System.Drawing.Imaging;   
  16. using System.Security.Cryptography;   
  17. using System.Runtime.InteropServices;   
  18. using System.Text.RegularExpressions;  
  19. #endregion  
  20. #region Assembly Info   
  21. [assembly: AssemblyTitle(“Test Application by eglic”)]   
  22. [assembly: AssemblyDescription(“Test Application by eglic”)]   
  23. [assembly: AssemblyConfiguration(“”)]   
  24. [assembly: AssemblyCompany(“eGlic.Com”)]   
  25. [assembly: AssemblyProduct(“Test Application by eglic”)]   
  26. [assembly: AssemblyCopyright(“Copyright (C) eGlic.Com 2007”)]   
  27. [assembly: AssemblyTrademark(“eGlic.Com”)]   
  28. [assembly: AssemblyCulture(“”)]   
  29. [assembly: ComVisible(false)]   
  30. [assembly: AssemblyVersion(“1.0.0.0”)]   
  31. [assembly: AssemblyFileVersion(“1.0.0.0”)]  
  32. #endregion   
  33.   
  34. namespace eGlic   
  35. {  
  36.     #region Application Entrance   
  37.     public class Test{   
  38.         [STAThread]   
  39.         public static void Main() {   
  40.             byte [] buffer=new byte [20];   
  41.             DataGraphHeader header=new DataGraphHeader();   
  42.             string data=“ABCDEFGH”;   
  43.                
  44.             header.Signature=0xFF;   
  45.             header.Protocol.Type=1;   
  46.             header.Protocol.Version=1;   
  47.             header.Type=99;   
  48.             header.SerialID=;   
  49.             header.Size=8;   
  50.                
  51.             Win32API.CopyMemoryEx(buffer,ref header);   
  52.             Win32API.CopyMemoryEx(buffer,12,System.Text.Encoding.ASCII.GetBytes(data),0,8);   
  53.                
  54.             string o=“”;   
  55.             for(int i=0;i

  56.                 if(buffer[i]<10) o+=“0”;   
  57.                 o+=String.Format(“{0:X}”,buffer[i]);   
  58.                 o+=” “;   
  59.             }   
  60.             MessageBox.Show(o,“转成Byte []之后的数据包”,MessageBoxButtons.OK,MessageBoxIcon.Information);   
  61.                
  62.             DataGraphHeader h=new DataGraphHeader();   
  63.             byte [] buf;   
  64.             string d=“”;   
  65.                
  66.             Win32API.CopyMemoryEx(ref h,buffer);   
  67.             buf=new byte [h.Size];   
  68.             Win32API.CopyMemoryEx(buf,buffer,12,h.Size);   
  69.                
  70.             o=“h.Signature=” +h.Signature.ToString()+“/r/n”;   
  71.             o+=“h.Protocol.Type=” +h.Protocol.Type.ToString()+“/r/n”;   
  72.             o+=“h.Protocol.Version=” +h.Protocol.Version.ToString()+“/r/n”;   
  73.             o+=“h.Type=” +h.Type.ToString()+“/r/n”;   
  74.             o+=“h.SerialID=” +h.SerialID.ToString()+“/r/n”;   
  75.             o+=“h.Size=” +h.Size.ToString()+“/r/n”;   
  76.             o+=“附加数据为:”+System.Text.Encoding.ASCII.GetString(buf);   
  77.             MessageBox.Show(o,“解析后数据包”,MessageBoxButtons.OK,MessageBoxIcon.Information);   
  78.         }   
  79.            
  80.     }  
  81.     #endregion  
  82.       
  83.     #region Win32API   
  84.     public class Win32API {   
  85.         [DllImport(“kernel32.dll”, EntryPoint = “RtlMoveMemory”, CharSet = CharSet.Ansi)]   
  86.         public extern static long CopyMemory(IntPtr dest, IntPtr source, int size);  
  87.         #region CopyMemoryEx   
  88.         /// 
      

  89.         /// CopyMemoryEx   
  90.         ///    
  91.         ///  目标缓存区   
  92.         ///  DataGraphPackage   
  93.         /// 
      
  94.         public unsafe static long CopyMemoryEx(byte[] dest, ref DataGraphHeader source) {   
  95.             return CopyMemoryEx(dest, 0,ref source);   
  96.         }   
  97.         /// 
      

  98.         /// CopyMemoryEx   
  99.         ///    
  100.         ///  目标缓存区   
  101.         ///  目标缓存区中的开始位置   
  102.         ///  DataGraphPackage   
  103.         /// 
      
  104.         public unsafe static long CopyMemoryEx(byte[] dest,int DestStart, ref DataGraphHeader source) {   
  105.             IntPtr dp;   
  106.             IntPtr sp;   
  107.             fixed (byte* ds = &dest[DestStart]) {   
  108.                 fixed (DataGraphHeader* sr = &source) {   
  109.                     dp = (IntPtr)ds;   
  110.                     sp = (IntPtr)sr;   
  111.                     return CopyMemory(dp, sp, sizeof(DataGraphHeader));   
  112.                 }   
  113.             }   
  114.         }   
  115.         /// 
      

  116.         /// CopyMemoryEx   
  117.         ///    
  118.         ///  DataGraphPackage   
  119.         ///  源数据缓存   
  120.         /// 
      
  121.         public unsafe static long CopyMemoryEx(ref DataGraphHeader dest, byte[] source) {   
  122.             return CopyMemoryEx(ref dest, source, 0);   
  123.         }   
  124.         /// 
      

  125.         /// CopyMemoryEx   
  126.         ///    
  127.         ///  DataGraphPackage   
  128.         ///  源数据缓存   
  129.         /// 
      
  130.         public unsafe static long CopyMemoryEx(ref DataGraphHeader dest, byte[] source,int SourceStart) {   
  131.             IntPtr dp;   
  132.             IntPtr sp;   
  133.             fixed (DataGraphHeader* ds = &dest) {   
  134.                 fixed (byte* sr = &source[SourceStart]) {   
  135.                     dp = (IntPtr)ds;   
  136.                     sp = (IntPtr)sr;   
  137.                     return CopyMemory(dp, sp, sizeof(DataGraphHeader));   
  138.                 }   
  139.             }   
  140.         }   
  141.         /// 
      

  142.         /// CopyMemoryEx   
  143.         ///    
  144.         ///  目标缓存   
  145.         ///  源数据   
  146.         ///  要从源数据中复制的长度   
  147.         /// 
      
  148.         public unsafe static long CopyMemoryEx(byte[] dest, byte[] source, int size) {   
  149.             return CopyMemoryEx(dest, 0, source, 0, size);   
  150.         }   
  151.         /// 
      

  152.         /// CopyMemoryEx   
  153.         ///    
  154.         ///  目标缓存   
  155.         ///  源数据   
  156.         ///  源数据缓存中开始位置   
  157.         ///  要从源数据中复制的长度   
  158.         /// 
      
  159.         public unsafe static long CopyMemoryEx(byte[] dest, byte[] source, int SourceStart,int size) {   
  160.             return CopyMemoryEx(dest, 0, source, SourceStart, size);   
  161.         }   
  162.         /// 
      

  163.         /// CopyMemoryEx   
  164.         ///    
  165.         ///  目标缓存   
  166.         ///  目标缓存中开始复制的位置   
  167.         ///  源数据   
  168.         ///  源数据缓存中开始位置   
  169.         ///  要从源数据中复制的长度   
  170.         /// 
      
  171.         public unsafe static long CopyMemoryEx(byte[] dest,int DestStart, byte[] source, int SourceStart, int size) {   
  172.             IntPtr dp;   
  173.             IntPtr sp;   
  174.             fixed (byte* ds = &dest[DestStart]) {   
  175.                 fixed (byte* sr = &source[SourceStart]) {   
  176.                     dp = (IntPtr)ds;   
  177.                     sp = (IntPtr)sr;   
  178.                     return CopyMemory(dp, sp, size);   
  179.                 }   
  180.             }   
  181.         }  
  182.         #endregion   
  183.     }  
  184.     #endregion   
  185.        
  186.     [StructLayout(LayoutKind.Sequential)]   
  187.     public struct ProtocolInfo {   
  188.         public byte Type;   
  189.         public byte Version;   
  190.     }   
  191.        
  192.     [StructLayout(LayoutKind.Sequential)]   
  193.     public struct DataGraphHeader {   
  194.         public byte Signature;              //包头:    1字节   
  195.         public ProtocolInfo Protocol;       //协议:    2字节   
  196.         public byte Type;                   //包类型:  1字节   
  197.         public uint SerialID;               //包序号    4字节   
  198.         public int Size;                    //包尺寸    4字节   
  199.     }   
  200. }  

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/209182.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 上午9:51
下一篇 2026年3月19日 上午9:51


相关推荐

  • git拉取代码冲突了怎么解决_github拉取代码慢

    git拉取代码冲突了怎么解决_github拉取代码慢问题描述在idea通过git从develop分支拉取最新代码时,又遇到了git冲突,特此记录一下原因分析:大部分原因都是其他同事更改了某些文件然后本地也更改了同样的文件导致冲突,所以每天上班先拉取一下最新代码,这样会大大减少冲突发生的可能(别问我怎么知道的)解决方案:如下图所示:我们点击merge去合并即可,解决冲突,不建议直接选择acceptyours或者accepttheirs会导致代码覆盖…

    2022年10月20日
    5
  • 设计原则之里氏替换原则详解

    设计原则之里氏替换原则详解一 里氏替换原则定义定义 如果对每一个类型为 T1 的对象 O1 都有类型为 T2 的对象 O2 使得所有以 T1 定义的所有程序 P 在所有的对象 O1 都替换成 O2 时 程序 P 的行为没有发生任何变化 那么类型 T2 是类型 T1 的子类型 通俗理解就是 子类可以扩展父类的功能 但不能改变父类原有的功能 有以下几个引申含义 子类可以实现父类的抽象方法 但不能覆盖父类的非抽象方法 子类中可以增加自己特有的方法 当子类的方法重载父类的方法时 方法的前置条件 方法的输入 入参 要比父类的入参更宽松 当子类的方法实现父类的方法时

    2026年3月18日
    2
  • 软通动力推出OpenClaw企业级AI Agent一键部署解决方案

    软通动力推出OpenClaw企业级AI Agent一键部署解决方案

    2026年3月14日
    1
  • 智谱 AI 的意外之举:CodeGeex 插件集成 Claude 模型

    智谱 AI 的意外之举:CodeGeex 插件集成 Claude 模型

    2026年3月12日
    3
  • 从0到1快速上手 seedance2.0 (保姆级入门教程,小白也能玩转)

    从0到1快速上手 seedance2.0 (保姆级入门教程,小白也能玩转)

    2026年3月13日
    1
  • meshgrid方法

    meshgrid方法目录meshgrid绘制曲面图三维网络meshgridmeshgrid和mesh方法的差别在于是否会画出栅格线绘制曲面图生成绘制3D图形所需的网格数据。因为在计算机中进行绘图操作时,往往需要一些采样点,然后根据这些采样点来绘制出整个图形。涉及到x、y这两组数据可以看做是在Oxy平面内对坐标进行采样得到的坐标对(x,y)。[X,Y]=meshgrid…

    2022年6月3日
    49

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号