13

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

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
sanaz dadkhah
  • 161
  • 1
  • 1
  • 5

7 Answers7

24

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);
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • 1
    Will that work in this case? I suspect what is being passed in is an array of bytes. – Robert Harvey Mar 20 '13 at 15:39
  • 1+ because you're the first person who gives a real answer mirroring the fact that he want to know the c# equivalent. – Beachwalker Mar 20 '13 at 15:41
  • 1
    @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 Mar 20 '13 at 15:43
  • 1
    @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..." – Sam Harwell Mar 20 '13 at 15:48
  • @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 Mar 20 '13 at 15:56
  • @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#. – Konrad Rudolph Mar 20 '13 at 15:58
8

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#

Community
  • 1
  • 1
Adam Sills
  • 16,896
  • 6
  • 51
  • 56
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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

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.

2

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
  }
}
Beachwalker
  • 7,685
  • 6
  • 52
  • 94
1

Void is actually nothing,

there are three ways of using void:

  • argument: int myFunc(void) -- the function takes nothing.

  • return value: void myFunc(int) -- the function returns nothing

  • Generic data pointer: void* data -- 'data' is a pointer to data of unknown type, and cannot be dereferenced

In this case its a Generic data pointer.

msc
  • 33,420
  • 29
  • 119
  • 214
Jona
  • 1,747
  • 2
  • 16
  • 22
  • 10
    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. – Adam Sills Mar 20 '13 at 15:39
  • @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. – Konrad Rudolph Mar 20 '13 at 15:44
1

void* is a pointer to an object of undefined type. There are various representations for this in C#:

  1. As an unformatted pointer in unsafe code.
  2. As an IntPtr, that is typically used for marshalling pointers between managed and unmanaged objects.
  3. As object for unspecified, but managed objects.
Carsten
  • 11,287
  • 7
  • 39
  • 62