Q&A

  • DLL문제
제가 이번에 첨으로 DLL을 만들어 보는거라..
문제해결이 잘 안되네요.
제가 작성한게 맞는지 확신도 없구요..
아래와 같이 만들었는데..
DB 연결은 정상적으로 되는데..
FreeLibrary(H); --> 이부분에서 오류가 발생하네요..
해결방법을 모르겠어요..
간단한 문제인듯 싶기도 한데 저한테는 어렵기만 하네요.
어떻게 하면 해결이 될까요? ㅠㅠ
-- DLL 호출
procedure TForm1.BitBtn2Click(Sender: TObject);
Type
   tConnectionInfo = record
     tProvide    : WideString;
     tServer     : WideString;
     tUSerID     : WideString;
     tPass       : WideString;
     tDefaultDB  : WideString;
   end;
var
   H : THandle;
   MyFunc : function(ConnectionInfo  : tConnectionInfo):String;
   aConnectionInfo :TConnectionInfo;
begin
   Try
      H := LoadLibrary('TestLib.DLL');
      if H >= 32 then
      begin
         @MyFunc := GetProcaddress(H,'ADO_Connection1');
         if @myFunc <> nil then
         begin
            aConnectionInfo.tProvide   := 'SQLOLEDB.1';
            aConnectionInfo.tServer    := edtServer.text;
            aConnectionInfo.tUSerID    := 'sa';
            aConnectionInfo.tPass      := edtPass.text;
            aConnectionInfo.tDefaultDB := edtDefaultDB.text
            Showmessage(MyFunc(aConnectionInfo));
         end;
      end;
   Finally
      FreeLibrary(H);
      H := 0;
   end;
end;

---- DLL
library TestLib;
uses
  SysUtils,
  Classes,
  Windows,
  DBConnection in 'DBConnection.pas';

Type
  tConnectionInfo = record
     tProvide    : WideString;
     tServer     : WideString;
     tUSerID     : WideString;
     tPass       : WideString;
     tDefaultDB  : WideString;
  end;

{$R *.res}

function ADO_Connection1(ConnectionInfo  : tConnectionInfo):String; export;
var
   tmpDB : TADO_Connection;
begin
   try
      tmpDB  := TADO_Connection.Create;
      tmpDB.tProvide   := ConnectionInfo.tProvide;
      tmpDB.tServer    := ConnectionInfo.tServer;
      tmpDB.tUSerID    := ConnectionInfo.tUSerID;
      tmpDB.tPass      := ConnectionInfo.tPass;
      tmpDB.tDefaultDB := ConnectionInfo.tDefaultDB;
      result := TmpDB.Server_Connection(True);
    except
      result := '서버연결에 실패했습니다.';
   end;
end;

exports
   ADO_Connection1;

end.

------
unit DBConnection;

interface

uses ADODB;

type
  TADO_Connection = Class
   AConnection : TADOConnection;
   AQuery : TADOQuery;

   tProvide    : WideString;
   tServer     : WideString;
   tUSerID     : WideString;
   tPass       : WideString;
   tDefaultDB  : WideString;

  private

  public
     function Server_Connection(tConnection : Boolean) :String;

  end;


function ADO_Connection : TADO_Connection; StdCall;

implementation

function TADO_Connection.Server_Connection(tConnection : Boolean) :String;
begin
   Try
      AConnection := TADOConnection.Create(AConnection);
      AConnection.Connected := False;
      AConnection.ConnectionString :='Provider='+tProvide+
                                     ';Password='+ tPass  +
                                     ';Persist Security Info=True;User ID=' + tUSerID     +
                                     ';Initial Catalog=' + tDefaultDB                     +
                                     ';Data Source=' + tServer                            +
                                     ';Auto Translate=False';
      AConnection.Connected := True;
      AQuery := TADOQuery.Create(AQuery);
      AQuery.Connection := AConnection;
      With AQuery do
      begin
         SQL.Clear;
         SQL.Add('select top 1 * from pubs..employee ');
         Open;
         Result := FindField('emp_id').AsString;
      end;
   except
      result := '';
   end;
end;

function ADO_Connection; external 'TestLib.DLL' name 'ADO_Connection'
2  COMMENTS