http://blog.csdn.net/goodname008/archive/2004/08/21/80827.aspx ; 帮我看一下在delphi中怎么动态调用这个dll?

解决方案 »

  1.   

    使用API:
    LoadLibrary
    GetProcAddress
    FreeLibraryhttp://lysoft.7u7.net
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    type
      TStartKey=function(lpdwVirtualKey:LPDWORD;VKLength: integer):boolean;stdcall ;
    var
      StopKey:function:pchar;stdcall ;
      StartKey: TStartKey;
      Handle: THandle;
      dwVK2: array of Dword;
      VKLength2: dword;
      i_Length : integer;
      b_Ok: boolean;
      pKey:LPDWORD;
      begin
    try
      i_Length:=9 ;
      VKLength2:=i_Length;
      SetLength(dwVK2, i_Length);
      dwVK2[0]:=ord('A');
      dwVK2[1]:=ord('B');
      dwVK2[2]:=ord('C');
      dwVK2[3]:=37;
      dwVK2[4]:=38;
      dwVK2[5]:=39;
      dwVK2[6]:=40;
      dwVK2[7]:=91;
      dwVK2[8]:=92;
      pKey:=@dwVK2;
      b_ok:=false;
      if not FileExists('MaskKey.dll') then
      begin
      showmessage('not this file');
      exit;
      end;
      Handle := LoadLibrary('MaskKey.dll');
      if Handle <> 0 then
      @StartKey:=GetProcAddress(Handle,'StartMaskKey');
      if @StartKey<>nil then
      b_ok:=StartKey(pKey,i_Length);
      if b_ok then
      showmessage('ok');
      //FreeLibrary(Handle);
      except
      end;
    end;//上面这个无效果
    //下面这个才行
    Function StartMaskKey(lpdwVirtualKey:LPDWORD;nLength:DWORD;bDisableKeyboard :Boolean = False) : DWORD;stdcall;external 'D:\MaskKey\DLL\Release\MaskKey.dll';
      Function StopMaskKey():DWORD;stdcall;external 'D:\MaskKey\DLL\Release\MaskKey.dll';
    var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    VAR
      KEY:ARRAY[0..8] of DWORD;
      len:DWORD;
      pKey:LPDWORD;
    begin
        key[0] := 65;
        key[1] := 66;
        key[2] := 67;
        key[3] := VK_Left;
        key[4] := VK_Right;
        key[5] := VK_Up;
        key[6] := VK_Down;
        key[7] := $5B;           
        key[8] := $5C;
        pKey:=@key;
        len:=9;
        StartMaskKey(pkey,len);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    StopMaskKey;
    end;
      

  3.   

    TCMPP_Connect = function(gw_ip: PChar;port:integer; username: PChar; passwd: PChar): Integer; stdcall;
    var
       myHandle:Thandle;
       mycmpp:TCMPP_Connect;
    begin
    try
       myHandle:=loadlibrary('D:\mydll.dll');
       @myCmpp:=GetProcAddress(myhandle,'CMPP_Connect');
       if myCmpp('10.28.0.1',30,'user1','abc')>0 then
         showmessage('ok')
       else
         showmessage('no');
    finally
       freelibrary(myhandle);    //我在运行时,这句报错??
    end;   
    end;
      

  4.   

    TCMPP_Connect = function(gw_ip: PChar;port:integer; username: PChar; passwd: PChar): Integer; stdcall;
    var
       myHandle:Thandle;
       mycmpp:TCMPP_Connect;
    begin
    try
       myHandle:=loadlibrary('D:\mydll.dll');
       @myCmpp:=GetProcAddress(myhandle,'CMPP_Connect');
       if myCmpp('10.28.0.1',30,'user1','abc')>0 then
         showmessage('ok')
       else
         showmessage('no');
    finally
       freelibrary(myhandle);   
    end;   
    end;
      

  5.   

    loadlibrary不成功的话你的freelibrary当然要出错啊
    再加except试试
      

  6.   

    输入检查功能的实现在Edit2的onKeyPress事件处理过程中,程序清单如下。 procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char); 
    var 
    order: Integer; 
    txt: PChar; 
    PFunc: TFarProc; 
    Moudle: THandle; 
    begin 
    Moudle := Loadlibrary('c:\dlls\example.dll'); 
    if Moudle > 32 then 
    begin 
    Edit2.text := ''; 
    Pfunc := GetProcAddress(Moudle,'Instr'); 
    txt := StrAlloc(80); 
    txt := StrPCopy(txt,Edit1.text); 
    Order := TInstr(PFunc)(txt,Key); 
    if Order = -1 then 
    Label1.Caption := '不包含这个字符 ' 
    else 
    Label1.Caption := '位于第'+IntToStr(Order+1)+'位'; 
    end; 
    Freelibrary(Moudle); 
    end;