相关疑难解决方法(0)

向IntPtr添加偏移量

我正在寻找一种在C#或.NET中执行指针操作的方法.

我想做一些非常简单的事情

有一个指针IntPtr我想获得指向前2个字节的IntPtr对象.

我读了一些帖子,说这个愚蠢的片段会起作用......

IntPtr ptr = new IntPtr(oldptr.ToInt32() + 2);
Run Code Online (Sandbox Code Playgroud)

但我怀疑这个语句是否也适用于64位机器(因为在那里寻址是64位).

我发现这种优雅的方法来添加偏移量,但不幸的是只在.NET 4.0中http://msdn.microsoft.com/en-us/library/system.intptr.add%28VS.100%29.aspx

.net c# pinvoke interop

36
推荐指数
5
解决办法
2万
查看次数

如何将 IntPtr 转换为数组?

如何将 转换IntPtr为数组。实际上我从非托管 dll 调用了该函数。它返回IntPtr。现在我需要将其转换为数组。请任何人给出一个想法。代码片段如下。

Unmanaged function declared

[DllImport("NLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe IntPtr N_AllocPt1dArray(NL_INDEX n, ref stacks S); 
Run Code Online (Sandbox Code Playgroud)

调用函数

void Function1()
{
     IntPtr PPtr=N_AllocPt1dArray(n, ref S);
}
Run Code Online (Sandbox Code Playgroud)

现在我需要转换PPtr为数组(数组是demo[])。其中 demo 是由

public unsafe struct demo 
{            
    public int x ;
    public int y ;
    public int z ;
}demo DEMO;
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
1
解决办法
4392
查看次数

标签 统计

c# ×2

.net ×1

interop ×1

pinvoke ×1