Board index » delphi » Sharing a database connection in DLL

Sharing a database connection in DLL

Okay so why am I getting error -502 "Declared cursor already exists" when
attempting to share a database connection from within a DLL? This is meant
purely to prove a concept, but it seems I amy be missing something
fundamental here! Any ideas?

Extract of code:
==========
Application:
------------
procedure TForm1.FormCreate(Sender: TObject);
begin
  FLibHandle := LoadLibrary(PChar(LibraryName));
  if FLibHandle <> 0 then
  begin
    @InitConnection := GetProcAddress(FLibHandle, 'InitConnection');
    if @InitConnection <> nil then
    begin
      if not(InitConnection(Pointer(IBDatabase1))) then
        raise Exception.Create('Failed to initialise libarary.');
      @GetDepartmentName := GetProcAddress(FLibHandle, 'GetDepartmentName');
    end
    else
      raise Exception.Create('Failed to initialise library - ' + LibraryName
+ '.');
  end
  else
    raise Exception.Create('Failed to initialise library - ' + LibraryName +
'.');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text :=
GetDepartmentName(IBDataSet1.FieldByName('DEPT_NO').AsInteger);
end;

DLL:
-----
var
  SaveExit: Pointer;
  IBDatabase1: TIBDatabase;
  IBTransactionLib: TIBTransaction;

function InitConnection(IBConnection: Pointer): Boolean;
begin
  try
    IBDatabase1 := TIBDatabase(IBConnection);
    IBTransactionLib := TIBTransaction.Create(IBDatabase1.Owner);
    IBDatabase1.AddTransaction(IBTransactionLib);
    IBTransactionLib.DefaultDatabase := IBDatabase1;
    Result := True;
  except
    on E:Exception do
    begin
      OutputDebugString(PChar(E.Message));
      Result := False;
    end;
  end;
end;

function GetDepartmentName(Employee: Integer): ShortString;
var
  Query1: TIBQuery;
begin
  Query1 := TIBQuery.Create(IBDatabase1.Owner);
  try
    try
      with Query1 do
      begin
        Database := IBDatabase1;
        Transaction := IBTransactionLib;
        BufferChunks := 1000;
        CachedUpdates := False;
        SQL.Add('select department');
        SQL.Add('from department');
        SQL.Add('where dept_no = :dept_no');
        Params.ParamByName('dept_no').AsInteger := Employee;
        Open;
        Result := FieldByName('department').AsString;
        Close;
      end;
    except
      Result := 'Error!';
    end;
  finally
    Query1.Free;
  end;
end;

 

Re:Sharing a database connection in DLL


Victor,
You must compile your application and DLL using the run-time package to
resolve that problem. It will be better if you change your DLL to a package.

Sachli

Quote
Victor Rodrigues <jeunj...@yahoo.com.au> wrote in message

news:8nae0v$hqd9@bornews.borland.com...
Quote
> Okay so why am I getting error -502 "Declared cursor already exists" when
> attempting to share a database connection from within a DLL? This is meant
> purely to prove a concept, but it seems I amy be missing something
> fundamental here! Any ideas?

> Extract of code:
> ==========
> Application:
> ------------
> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   FLibHandle := LoadLibrary(PChar(LibraryName));
>   if FLibHandle <> 0 then
>   begin
>     @InitConnection := GetProcAddress(FLibHandle, 'InitConnection');
>     if @InitConnection <> nil then
>     begin
>       if not(InitConnection(Pointer(IBDatabase1))) then
>         raise Exception.Create('Failed to initialise libarary.');
>       @GetDepartmentName := GetProcAddress(FLibHandle,

'GetDepartmentName');

- Show quoted text -

Quote
>     end
>     else
>       raise Exception.Create('Failed to initialise library - ' +
LibraryName
> + '.');
>   end
>   else
>     raise Exception.Create('Failed to initialise library - ' + LibraryName
+
> '.');
> end;

> procedure TForm1.Button1Click(Sender: TObject);
> begin
>   Edit1.Text :=
> GetDepartmentName(IBDataSet1.FieldByName('DEPT_NO').AsInteger);
> end;

> DLL:
> -----
> var
>   SaveExit: Pointer;
>   IBDatabase1: TIBDatabase;
>   IBTransactionLib: TIBTransaction;

> function InitConnection(IBConnection: Pointer): Boolean;
> begin
>   try
>     IBDatabase1 := TIBDatabase(IBConnection);
>     IBTransactionLib := TIBTransaction.Create(IBDatabase1.Owner);
>     IBDatabase1.AddTransaction(IBTransactionLib);
>     IBTransactionLib.DefaultDatabase := IBDatabase1;
>     Result := True;
>   except
>     on E:Exception do
>     begin
>       OutputDebugString(PChar(E.Message));
>       Result := False;
>     end;
>   end;
> end;

> function GetDepartmentName(Employee: Integer): ShortString;
> var
>   Query1: TIBQuery;
> begin
>   Query1 := TIBQuery.Create(IBDatabase1.Owner);
>   try
>     try
>       with Query1 do
>       begin
>         Database := IBDatabase1;
>         Transaction := IBTransactionLib;
>         BufferChunks := 1000;
>         CachedUpdates := False;
>         SQL.Add('select department');
>         SQL.Add('from department');
>         SQL.Add('where dept_no = :dept_no');
>         Params.ParamByName('dept_no').AsInteger := Employee;
>         Open;
>         Result := FieldByName('department').AsString;
>         Close;
>       end;
>     except
>       Result := 'Error!';
>     end;
>   finally
>     Query1.Free;
>   end;
> end;

Other Threads