4

I need to access in Delphi XE the method "Auth" from this Delphi Prism class library:

    namespace ClassLibrary1;

    interface

    uses
      System,
      System.IO,
      System.Security.Cryptography,
      System.Runtime.InteropServices,
      System.Text;

    type
      ConsoleApp = public class
      private
        class method hashMe(input: string): string;
        class method Encrypt(clearText: string; Password: string; Salt: array of byte; iteration: Integer): string;
        class method Encrypt(clearData: array of byte; Key: array of byte; IV: array of byte): array of byte;
        class method Encrypt(clearData: array of byte; Password: string; Salt: array of byte; iteration: integer): array of byte;
        class method Decrypt(cipherText: string; Password: string; Salt: array of byte; iterations: Integer): string;
        class method Decrypt(cipherData: array of byte; Password: string; Salt: array of byte; iterations: integer): array of byte;
        class method Decrypt(cipherData: array of byte; Key: array of byte; IV: array of byte): array of byte;
      protected
      public
        [UnmanagedExport('Auth')]
        class method Auth(userName: String; userPassword: String): String;
      end;

    implementation
[...]

This is very easy with CrossTalk, but CrossTalk is very expensive and this code is for a pet project. Any easy way to do this?

TIA

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
ioan ghip
  • 1,202
  • 2
  • 16
  • 27

1 Answers1

5
function Auth(userName: PAnsiChar; userPassword: PAnsiChar): PAnsiChar; stdcall; external 'ClassLibrary1.dll' 

But returning a PAnsiChar is not really a good idea in unmanaged/win32 code. Who is going to free the string?

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • This doesn't seem very convincing to me. – David Heffernan May 27 '11 at 20:06
  • 1
    What do you mean? It's a feature of Delphi Prism: http://prismwiki.codegear.com/en/Unmanaged_Exports. – Lars Truijens May 27 '11 at 20:12
  • 1
    Lars the string is marshaled as an ANSI string , so the declaration must be `function Auth(userName: PAnsiChar; userPassword: PAnsiChar):PAnsiChar; stdcall; external 'ClassLibrary1.dll';` – RRUZ May 27 '11 at 20:26
  • Thank you RRUZ, will change my answer – Lars Truijens May 27 '11 at 20:33
  • Yes, I understand. I have to pass a PChar that is allocated in the main application, and the dll just fills the content, that way I can allocate and free the memory in the main application. – ioan ghip May 27 '11 at 20:40
  • 1
    I also found this answer and looks like this works perfect. Thank's all. http://stackoverflow.com/questions/2273141/how-can-i-pass-a-delphi-string-to-a-prism-dll – ioan ghip May 27 '11 at 21:35