What is void * in C #?

I am looking at the source of VC ++ 6.00 program.i need to convert this source to C #, but I can’t understand what is it (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 byte * imageData pointer p>

+5
source share
7 answers

In general, it void*will be converted to IntPtrin C # code.

: IntPtr .NET/# . , "" (, , ), , , . C/++ #, void*, # IntPtr, , .

pinvoke.net glTexImage2D, , . byte[] .NET/#, , byte[]. IntPtr #, , 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);
+16

IntPtr #.

#:

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

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

byte[], , :

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

-, Win32, , IntPtr byte[] .

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

// Call your function here, passing unmanagedPointer to it

Marshal.FreeHGlobal(unmanagedPointer);

: IntPtr [] #

+6

,

void:

  • : int myFunc(void) - .

  • : void myFunc(int) -

  • : void* data - 'data' -

.

+2

, void* Cs , . Cs # (System.IntPtr), , , - , # , , .

, , void* #:

  • byte[]
  • System.Drawing.Color[,], ()
  • System.Drawing.Image, ; , , , .
  • IntPtr, , API API .

, , , .

+2

, IntPtr .

#, :

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

imageData (void*), , (void*)

, void* .


As for converting your example to C #, you don’t have to worry too much about how things are done in C ++. Just make sure you pass the correct types to the appropriate C # function.

0
source

void*is a pointer to an object of type undefined. For this, there are various views in C #:

  • Like an unformatted pointer in unsafe code.
  • Like IntPtr, which is commonly used to sort pointers between managed and unmanaged objects.
  • As objectfor unspecified but manageable objects.
0
source

All Articles