Thanks for the help.
I think I understand what you're saying but since I'm pretty new with this
type of programming, how would that look like?
So far, I have:
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern IntPtr z4date();
....
public static IntPtr getDate()
{
IntPtr ptr ;
ptr = z4date(); // ptr.ToString() = "2"
string str = Marshal.PtrToStringAnsi(ptr);
MessageBox.Show (str); //str is null
return str;
}
But the debugger tells me that str is null, so I know I'm doing something
wrong. If I don't move it to a string (eg. MessageBox.Show
(ptr.ToString());), it displays a "2".
Angel
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:ulQjXIHAEHA.552@TK2MSFTNGP11.phx.gbl...[color=blue]
> Angel,
>
> You will not be able to do this. The reason that it doesn't work with
> your definitions is because the pointer that is being returned by the
> function is of an indeterminate nature. True, the documentation states[/color]
that[color=blue]
> it returns eight bytes, but there is nothing in the code that indicates[/color]
that[color=blue]
> is the case.
>
> To get around this, you should declare your return type as an IntPtr,
> and then pass that to the static PtrToStringAnsi method on the Marshal
> class.
>
> The only thing to be considerate of is the fact that the function
> allocated memory and returned it. To prevent a leak, you will have to
> dispose of the memory pointed to by the IntPtr. How you do this is
> dependent on how the DLL allocates it, and will require another call[/color]
through[color=blue]
> P/Invoke.
>
> Also, you do have to use the attribute for every declaration.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> -
mvp@spam.guard.caspershouse.com
>
> "Angel" <none> wrote in message
> news:u5etX9GAEHA.2300@tk2msftngp13.phx.gbl...[color=green]
> > Hi again,
> > I'm trying to call functions from a proprietary DLL but it's turned out[/color][/color]
to[color=blue][color=green]
> > be more difficult than I thought.
> >
> > I have this W32.DLL which was written in C by USPS. They don't provide[/color][/color]
the[color=blue][color=green]
> > code so I only have the documentation.
> > I'm trying to call a function called z4date that, according to the docs,
> > returns the date as "an 8-byte character string in the "YYYYMMDD"[/color][/color]
format".[color=blue][color=green]
> > When I run it with this code I've written , I get "Can not marshal[/color][/color]
return[color=blue][color=green]
> > value".
> >
> > My real concern is that, if I'm having trouble with these functions that
> > return simple data types, how am I going to interact with functions that
> > return user-defined data types?
> >
> > This is what I've written in the class so far:
> >
> > ...
> > using System.Runtime.InteropServices;
> > namespace ZM7
> > {
> > /// <summary>
> > /// Summary description for AMS.
> > /// </summary>
> > public class AMS
> > {
> > [DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
> > public static extern int z4open(); //another function from the dll
> > [DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")] /* Do I need to use
> > DllImport for every fiunction I'm using? */
> > public static extern byte[] z4date();
> > ...
> >
> > public byte[] getDate()
> > {
> > Byte[] myDate;
> > myDate = new Byte[8];
> > myDate = z4date(); //function call where I receive error "Can[/color][/color]
not[color=blue][color=green]
> > marshal return value"
> > return myDate();
> > }
> >
> > Any help is appreciated.
> >
> >[/color]
>
>[/color]