exporting function with parm pointer to char string

A

Angel

I'm exporting a C-style function call with this syntax:

int getDate(char *date);

I'm trying to export to my c# app like this:

[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern int getDate(System.IntPtr ptr);
public void getDate()
{
IntPtr ptr = new IntPtr();
string str = "";
getDate(ptr); //// ERROR: Object reference not set to an instance of
an object
str = Marshal.PtrToStringAnsi(ptr);
}

What Am I doing wrong?

Thanks.
 
M

Mattias Sjögren

What Am I doing wrong?

You're passing in a null pointer. Try it this instead

public static extern int getDate(StringBuilder ptr);



Mattias
 
A

Angel

That worked.

Thank you.

Mattias Sjögren said:
You're passing in a null pointer. Try it this instead

public static extern int getDate(StringBuilder ptr);



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top