Dav*_*vio 1 c# pinvoke marshalling
我有一个本机方法,需要一个指针来写出一个dword(uint).
现在我需要从(Int)指针获取实际的uint值,但Marshal类只有读取(签名)整数的方便方法.
如何从指针获取uint值?
我搜索了问题(和谷歌),但找不到我需要的东西.
示例(不工作)代码:
IntPtr pdwSetting = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint)));
        try
        {
            // I'm trying to read the screen contrast here
            NativeMethods.JidaVgaGetContrast(_handleJida, pdwSetting);
            // this is not what I want, but close
            var contrast = Marshal.ReadInt32(pdwSetting);
        }
        finally
        {
            Marshal.FreeHGlobal(pdwSetting);
        }
来自本机函数的返回值是0到255之间的双字,其中255是完全对比.
您可以简单地将其转换为uint:
uint contrast = (uint)Marshal.ReadInt32(pdwSetting);
例如:
int i = -1;
uint j = (uint)i;
Console.WriteLine(j);
输出4294967295。
根据您是否使用usafe代码,您甚至可以执行以下操作:
static unsafe void Method()
{
    IntPtr pdwSetting = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint)));
    try
    {
        NativeMethods.JidaVgaGetContrast(_handleJida, pdwSetting);
        var contrast = *(uint*)pdwSetting;
    }
    finally
    {
        Marshal.FreeHGlobal(pdwSetting);
    }
}
注意,那个C++函数指针就好
void (*GetContrastPointer)(HANDLE handle, unsigned int* setting);
可以被整理为
[DllImport("*.dll")]
void GetContrast(IntPtr handle, IntPtr setting); // most probably what you did
但也作为
[DllImport("*.dll")]
void GetContrast(IntPtr handle, ref uint setting);
它可以让你编写代码
uint contrast = 0; // or some other invalid value
NativeMethods.JidaVgaGetContrast(_handleJida, ref contrast);
它在性能和可读性方面都很出色.
| 归档时间: | 
 | 
| 查看次数: | 5136 次 | 
| 最近记录: |