Dll returning null terminated string, how to handle this ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wilfried Mestdagh

    Dll returning null terminated string, how to handle this ?

    Hi,

    I have a DLL using stdcall and a function of it is returning a null
    terminated string. How do I declare this in C# (VS2005) ?

    I try things like:

    [DllImport(dllNa me)]
    public static extern [MarshalAs(Unman agedType.LPStr)] string
    Version();

    But keep getting syntax errors, so clearly I miss something. I also tryed
    char[] but this give me exception error.

    --
    rgds, Wilfried
    freeware applications, delphi components, interesting links

  • Wilfried Mestdagh

    #2
    RE: Dll returning null terminated string, how to handle this ?

    Hi,

    I tryed another approach, to just copy something to a string as argument,
    but have also strange result.

    char[] ver = new char[21];
    ApiDll.Version( ver, 20);

    And the dll:

    public static class ApiDll
    {
    [DllImport(dllNa me)]
    public static extern void Version([MarshalAs(Unman agedType.LPArra y)]
    char[] c, int len);

    In the DLL I see the value given with the call. There I copy the string to
    the pointer, but in C# the result value is the same. It seems that a copy is
    passed to the dll instead of the pointer.

    In the DLL (in Delphi):

    procedure Version(c: PChar; Len: integer): PChar; export; stdcall;
    begin
    c := StrPLCopy(c, 'hihihihi', 10);
    end;

    But the "hihihi" never visisble in C# calling program.

    I tryed with the 'out' argument, but this gives me exception errors. So I
    think I have to go back to the return value. But dont kno whow..;.

    --
    rgds, Wilfried
    freeware applications, delphi components, interesting links

    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Dll returning null terminated string, how to handle this ?

      A "return" pointer must be declared as an IntPtr like this:

      public static extern IntPtr Version();

      and the IntPtr must be explicitely marshaled by using
      Marshal.PtrToSt ringXXX, where XXX depends on type of string (ansi or
      unicode).

      Willy.


      "Wilfried Mestdagh" <WilfriedMestda gh@discussions. microsoft.comwr ote in
      message news:C6D71852-6506-409D-A27B-B7840A0081CC@mi crosoft.com...
      | Hi,
      |
      | I have a DLL using stdcall and a function of it is returning a null
      | terminated string. How do I declare this in C# (VS2005) ?
      |
      | I try things like:
      |
      | [DllImport(dllNa me)]
      | public static extern [MarshalAs(Unman agedType.LPStr)] string
      | Version();
      |
      | But keep getting syntax errors, so clearly I miss something. I also tryed
      | char[] but this give me exception error.
      |
      | --
      | rgds, Wilfried
      | http://www.mestdagh.biz


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: Dll returning null terminated string, how to handle this ?

        How does this relate to your previous question?, this looks like a different
        function.
        Maybe it's time to read the Pinvoke stuff in MSDN, and to to take a look at
        the samples too.

        Willy.

        "Wilfried Mestdagh" <WilfriedMestda gh@discussions. microsoft.comwr ote in
        message news:72D6EC45-2B42-4664-8078-87A0C127AF6F@mi crosoft.com...
        | Hi,
        |
        | I tryed another approach, to just copy something to a string as argument,
        | but have also strange result.
        |
        | char[] ver = new char[21];
        | ApiDll.Version( ver, 20);
        |
        | And the dll:
        |
        | public static class ApiDll
        | {
        | [DllImport(dllNa me)]
        | public static extern void
        Version([MarshalAs(Unman agedType.LPArra y)]
        | char[] c, int len);
        |
        | In the DLL I see the value given with the call. There I copy the string to
        | the pointer, but in C# the result value is the same. It seems that a copy
        is
        | passed to the dll instead of the pointer.
        |
        | In the DLL (in Delphi):
        |
        | procedure Version(c: PChar; Len: integer): PChar; export; stdcall;
        | begin
        | c := StrPLCopy(c, 'hihihihi', 10);
        | end;
        |
        | But the "hihihi" never visisble in C# calling program.
        |
        | I tryed with the 'out' argument, but this gives me exception errors. So I
        | think I have to go back to the return value. But dont kno whow..;.
        |
        | --
        | rgds, Wilfried
        | http://www.mestdagh.biz


        Comment

        • Wilfried Mestdagh

          #5
          Re: Dll returning null terminated string, how to handle this ?

          Hi,

          Thank you it works. My second question was because I tryed another way to
          get forward. It works but I wonder wy the original value is not changed.
          Where have I written to ? I try to explain:

          This is my call:
          public string version()
          {
          char[] ver = new char[21];
          string v = Marshal.PtrToSt ringAnsi(ApiDll .Version(ver, 20));
          return v + " | " + new string(ver);
          }

          in v is copied the version witch is copy to it in the DLL. But original
          value 'ver' is empty. But both point to same memory location. So where has
          code in dll written to ?

          [DllImport(dllNa me)]
          public static extern IntPtr
          Version([MarshalAs(Unman agedType.LPArra y)] char[] c, int len);

          And code in DLL is simple:

          function Version(c: PChar; Len: integer): PChar; export; stdcall;
          begin
          c := StrPLCopy(c, VersionInfo, Len);
          Result := c;
          end;

          c is pointer same pointer as ver in C# calling program. Result is return
          value and is same pointer. It is the IntPtr in C# calling program.

          But in C# calling program ver is empty. So to witch pointer has DLL copy the
          string ?

          --
          rgds, Wilfried
          freeware applications, delphi components, interesting links

          Comment

          • Wilfried Mestdagh

            #6
            Re: Dll returning null terminated string, how to handle this ?

            Hi,

            I found it. I declared a char[] where it has to be a byte[]. I still get
            confused with these 16 bit chars...

            now v and ver are same string and second question is solved too :)

            --
            rgds, Wilfried
            freeware applications, delphi components, interesting links

            Comment

            Working...