Marshalling Unmanaged DLL

  • Thread starter Thread starter Jim shedden
  • Start date Start date
J

Jim shedden

Hi,

I've really flailed at this today, and read many posts and MSDN help
areas. I would really appreciate a sanity check.

The DLL is for OziExplorer API, the function declaration from the
documentation is:

function oziGetExePath(var Path:pansichar;
var DataLenth:integer):integer;stdcall;

I believe that PAnsiChar is a Delphi type for "pointer to Ansi Char"
and can be used similar to LPSTR to point at null terminated strings.

In general the API states return values as -1 for timeout, 0 for
completion, or other numbers as indicated by the specific funtion.
This function should return the path to the EXE file, so I expect it
should return only 0 or -1.

In C# I write the following:
________

StringBuilder sb = new StringBuilder(512);

[DllImport("OziAPI.dll", CharSet = CharSet.Ansi, CallingConvention =
CallingConvention.StdCall)]

static extern int oziGetExePath(ref StringBuilder s, ref int length);

public string getOziExePath()
{
int size = 0;
int ret;

ret=oziGetExePath(ref sb, ref size);
//if (ret != 0)
// throw new Exception("Could not get Ozi exe Path");
return (sb.ToString());
}
___________

1. While the return value is correct, the "ret" value is 1, so my API
error trap will always throw an exception. I'm a bit leery about
ignoring return values.

2. In examples I do not see the StringBuilder passed by reference. I
do not get the correct result if I do not pass by reference. I also
tried defining the path parameter as String. It also required a "ref"
and the behaviour is identical to using StringBuilder.

3. I think that the unmanaged code allocates a buffer, and I don't
want the CLR to try and release it. The API provides a "close"
function to free up storage, as it reuses it internally. I tried some
tricks with "IntPtr" and "Marshal.PtrToStringAuto()" that did not seem
to work either.

I would be happy to learn more from anyone in the group! Thanks in
advance for any help.

Regards,
Jim
 
Id try again with IntPtr
Did you try Marshal.PtrToStringAnsi (using the size returned by the function)?

maybe you could try Marshal.Copy to get the data into a byte array and see
whats in it.

Does size return a value?

Scott
 
sfawcett said:
Id try again with IntPtr
Did you try Marshal.PtrToStringAnsi (using the size returned by the function)?

maybe you could try Marshal.Copy to get the data into a byte array and see
whats in it.

Does size return a value?

Hi Scott,

Thanks for the reply. I do not have a problem getting the data back in
both the string and integer parameters. I was looking for a sanity
check on the most accestable way to do it, as I am new to C#.

My problem is the return value of the function. It doesn't make sense
to get back expected data, with a return value inconsistent with
proper execution. I've never used this DLL before, but I imagine the
author is reliable.

Regards,
Jim Shedden
 
Back
Top