- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在编写一些代码,这些代码反射(reflect)了从对 native dll 的调用中编码返回的结构。一些结构包含指向以 null 结尾的指针数组的 IntPtr* 字段。这些字段需要特殊处理。反射(reflection)结构时,我可以识别这些字段,因为它们由自定义属性标记。
以下说明了我正在尝试做的事情:
public void ProcessStruct(object theStruct)
{
foreach (FieldInfo fi in theStruct.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
{
if (fi.FieldType.IsPointer && IsNullTermArray(fi))
{
//Has the custom attribute, commence processing of
//IntPtr* pointing to null-terminated array
ProcessIntPtr(fi.GetValue(theStruct));
}
else{/*..Other Processing..*/ }
}
}
public void unsafe ProcessIntPtr(IntPtr* ptr)
{
//Iterate over the array and process the elements
//There are pointer operations here.
}
问题是
fi.GetValue(theStruct)
返回一个对象,我显然不能将其直接传递给 ProcessIntPtr()。我无法更改 ProcessIntPtr() 的签名以接受对象,因为那样我将无法执行我需要的指针操作。显然,我也不能从对象转换为 IntPtr*。
可以使用哪些技术来处理这个问题?
最佳答案
虽然您可能无法从 Object 转换为 IntPtr*,但您可以转换为 IntPtr。请记住,IntPtr*
只是一个指针指针。所以你可以到达第一个指针,然后将它投回去。
var ptr1 = (IntPtr)(fi.GetValue(theStruct));
var ptr2 = (IntPtr*)(ptr1);
关于C#:通过反射检索和使用 IntPtr*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1126034/
我正在尝试使用 BitmapData 类?并且在将 IntPtr 值分配给 BitmapData.Scan0 属性时遇到一些问题。这是我的代码: var data = bmp.LockBits(new
我正在尝试使用 BitmapData 类?并且在将 IntPtr 值分配给 BitmapData.Scan0 属性时遇到一些问题。这是我的代码: var data = bmp.LockBits(new
我有两个 IntPtr 值指向一些 length 字节的数据区域。 length 可能有 200k 到 400k 的数量级。 int length = /* ..*/ IntPtr ptrSrc =
我在网上到处都看过,似乎找不到能够解决该问题的特定答案。从字面上看,这是我得到的唯一错误,只有在解决此问题后才能构建可执行文件。 int num3 = new VAMemory(Data.proces
我不知道 Copy(IntPtr[], Int32, IntPtr, Int32) 方法是如何工作的。我虽然它可以将多个 IntPtr 中包含的数据复制到一个 IntPtr 中(如 MSDN 所述),
我有一个类的以下代码。这是一个类的初始化。 第三方动态链接库 [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatib
我刚刚了解到 IntPtr.Zero 无法与 default(IntPtr) 相提并论。谁能告诉我为什么? IntPtr.Zero == new IntPtr(0) -> "could not eva
这两种说法有什么区别吗: IntPtr myPtr = new IntPtr(0); IntPtr myPtr2 = IntPtr.Zero; 我见过许多使用 PInvoke 的示例,如果 myPtr
这个问题已经有答案了: What makes more sense - char* string or char *string? [duplicate] (13 个回答) 已关闭 9 年前。 本能地
我知道 Marshal.SizeOf() 和 sizeof() 有多么根本的不同。但是在 IntPtr 的情况下,无论 CPU 架构如何,它们都不会总是返回完全相同的东西吗? 最佳答案 一方面,有原始
我需要实现一个与找到的解决方案非常相似的解决方案 here ,写于 2009 年,用于检索文件的唯一标识符。具体来说,我正在查看答案的 ApproachB()函数和以下行(重写了一点以反射(refle
是否可以通过 Skype 用户窗口获取处理程序并使用 SendMessage(whdl,BM_CLICK,intptr.zero,intrptr.zero,intptr.zero) 单击发送文件或调用
经过一些研究我翻译了 pHandle = IntPtr.Zero; 到德尔福 var pHandle: Pointer; 当我在 Delphi 中中断调试时,我得到 $314328,当我在 C s
运营商的description on MSDN有一句话: An exception is only thrown if the value of value requires more bits th
这个函数在一个循环中。当我运行程序时,带有 IntPtr 的行给我内存问题,我已经删除 [],但它仍然没有解决内存问题,有人可以帮忙吗?谢谢 void showImage(IplImage *img,
直到最近,我还认为在 P/Invoke 中编码 IntPtr 只会涉及“blitting”操作;即简单地从例如复制指针C++ 到 C#。 然而,我最近与某人交谈时提到,事情远不止于此。他特别提到 In
我目前正在编写一些代码,这些代码反射(reflect)了从对 native dll 的调用中编码返回的结构。一些结构包含指向以 null 结尾的指针数组的 IntPtr* 字段。这些字段需要特殊处理。
我正在尝试在 Ubuntu 14.04 上使用 Mono 执行函数 P/Invoke: [DllImport(@"libRT", CharSet = CharSet.Ansi, CallingConv
我有一个类定义如下: [StructLayout(LayoutKind.Sequential)] public class OUR_MEM_STR {
我遇到了一段C#源码如下 int* ptr = ...; int w = ...; int* ptr3 = ptr + (IntPtr)w; CS0019: Operator '+' cannot
我是一名优秀的程序员,十分优秀!