Problems with Delphi: Pchar argument Xojo

Hello everyone.
I am translating this code in Delphi to Xojo, the library in question would encrypt choosing between multiple files encryption algorithms. My question is how should I treat a PChar Xojo argument. I will teach you the original code and what lead.


[code]function DirectPack (fromf: PChar; tof: PChar): integer; stdcall; external ‘darkcryptrtc.WCX’;
function DirectUnpack (archive: PChar; todir: PChar): integer; stdcall; external ‘darkcryptrtc.WCX’;
function DirectMode (algo: PChar; key: PChar; direct: boolean): integer; stdcall;external ‘darkcryptrtc.WCX’;

procedure ListStart; stdcall; external ‘darkcryptrtc.WCX’;
function ListNext (var name: PChar): integer; stdcall;external ‘darkcryptrtc.WCX’;

procedure DirectUnpackString (input: PChar; Res: PChar); stdcall;external ‘darkcryptrtc.WCX’;
procedure DirectPackString (input: PChar; Res: PChar); stdcall;external ‘darkcryptrtc.WCX’;

procedure TForm12.Button1Click(Sender: TObject);
var fromf, tof, algo, key: string;
if opendialog1.Execute then
if savedialog1.Execute then

fromf := OpenDialog1.FileName;
tof := SaveDialog1.FileName;
algo := desx.Text;
key := keyt.Text;

if DirectPack(pchar(fromf),pchar(tof)) = 0 then ShowMessage(‘Done’)
else ShowMessage(‘Error’)


[code] Declare Function DirectPack lib “d:\MyProject\darkcryptrtc.WCX”(fromf as Ptr, tof as Ptr) as Integer
Declare Function DirectUnpack lib “d:\MyProject\darkcryptrtc.WCX”(archive as Ptr,tof as Ptr) as Integer
Declare Function DirectMode lib “d:\MyProject\darkcryptrtc.WCX”(algo as Ptr, key as Ptr, direct as Boolean) as Integer
Declare Sub ListStar lib “d:\MyProject\darkcryptrtc.WCX”
Declare Function ListNext lib “d:\MyProject\darkcryptrtc.WCX”(name as Ptr) as Integer
Declare Sub DirectUnpackString lib “d:\MyProject\darkcryptrtc.WCX”(input as Ptr, res as Ptr)
Declare Sub DirectPackString lib “d:\MyProject\darkcryptrtc.WCX”(input as Ptr, res as Ptr)

dim dlgOpen As OpenDialog
dim fOpen As FolderItem
dim fromf, tof, algo, key as String

dlgOpen = New OpenDialog
dlgOpen.InitialDirectory = SpecialFolder.Documents
dlgOpen.Title = “File to Crypt”
dlgOpen.Filter = FileTypes1.Any
fOpen = dlgOpen.ShowModal

If fOpen <> Nil Then
Dim dlgSave as New SaveAsDialog
dim fSave As FolderItem
dlgSave.promptText=“Prompt Text”
dlgSave.SuggestedFileName=“Suggested Filename”
dlgSave.Title=“Destine Path”

if fSave <> Nil Then
  fromf = fOpen.NativePath
  tof = fSave.NativePath
  algo = cbAlgo.Text
  key = tfKey.Text
  // MemoryBlock
  dim malgo As  MemoryBlock = algo
  dim mkey As  MemoryBlock = key
  if DirectMode(malgo, mkey, true) = 0 Then <---- compiler error
  dim mfromf as MemoryBlock = fromf
  dim mtof as MemoryBlock = tof
  if DirectPack(mfromf, mtof) = 0 then
    MsgBox "Crypt file"
    MsgBox "Error crypt"
  end if
end if

//User Cancelled
End If

I intend to share the results with the community, thank you very much for your help.

Ptr looks right
However the Delphi boolean is 1 byte and that may be the issue on the line you highlighted
I’d try defining that parameter in the declare as an int8 and passing either 1 or 0

[quote=173860:@Javier Navarro] dim malgo As MemoryBlock = algo
dim mkey As MemoryBlock = key
Your declare is expecting a PChar (PString in Xojo), so that should be

dim malgo as new MemoryBlock(algo.LenB+1)
dim mkey as new MemoryBlock(key.LenB+1)
malgo.PString(0) = algo
mkey.PString(0) = key

Actually a PChar in Delphi is a “pointer to a char” (well a wide char)

So it’s a WString? In any event, a normal string won’t work.

Thank you very much Norman and Tim were very accurate in their recommendations. I tried this and it worked great.

// MemoryBlock

dim malgo As new MemoryBlock(len(algo))
dim mkey As new MemoryBlock(len(key))
malgo = algo
mkey = key

if DirectMode(malgo, mkey, true) = 0 Then
dim mfromf as new MemoryBlock(len(fromf))
dim mtof as new MemoryBlock(len(tof))
mfromf = fromf
mtof = tof
if DirectPack(mfromf, mtof) = 0 then
MsgBox “Crypt file”
MsgBox “Error crypt”
end if
end if[/code]
Soon I’ll upload the results