Riera
Riera

Reputation: 369

Declare C function with char* parameter in c#

Using C#.net, how I declare and use this function inside a .dll file:

DEMO_API Boolean DEMO_CALL GetVersion(Char* versionBuffer,
    UInt16* versionLengthBuffer);

Thanks in advance!

Upvotes: 1

Views: 558

Answers (2)

Riera
Riera

Reputation: 369

This is the solution I found:

    [DllImport("DEMO.dll",
        SetLastError = true,
        CallingConvention = CallingConvention.Cdecl)]
    public extern static byte GetVersion(
        [MarshalAs(UnmanagedType.LPStr)] StringBuilder versionBuffer,
        [MarshalAs(UnmanagedType.LPWStr)] StringBuilder versionLengthBuffer);

Upvotes: 0

nyxthulhu
nyxthulhu

Reputation: 9752

C# doesn't have the notion of string pointers as such, you can just use a string / int.

Basically to define your own you'd have to do somethign slightly different, but it wouldn't be compatible.

public bool GetVersion (string versionBuffer, int versionLengthBuffer) 

If your using an existing item have a look at pInvoke for a list of possible signatures.

Upvotes: 2

Related Questions