C# 中的 void* 是什么? [英] What is void* in C#?

查看:138
本文介绍了C# 中的 void* 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 VC++ 6.00 程序的源代码.我需要将此源代码转换为 C#,但我无法理解此示例中的 (void*) 是什么?

I'm looking through the source of a VC++ 6.00 program.i need convert this source to C# but i can not understand what is (void*) in this example?

glTexImage2D(
     GL_TEXTURE_2D,
     0,
     GL_RGBA8,
     IMAGE_WIDTH,
     IMAGE_HEIGHT,
     0,
     PIXEL_FORMAT,
     GL_UNSIGNED_BYTE,
     (void*)imageData
 );

在这段代码中 imagedata 是一个指针字节*图像数据

in this code imagedata is a pointer byte* imageData

推荐答案

一般情况下,void* 在 C# 代码中会被转换为 IntPtr.

In general, void* would be converted to IntPtr in C# code.

使用更多信息进行.NET/C# 中的 IntPtr 通常表现得像一个不透明的句柄.您不能直接取消引用它,从中获取大小"信息(例如您指向的数组的大小),并且它不会试图告诉您它指向的数据类型 - 或者即使它是一个指针.当您将 C/C++ 代码转换为 C# 并看到 void* 时,应该使用 IntPtr 编写 C# 代码,直到您更好地了解要处理的内容与.

Edit with a bit more information: IntPtr in .NET/C# often behaves like an opaque handle. You can't directly dereference it, obtain "size" information from it (e.g. the size of an array you're pointing at), and it doesn't try to tell you what data type it's pointing at - or even if it's a pointer at all. When you are translating C/C++ code to C# and you see void*, the C# code should be written with IntPtr until you have a better idea what exactly you're dealing with.

pinvoke.net 站点有两个 glTexImage2D 条目,具体取决于图像数据的存储位置.如果图像数据存储在 .NET/C# 中的托管 byte[] 中,则调用传递 byte[] 的版本.如果图像数据存储在非托管内存中,并且您只有一个 IntPtr 到 C# 代码中的数据,则调用传递 IntPtr 的版本.

The pinvoke.net site has two entries for glTexImage2D, depending on where the image data is stored. If the image data is stored in a managed byte[] in .NET/C#, you call the version that passes a byte[]. If the image data is stored in unmanaged memory and you only have an IntPtr to the data in the C# code, you call the version that passes an IntPtr.

来自 pinvoke.net opengl32 的示例:

[DllImport(LIBRARY_OPENGL)] protected static extern void glTexImage2D (
    uint target,
    int level, 
    int internalformat, 
    int width, 
    int height, 
    int border, 
    uint format, 
    uint type, 
    byte[] pixels);
[DllImport(LIBRARY_OPENGL)] protected static extern void glTexImage2D (
    uint target, 
    int level, 
    int internalformat, 
    int width, 
    int height, 
    int border, 
    uint format, 
    uint type, 
    IntPtr pixels);

这篇关于C# 中的 void* 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆