Search code examples
c#arrayspointerspinvokeunsafe

C# unsafe type -> char*[] , getting a pointer on char array


Have:

[DllImport("OpenAL32.dll")]
static extern void alcOpenDevice(char*[] devicename);

Wanna send the name to this function like smth that:

char[] data = "Hello!".ToCharArray();
char*[] txt = &data;

But getting the errors:

  • cannot implicitly convert type char[] * to char * []

    (funny error, because C# compiler refuses to define char[] * in /unsafe mode also :) )

  • Cannot take the address of, get the size of, or declare a pointer to a managed type (char[])

PS
When does char become managed? It's a struct, isn't it?

public struct Char : IComparable, IConvertible, IComparable<char>, IEquatable<char>

Although compiler showed info about declaring the pointer to a managed type (char[]). I can only suggest that when the type is an array CLR may present it like a managed type, but it sounds very crazy.


Solution

  • alcOpenDevice does not take char*[] or char**, it takes char*, which you should specify as a string. It also returns a handle.

        [DllImport("OpenAL32.dll", CharSet = CharSet.Ansi)]
        static extern IntPtr alcOpenDevice(string devicename);