Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace ClassLibrary1;
  2.  
  3. interface
  4.  
  5. uses System.Runtime.InteropServices;
  6.  
  7. type
  8.   [StructLayout(LayoutKind.Sequential)]
  9.   TestRecord = public record
  10.   public
  11.     X: Int32;
  12.     Y: Int32;
  13.   end;
  14.  
  15.   TestClass = public class
  16.   private
  17.   protected
  18.   public
  19.       [UnmanagedExport('Test', CallingConvention.StdCall)]
  20.       class procedure Test(out B: TestRecord);
  21.       [UnmanagedExport('TestString', CallingConvention.StdCall)]
  22.       class procedure TestString(out AString: WideString);
  23.       [UnmanagedExport('TestStringRes', CallingConvention.StdCall)]
  24.       class function TestStringRes: String;
  25.   end;
  26.  
  27. implementation
  28.  
  29. class procedure TestClass.Test(out B: TestRecord);
  30. begin
  31.     B.X := 25;
  32.     B.Y := -10;
  33. end;
  34.  
  35. class procedure TestClass.TestString(out AString: WideString);
  36. begin
  37.     AString  := 'A wide string';
  38. end;
  39.  
  40. class function TestClass.TestStringRes: String;
  41. begin
  42.     Result := 'A wider string';
  43. end;
  44.  
  45. end.
  46.  
  47.  
  48.  
  49.  
  50.  
  51. unit untTest1;
  52.  
  53. interface
  54.  
  55. uses
  56.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  57.   Dialogs, StdCtrls;
  58.  
  59. type
  60.   TForm1 = class(TForm)
  61.     btn1: TButton;
  62.     procedure btn1Click(Sender: TObject);
  63.   private
  64.     { Private declarations }
  65.   public
  66.     { Public declarations }
  67.   end;
  68.  
  69.   TTestRecord = packed record
  70.     X, Y: integer;
  71.   end;
  72.  
  73.   procedure Test(out B: TTestRecord);stdcall; external 'ClassLibrary1.dll' name 'Test';
  74.   procedure TestString(out AString: Widestring);stdcall; external 'ClassLibrary1.dll' name 'TestString';
  75.   function TestStringRes: Widestring;stdcall; external 'ClassLibrary1.dll' name 'TestStringRes';
  76.  
  77. var
  78.   Form1: TForm1;
  79.  
  80. implementation
  81.  
  82. {$R *.dfm}
  83.  
  84. procedure TForm1.btn1Click(Sender: TObject);
  85. var
  86.   B: TTestRecord;
  87.   AString: widestring;
  88. begin
  89.   Test(B);
  90.   ShowMessage(IntToStr(B.X));
  91.   ShowMessage(IntToStr(B.Y));
  92.   ShowMessage(TestStringRes);
  93.   SetLength(AString, 100);
  94.   TestSTring(AString);
  95.   ShowMessage(AString);
  96. end;
  97.  
  98. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement