美文网首页Spring.Net
C#笔记,C#和C之间交互,非托管代码

C#笔记,C#和C之间交互,非托管代码

作者: eleksbai | 来源:发表于2019-05-23 10:22 被阅读0次

c# 开机启动配置
windows 程序开机启动配置
在注册表
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
路径下新增一个键值指向启动程序路径

注释解释

/****************************************************************************
 * @fn           EnumDevices
 * @brief        枚举可连接设备
 * @param        nLayerType       IN         传输层协议:1-GigE; 4-USB;可叠加
 * @param        stDeviceList     OUT        设备列表
 * @return       成功:0;错误:错误码
 ****************************************************************************/

依次为

  1. 方法名
  2. 方法概述
  3. 参数 输入 (传递进去被使用,不用管了)
  4. 参数 输出 (承载处理的中间结果,有些结果不是返回的)
  5. 返回

c# ref修饰符

参数说明
在方法的参数列表中使用 ref 关键字时,它指示参数按引用传递,而非按值传递。 ref 关键字让形参成为实参的别名,这必须是变量。
换而言之,对形参执行的任何操作都是对实参执行的。
若要使用 ref 参数,方法定义和调用方法均必须显式使用 ref 关键字
传递到 ref 或 in 形参的实参必须先经过初始化,然后才能传递

// 调用
float fExposure = 0;
nRet = m_pcCameraOperator.GetFloatValue("ExposureTime", ref fExposure);
// 实现
public int GetFloatValue(string strKey, ref float pfValue){...}

// 实现, 引用结构体
public int SaveImage(ref CMvSmartSDK.MVSC_IMG_CONVERT_PARAM pstImageConvertParam){...}

// 结构体定义 
 public struct MVSC_IMG_CONVERT_PARAM{...}

MarshalAs属性和使用

MarshalAs属性指示如何在托管代码和非托管代码之间封送数据。
MarshalAs属性和使用

public struct MV_SC_DEVICE_INFO_LIST
{
    public UInt32 nDeviceNum;
    
    // 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = MV_SC_MAX_DEVICE_NUM)]
    public MV_SC_DEVICE_INFO[] astDeviceInfo;
};

UnmanagedType数据类型
指定如何将参数或字段封送到非托管代码。

什么是托管代码和非托管代码
托管代码是基于.net框架的代码,有框架进行内存资源管理
非托管代码是不受框架控制的代码,如调dll的c代码

关键字

Thread 类型
C#中关于线程的管理

IntPtr 类型
实例的IntPtr中广泛使用System.IO.FileStream类来保存的文件句柄。

C#调用C语言的API时一般把void *指针转换成IntPtr
C#指针转换介绍
对象转换代码

将数据从托管对象编组到非托管内存块方法 (反向操作Marshal.PtrToStructure )
Marshal.StructureToPtr
marshal.structuretoptr
将非托管内存转存到托管内存
Marshal.PtrToStructure
托管内存变化分析:

  1. 实例一个对象,占用300M内存(只有在真正使用的时候才会自动去申请内存)
  2. 对象传递到非托管代码,实际是整个复制,此时内存占用600M
  3. 返回一个新的对象(需定义这个对象的结构)
    anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));
    ,将非托管代码的对象整个复制出来,此时内存占用900M

官网源码示例

using System;
using System.Runtime.InteropServices;
using System.Threading;

public struct Point
{
    public int x;
    public int y;
    public byte[] buffer;
    // byte[] buffer = new byte[MAX_IMAGE_FRAME_SIZE];
}
    class Example
{

    static void Main()
    {

        // Create a point struct.
        Point p;
        p.x = 1;
        p.y = 1;
        Thread.Sleep(3000);
        // 100M
        p.buffer = new byte[1024 * 1024 * 300]; 

        Console.WriteLine("The value of first point is " + p.x + " and " + p.y + "." + p.buffer.Length);
        
        Thread.Sleep(1000);
        // Initialize unmanged memory to hold the struct.
        IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));
        Thread.Sleep(1000);
        try
        {

            // Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, false);
            Thread.Sleep(1000);
            // Create another point.
            Point anotherP;

            // Set this Point to the value of the 
            // Point in unmanaged memory. 
            anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));
            Thread.Sleep(1000);
            Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + ".");
            Console.WriteLine(".." + p.buffer.Length);
            Thread.Sleep(1000);
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }

}

Graphics C#内置绘图类
C# 绘图相关

相关文章

网友评论

    本文标题:C#笔记,C#和C之间交互,非托管代码

    本文链接:https://www.haomeiwen.com/subject/oqckzqtx.html