What is void* in C#?

21,264

Solution 1

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

Solution 2

It's most typically an IntPtr in C#.

Here is an example of how to call this function from C#:

Bitmap bitmap = new Bitmap("blah");
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, 4, bitmap.Width, bitmap.Height, 0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, bitmap.Scan0);

if(bitmap != null) {
    bitmap.Dispose();
}

Source: http://www.gamedev.net/topic/193827-how-to-use-glglteximage2d--using-csgl-library-in-c/

And if what you have is a byte[] that contains a valid image, you can load it into a bitmap as such:

public static Bitmap BytesToBitmap(byte[] byteArray)
{
    using (MemoryStream ms = new MemoryStream(byteArray))
    {
        Bitmap img = (Bitmap)Image.FromStream(ms);
        return img;
    }
}

If that gives you trouble, or your data is not something that the Win32 libs can handle, you can use Marshal to give you an IntPtr from a byte[] directly.

IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length);

// Call your function here, passing unmanagedPointer to it

Marshal.FreeHGlobal(unmanagedPointer);

From: How to get IntPtr from byte[] in C#

Solution 3

As Jonathan D has explained in his data, void* is C’s way of denoting a generic type that isn’t directly usable. It’s C’s way of implementing a generic interface, and although it has a direct equivalent in C# (System.IntPtr) you will probably want to use something else because C# has better ways of dealing with generic data, and it’s uncertain that you need to pass generic data here.

Since you’re passing image data to the function there are several possibilities what void* would represent in C#:

  • byte[] to pass the raw image data to the function
  • System.Drawing.Color[,] if you are handling higher-level point data (unlikely)
  • System.Drawing.Image if you want to handle the image data on a higher level; this is probably a good idea if you can get away with it
  • IntPtr if you have a low-level buffer and the function internally passes the data to a C API without processing it itself.

Without more information on how you will use the code and what parts you are translating it’s impossible to tell which of these types you need here.

Solution 4

As already said IntPtr is the equivalent.

You can use pointers only in unsafe context in c#, e.g.:

void MyMethod(IntPtr ptr)
{
  unsafe
  {
      var vptr = (void*) ptr;
      // do something with the void pointer
  }
}

Solution 5

it's casting imageData to the type (void*), so basically telling the compiler to take this value and treat it as though it were a (void*)

from my experience, void* is a pointer to a non-specific type.


As far as converting your example to c#, then you don't really have to worry too much about how things are being cast in the c++. Just make sure that you're passing the right types into the corresponding c# function.

Share:
21,264
sanaz dadkhah
Author by

sanaz dadkhah

Updated on April 02, 2020

Comments

  • sanaz dadkhah
    sanaz dadkhah about 4 years

    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

  • Yinda Yin
    Yinda Yin about 11 years
    Will that work in this case? I suspect what is being passed in is an array of bytes.
  • Adam Sills
    Adam Sills about 11 years
    That doesn't answer his question at all, I'm confused by the up-votes. He wants to know what void* equivalent is in C#, not an explanation of what void is.
  • Beachwalker
    Beachwalker about 11 years
    1+ because you're the first person who gives a real answer mirroring the fact that he want to know the c# equivalent.
  • Konrad Rudolph
    Konrad Rudolph about 11 years
    @Beachwalker Maybe that’s a bit premature. You cannot do many things with an IntPtr and depending on the usage another type is probably way more appropriate here.
  • Konrad Rudolph
    Konrad Rudolph about 11 years
    @Adam As one of the upvoters, let me explain: I’m not entirely happy with this answer (for the reason you mentioned) but it’s the first one that actually explains what void* is, in a concise fashion. How to translate it to C# depends on its usage, and OP hasn’t been that forthcoming about it.
  • Sam Harwell
    Sam Harwell about 11 years
    @RobertHarvey That's why I said "in general." There are a plethora of specific cases, each of which should be handled in an appropriate manner. Interop code is not an area where you want to be making decisions from an "I suspect..."
  • Beachwalker
    Beachwalker about 11 years
    @KonradRudolph Samples? IntPtr is imho the most common equivalent when doing interop without using the unsafe keyword. Why premature? Anyone else didn't answered the question about an equivalent in c# and just wrote what a (void) pointer is.
  • Beachwalker
    Beachwalker about 11 years
    Good sample as answer because it is in the same context as the question sample (both OpenGL).
  • Konrad Rudolph
    Konrad Rudolph about 11 years
    @Beachwalker Why premature: because the details are simply missing, OP should provide them (see my comment on Jonathan’s good answer). As for IntPtr being the most common equivalent for interop – I don’t think that’s the case (you often don’t need the genericity, you can just use the object directly and use the appropriate type) but even assuming it is, we don’t know whether OP is dealing with interop or whether they want to have the whole code in C#.