zoukankan      html  css  js  c++  java
  • C#访问非托管内存

    示例1:分配一个新的内存地址给新变量

    Point p;
    // Initialize unmanged memory to hold the struct.
    IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));
    // Copy the struct to unmanaged memory.
    Marshal.StructureToPtr(p, pnt, false);
    
    // Create another point.
    Point anotherP;
    
    // Set this Point to the value of the 
    // Point in unmanaged memory. 
    anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));
    Marshal.FreeHGlobal(pnt);


    示例2:将一个字符串地址转换为字符串,以及获取字符串的地址:

    IntPtr lpFileName;
    string filename = Marshal.PtrToStringUni(lpFileName);
    lpFileName = Marshal.StringToHGlobalUni(filename);
  • 相关阅读:
    Python之sys & os
    1161
    1142
    P1599 货币
    P1547逆转,然后再见
    P1629八
    P1753HackSon的趣味题
    Problem 2233 ~APTX4869
    1269
    1091. Tmutarakan Exams
  • 原文地址:https://www.cnblogs.com/nanfei/p/5036583.html
Copyright © 2011-2022 走看看