从Delphi XE访问Delphi Prism类库

4

我需要在Delphi XE中访问来自Delphi Prism类库的“Auth”方法:

    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
[...]

这很容易使用CrossTalk实现,但CrossTalk非常昂贵,而且这段代码是为了一个宠物项目。有没有更简单的方法来做到这一点呢?
谢谢提前回答。
1个回答

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

但是在非托管/Win32代码中,返回PAnsiChar并不是一个好主意。谁来释放这个字符串?


这对我来说似乎不太令人信服。 - David Heffernan
1
你是什么意思?这是Delphi Prism的一个特性:http://prismwiki.codegear.com/en/Unmanaged_Exports。 - Lars Truijens
1
Lars,该字符串被编组为ANSI字符串,因此声明必须为function Auth(userName: PAnsiChar; userPassword: PAnsiChar):PAnsiChar; stdcall; external 'ClassLibrary1.dll'; - RRUZ
谢谢RRUZ,我会修改我的回答。 - Lars Truijens
是的,我明白。我必须传递一个在主应用程序中分配的 PChar,并且 dll 只填充内容,这样我就可以在主应用程序中分配和释放内存。 - ioan ghip
1
我也找到了这个答案,看起来这个方法非常完美。谢谢大家。https://dev59.com/HUzSa4cB1Zd3GeqPiwJ4 - ioan ghip

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接