打印
[实用程序源码及工具软件]

Ada语言调用动态链接库DLL例程源码(独家在21ic发表)

[复制链接]
148|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hotpower|  楼主 | 2023-9-19 14:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 hotpower 于 2023-9-20 09:47 编辑
with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Interfaces;   use Interfaces;
with Interfaces.C; use Interfaces.C;
with System;       use System;
with Ada.Unchecked_Conversion;

procedure Main is
   type byte_array is array (size_t range <>) of aliased Interfaces.C.unsigned_char;
   function LoadLibrary(
     File_Name : in Interfaces.C.Char_Array)
     return System.Address;
   pragma Import(Stdcall, LoadLibrary, "LoadLibrary", "LoadLibraryA");

   function GetProcAddress(
     Module        : in System.Address;
     Function_Name : in Char_Array)
     return System.Address;
   pragma Import(stdcall, GetProcAddress, "GetProcAddress", "GetProcAddress");
   type fnPEA256Init is access function return Boolean;
   pragma Convention (Stdcall, fnPEA256Init);
   function PEA256Init is new Ada.Unchecked_Conversion (Address, fnPEA256Init);

   type fnPEA256Open is access function return System.Address;
   pragma Convention (Stdcall, fnPEA256Open);
   function PEA256Open is new Ada.Unchecked_Conversion (Address, fnPEA256Open);

   type fnPEA256EncryptUserKey is access function(handle : in System.Address; keystr: in char_array; keybuff: in char_array; keyfile: in char_array) return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256EncryptUserKey);
   function PEA256EncryptUserKey is new Ada.Unchecked_Conversion (Address, fnPEA256EncryptUserKey);

   type fnPEA256DecryptUserKey is access function(handle : in System.Address; keystr: in char_array; keybuff: in char_array; keyfile: in char_array) return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256DecryptUserKey);
   function PEA256DecryptUserKey is new Ada.Unchecked_Conversion (Address, fnPEA256DecryptUserKey);

   type fnPEA256EncryptBytes is access function(handle : in System.Address; bytes: in byte_array; len: in Interfaces.C.unsigned_long; subkey: in Interfaces.C.unsigned_long) return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256EncryptBytes);
   function PEA256EncryptBytes is new Ada.Unchecked_Conversion (Address, fnPEA256EncryptBytes);

   type fnPEA256DecryptBytes is access function(handle : in System.Address; bytes: in byte_array; len: in Interfaces.C.unsigned_long; subkey: in Interfaces.C.unsigned_long) return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256DecryptBytes);
   function PEA256DecryptBytes is new Ada.Unchecked_Conversion (Address, fnPEA256DecryptBytes);

   type fnPEA256EncryptBytesEx is access function(handle : in System.Address; inbytes: in byte_array; outbytes: out byte_array; len: in Interfaces.C.unsigned_long; subkey: in Interfaces.C.unsigned_long) return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256EncryptBytesEx);
   function PEA256EncryptBytesEx is new Ada.Unchecked_Conversion (Address, fnPEA256EncryptBytesEx);

   type fnPEA256DecryptBytesEx is access function(handle : in System.Address; inbytes: in byte_array; outbytes: out byte_array; len: in Interfaces.C.unsigned_long; subkey: in Interfaces.C.unsigned_long) return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256DecryptBytesEx);
   function PEA256DecryptBytesEx is new Ada.Unchecked_Conversion (Address, fnPEA256DecryptBytesEx);

   type fnPEA256Close is access function(handle : in System.Address) return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256Close);
   function PEA256Close is new Ada.Unchecked_Conversion (Address, fnPEA256Close);

   type fnPEA256Test is access function return Boolean;
   pragma Convention (Stdcall, fnPEA256Test);
   function PEA256Test is new Ada.Unchecked_Conversion (Address, fnPEA256Test);
   type fnPEA256Error is access function return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256Error);
   function PEA256Error is new Ada.Unchecked_Conversion (Address, fnPEA256Error);
   type fnPEA256Version is access function return Interfaces.C.unsigned_long;
   pragma Convention (Stdcall, fnPEA256Version);
   function PEA256Version is new Ada.Unchecked_Conversion (Address, fnPEA256Version);
   Library : System.Address;
   fpPEA256Init : System.Address;
   fpPEA256Open : System.Address;
   fpPEA256Close : System.Address;
   fpPEA256EncryptUserKey : System.Address;
   fpPEA256DecryptUserKey : System.Address;
   fpPEA256EncryptBytes : System.Address;
   fpPEA256DecryptBytes : System.Address;
   fpPEA256EncryptBytesEx : System.Address;
   fpPEA256DecryptBytesEx : System.Address;
   fpPEA256Test : System.Address;
   fpPEA256Version : System.Address;
   fpPEA256Error : System.Address;
   handle : System.Address;
   val : Interfaces.C.unsigned_char;
   subkey : Interfaces.C.unsigned_long;
   byteArray : byte_array(0..3);
   ArrayEncrypt : byte_array(0..3);
   ArrayDecrypt : byte_array(0..3);
begin
   Library := LoadLibrary(To_C("D:\\libpea256x64\\libpea256x64.dll"));
   if Library /= System.Null_Address then
      fpPEA256Init := GetProcAddress(Library, To_C("PEA256Init"));
      if fpPEA256Init /= System.Null_Address then
         begin
            if PEA256Init(fpPEA256Init).all then
               fpPEA256Test := GetProcAddress(Library, To_C("PEA256Test"));
               if PEA256Test(fpPEA256Test).all then
                  Put("PEA256Test()!");New_Line;
               end if;
               fpPEA256Version := GetProcAddress(Library, To_C("PEA256Version"));
               subkey := PEA256Version(fpPEA256Version).all;
               Put(subkey'Image);New_Line;
               fpPEA256Open := GetProcAddress(Library, To_C("PEA256Open"));
               fpPEA256Close := GetProcAddress(Library, To_C("PEA256Close"));
               handle := PEA256Open(fpPEA256Open).all;
               if handle /= System.Null_Address then
                  Put("PEA256Open()!");New_Line;
                  for I in 0..3 loop
                     --byteArray(I) := I;
                     null;
                  end loop;
                  byteArray(0) := 0;
                  byteArray(1) := 1;
                  byteArray(2) := 2;
                  byteArray(3) := 3;
                  ArrayEncrypt(0) := 0;
                  ArrayEncrypt(1) := 1;
                  ArrayEncrypt(2) := 2;
                  ArrayEncrypt(3) := 3;
                  fpPEA256EncryptUserKey := GetProcAddress(Library, To_C("PEA256EncryptUserKey"));
                  subkey := PEA256EncryptUserKey(fpPEA256EncryptUserKey).all(handle, To_C("123"), To_C(""), To_C(""));
                  Put(subkey'Image);New_Line;
                  for I in 0..3 loop
                     --Put(byteArray(0)'Image);
                     null;
                  end loop;
                  fpPEA256EncryptBytes := GetProcAddress(Library, To_C("PEA256EncryptBytes"));
                  subkey := PEA256EncryptBytes(fpPEA256EncryptBytes).all(handle, byteArray, 4, subkey);
                  Put(subkey'Image);New_Line;
                  Put(byteArray(0)'Image);
                  Put(byteArray(1)'Image);
                  Put(byteArray(2)'Image);
                  Put(byteArray(3)'Image);
                  New_Line;
                  fpPEA256DecryptUserKey := GetProcAddress(Library, To_C("PEA256DecryptUserKey"));
                  subkey := PEA256DecryptUserKey(fpPEA256DecryptUserKey).all(handle, To_C("123"), To_C(""), To_C(""));
                  Put(subkey'Image);New_Line;
                  fpPEA256DecryptBytes := GetProcAddress(Library, To_C("PEA256DecryptBytes"));
                  subkey := PEA256DecryptBytes(fpPEA256DecryptBytes).all(handle, byteArray, 4, subkey);
                  Put(subkey'Image);New_Line;
                  Put(byteArray(0)'Image);
                  Put(byteArray(1)'Image);
                  Put(byteArray(2)'Image);
                  Put(byteArray(3)'Image);
                  New_Line;
                  fpPEA256EncryptUserKey := GetProcAddress(Library, To_C("PEA256EncryptUserKey"));
                  subkey := PEA256EncryptUserKey(fpPEA256EncryptUserKey).all(handle, To_C(""), To_C("0123456789ABCDEF0123456789ABCDEF"), To_C(""));
                  Put(subkey'Image);New_Line;
                  fpPEA256EncryptBytesEx := GetProcAddress(Library, To_C("PEA256EncryptBytesEx"));
                  subkey := PEA256EncryptBytesEx(fpPEA256EncryptBytesEx).all(handle, ArrayEncrypt, ArrayDecrypt, 4, subkey);
                  Put(subkey'Image);New_Line;
                  Put(ArrayDecrypt(0)'Image);
                  Put(ArrayDecrypt(1)'Image);
                  Put(ArrayDecrypt(2)'Image);
                  Put(ArrayDecrypt(3)'Image);
                  New_Line;
                  fpPEA256DecryptUserKey := GetProcAddress(Library, To_C("PEA256DecryptUserKey"));
                  subkey := PEA256DecryptUserKey(fpPEA256DecryptUserKey).all(handle, To_C(""), To_C("0123456789ABCDEF0123456789ABCDEF"), To_C(""));
                  Put(subkey'Image);New_Line;
                  fpPEA256DecryptBytesEx := GetProcAddress(Library, To_C("PEA256DecryptBytesEx"));
                  subkey := PEA256DecryptBytesEx(fpPEA256DecryptBytesEx).all(handle, ArrayDecrypt, ArrayEncrypt, 4, subkey);
                  Put(subkey'Image);New_Line;
                  Put(ArrayEncrypt(0)'Image);
                  Put(ArrayEncrypt(1)'Image);
                  Put(ArrayEncrypt(2)'Image);
                  Put(ArrayEncrypt(3)'Image);
                  New_Line;
               end if;
               fpPEA256Error := GetProcAddress(Library, To_C("PEA256Error"));
               subkey := PEA256Error(fpPEA256Error).all;
               Put(subkey'Image);New_Line;
               subkey := PEA256Close(fpPEA256Close).all(handle);
               Put(subkey'Image);New_Line;
            end if;
            Put("PEA256COM!!!");New_Line;
         end;
      else
         -- TODO Handle Error
         null;
      end if;
   end if;
   Put("Hello, World!");New_Line;
   Put_Line("Hello, World!");
end Main;


使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1538

主题

21697

帖子

506

粉丝