-1

I have the following C function

NTSTATUS
SendBuffer(
_In_ UINT32 Number,
_In_ PWCHAR wszBuffer
)

and the following c# method

[DllImport(@"cfunc.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SendBuffer")]
    public static extern long SendBuffer(uint Number, [MarshalAsAttribute(UnmanagedType.LPWStr)] string wszBuffer);

and in c# i try to call the function with

string buffer = "one two treee";
SendBuffer((uint)x, buffer);

But i receive an System.AccessViolationException when i run the program. How can i fix this problem?

Thank you.

Martin Rezyne
  • 445
  • 3
  • 9
  • 24

1 Answers1

-1

A WChar is 16 bits so you need to send Uint16[]. See code below.

            string buffer = "one two treee";
            UInt16[] data = buffer.ToCharArray().Select(x => (UInt16)x).ToArray();
jdweng
  • 33,250
  • 2
  • 15
  • 20