• "Delphi" DLL

    From Andy Gorman@1:229/426.52 to All on Sat May 8 15:43:38 2021
    I'm creating a DLL that is going to get a value passed into it (path to a file). I am able to pass in a value, but it's getting truncted in the DLL.

    Here's the code:

    library SimpleLib;

    Uses
    SysUtils;

    Var
    Path : String;
    o : Text;

    function MySucc(AVal : PWideString; pLen : Integer) : PWideString; stdcall; begin
    SetLength(String(AVal), pLen);
    Assign(o, 'c:\temp\simp.txt');
    Rewrite(o);
    Write(o,String(AVal));
    Close(o);
    Result := AVal;
    end;

    exports
    MySucc;
    End.

    I'm passing in a path (i.e. c:\mystic\data\users.dat) and the length of said path. Everytime I try to read the path, I end up with "c:\mystic\da" and I'm not sure how to get around this. I'm very new to Pascal (I'm a C# developer generally) and so I'm sure I'm missing something simple, but don't know what that is.

    Any help is greatly appreciated.

    Andy
    --- Mystic BBS v1.12 A47 2021/05/03 (Windows/64)
    * Origin: The Pot O'Gold - bbs.thepotogold.net:4888 (1:229/426.52)
  • From Alexander Grotewohl@1:120/616 to Andy Gorman on Sat May 8 20:02:46 2021
    On 08 May 2021, Andy Gorman said the following...

    I'm creating a DLL that is going to get a value passed into it (path to a file). I am able to pass in a value, but it's getting truncted in the DLL.

    Here's the code:

    library SimpleLib;

    Uses
    SysUtils;

    Var
    Path : String;
    o : Text;

    function MySucc(AVal : PWideString; pLen : Integer) : PWideString; stdcall; begin
    SetLength(String(AVal), pLen);
    Assign(o, 'c:\temp\simp.txt');
    Rewrite(o);
    Write(o,String(AVal));
    Close(o);
    Result := AVal;
    end;

    exports
    MySucc;
    End.

    are you calling this from C# then? it would seem SetLength() is doing something to a pointer that is not technically a 'PWideString'. (a PWideString has a known length, see https://wiki.freepascal.org/Character_and_string_types)

    String() on the other hand (depending on compiler settings) would force AVal to a old pascal style string where string[0] is a byte for the length. so if the length is stored in memory at str[-1] and str[0] (this is not something you'd normally interact with.. these are managed types and the compiler handles this), you're only getting one byte of the length.

    is there a reason you're using PWideString? SysUtils has a StrPas function that makes it fairly easy to do things like UnicodeStringVar:=StrPas(AVal); where AVal is a PWideChar .. which is usually enough for me to interact with C/C++ from pascal.

    sorry for the lack of code.. but i'd be unable to test it if it's called from other than pascal ;) but hopefully it's enough

    --- Mystic BBS v1.12 A47 2021/04/20 (Windows/32)
    * Origin: cold fusion - cfbbs.net - grand rapids, mi (1:120/616)
  • From Andy Gorman@1:229/426.52 to Alexander Grotewohl on Sat May 8 18:50:04 2021
    On 08 May 2021, Alexander Grotewohl said the following...

    is there a reason you're using PWideString? SysUtils has a StrPas
    function that makes it fairly easy to do things like UnicodeStringVar:=StrPas(AVal); where AVal is a PWideChar .. which is usually enough for me to interact with C/C++ from pascal.

    Thanks for the direction. I got it to accept the full path by switching PWideChar. I'm now working on getting a return string from the DLL.

    are you calling this from C# then? it would seem SetLength() is doing

    I am calling this from C# because I'm trying to make my life more challenging (and it's what I know) <G>

    Thanks,
    Andy
    --- Mystic BBS v1.12 A47 2021/05/03 (Windows/64)
    * Origin: The Pot O'Gold - bbs.thepotogold.net:4888 (1:229/426.52)