Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const char * return value -- how to marshal?

540 views
Skip to first unread message

James F. Bellinger

unread,
Sep 17, 2002, 10:51:33 PM9/17/02
to
Hello :)

I have a method that looks like this:

[DllImport(OpenGLLibrary.OpenGLDriver, EntryPoint="glGetString")]
public static extern string GetString(GLConstant param);

However, any time I call it, I get a NullReferenceException, which I
think indicates that the calling method is wrong. The return value is a
const char * to a static place in memory, containing an ANSI string. I have
tried [return: MarshalAs(UnmanagedType.LPStr)] but that didn't help. :-/ Any
ideas?

Thanks :)

Jim Bellinger


Mattias Sjögren

unread,
Sep 18, 2002, 5:04:36 AM9/18/02
to
Jim,

> However, any time I call it, I get a NullReferenceException, which I
>think indicates that the calling method is wrong. The return value is a
>const char * to a static place in memory, containing an ANSI string. I have
>tried [return: MarshalAs(UnmanagedType.LPStr)] but that didn't help. :-/ Any
>ideas?

Make the return type an IntPtr, then use Marshal.PtrToStringAnsi() to
retrieve the string. If the return type is set to string, the runtime
will try to release the string memory.

Mattias

===
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

James F. Bellinger

unread,
Sep 18, 2002, 5:10:13 PM9/18/02
to
Works perfectly! :-) Thanks! :-)

Now if only I can find a way to use C# for calling unmanaged function
pointers gotten at runtime (wglGetProcAddress ;-) I can add multitexturing.
:-)
As it stands though, this will let me support GL_NV_blend_square. :)

Thanks a bunch :)
Jim Bellinger

"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:OhFq$HvXCHA.2496@tkmsftngp09...

0 new messages