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

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

问题描述

我期待通过VC ++ 6.00 program.i源需要这个源转换成C#,但我不明白是什么(无效*)在这个例子吗?



  glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA8,
IMAGE_WIDTH,
IMAGE_HEIGHT,
0,
PIXEL_FORMAT,
GL_UNSIGNED_BYTE,
(无效*)为imageData
);

在此代码为imageData是一个指向
字节*为imageData

解决方案

在一般情况下,无效* 将被转换为的IntPtr 在C#代码



编辑与位的更多信息:在.NET 的IntPtr / C#常行为类似于不透明句柄。你不能直接引用这些,从中获取大小的信息(如你在指着一个数组的大小),它并不试图告诉你什么数据类型它的指向 - 或者即使它是一个指针在所有。当你正在翻译的C / C ++代码到C#,你看到无效* ,C#代码应与的IntPtr 写入直到你有一个更好的主意你处理什么用。



该pinvoke.net网站有两个条目 glTexImage2D ,根据图像数据的存储位置。如果图像数据存储在一个托管字节[] 在.NET / C#中,调用传递版本字节[] 。如果图像数据存储在非托管内存,你只需要一个的IntPtr 在C#代码中的数据,调用传递版本的的IntPtr从的



例子。 HTML> pinvoke.net opengl32

 函数[DllImport(LIBRARY_OPENGL)保护静态外部无效glTexImage2D(
UINT目标,
INT水平,
INT internalformat,
INT宽度,
INT高度,
INT边框,
uint格式,
UINT类型,
字节[]像素);
函数[DllImport(LIBRARY_OPENGL)保护静态外部无效glTexImage2D(
UINT目标,
INT水平,
INT internalformat,
INT宽度,
INT高度,
INT边框,
uint格式,
UINT类型,
IntPtr的像素);


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
 );

in this code imagedata is a pointer byte* imageData

解决方案

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

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.

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.

Examples from 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天全站免登陆