using pgDAC in a DLL

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
russell
Posts: 4
Joined: Thu 14 Mar 2013 08:31

using pgDAC in a DLL

Post by russell » Thu 14 Mar 2013 08:48

I am trying to use pgConnection in a datamodule in a DLL.
In the D2010 IDE I can connect to the target DB.
When I make the datamodule when the DLL is loaded by an EXE the connection fails.
I am guessing I am not doing enough in the create() calls.
Is there an example of using pgDAC in a DLL so I can learn from the code?

Thanks,
Russell

russell
Posts: 4
Joined: Thu 14 Mar 2013 08:31

Re: using pgDAC in a DLL

Post by russell » Thu 14 Mar 2013 22:04

In my D2010 DLL I am trying to connect to a pg DB using pgConnection. I tried a datamodule and failed. So I used a form in the DLL and added to the form a pgConnection. pgQuery, DataSource, DBGrid. They are configured together correctly.

In the D2010 IDE I check pqQuery.active and the DBGrid fills with data from the remote DB, which requires SSL. The pgConnection shows connected=true.
This shows the connection was successful.

In the DLL my formCreate procedure is tedious to assist with diagnosis.
procedure TzwForm.FormCreate(Sender: TObject);
begin
inherited;
ZWConfigFile := TIniFile.Create(txtZWConfigFileName);
ZWConfigFile.WriteString('log','started',dateTimeToStr(NOW));

PgConnection1 := TPgConnection.Create(self);
with PgConnection1 do
begin
if assigned(SSLOptions)
then ZWConfigFIle.WriteString('log','pgConnection.SSLOptions','assigned')
else ZWConfigFIle.WriteString('log','pgConnection.SSLOptions','unassigned');
end;
PgConnection1.SSLOptions.mode := pgClasses.smAllow;
if (pgConnection1.sslOptions.mode = pgClasses.smAllow)
then ZWConfigFIle.WriteString('log','pgConnection.SSLOptions','smAllow')
else ZWConfigFIle.WriteString('log','pgConnection.SSLOptions','error');
ZWConfigFile.updateFile;
PgQuery1 := TPgQuery.create(self);
end;

In the config file the Log section shows SSLOptions is assigned and set to smAllow.
But the connection fails.
The remote server log shows:

2013-03-14 22:17:04 NZDT FATAL: no pg_hba.conf entry for host "NNN.NNN.NNN.NNN", user "XXX", database "postgres", SSL off

I am requestion assistance:
Am I not doing something required?
Am I making a mistake somewhere?
Are there any example of using pgConnection in a DLL with SSL on that I could learn from?

Thanks,
Russell

DemetrionQ
Devart Team
Posts: 271
Joined: Wed 23 Jan 2013 11:21

Re: using pgDAC in a DLL

Post by DemetrionQ » Mon 18 Mar 2013 11:38

Hello.

SSLOptions.Mode = smAllow means that the application will first of all try to connect to the server via a non-SSL connection. And if only this was unsuccessful, it will try to connect via an SSL connection. As your server is set only to SSL connection, in the smAllow mode, the first attempt to connect to the server (via a non-SSL connection) will be unsuccessful - the server will return an error message similar to the one you have provided. PgDAC hides this message and switches to an SSL connection. In the described situation, you will see the following message:
no pg_hba.conf entry for host "NNN.NNN.NNN.NNN", user "XXX", database "postgres", SSL off
only if you run your project from the IDE. After pressing the "Continue" button in the Debugger Exception Notification window, your application will continue running and start connection to the server via SSL.
If you run your application directly, you won't see the message.
The more detailed information on the logic of connections work in different SSL modes can be read in the "Secure Connections" article of the PgDAC documentation.

russell
Posts: 4
Joined: Thu 14 Mar 2013 08:31

Re: using pgDAC in a DLL

Post by russell » Tue 19 Mar 2013 03:44

Thank you Dmitry for your reply, from which I understand I should use SSLOptions.mode = smRequire.

I did this and retested. I can connect with the component in an exe.
When building my DLL I have the component on a form (D2010) and using the object inspector I check "[ ] Connected" and I connect. On the form I have a pgQuery with sql. I also have a DBgrid and a datasource. When pgQuery.Active is checked the DBGrid fills with data. This is all in the IDE in the DLL project. I do not mean running the DLL from the IDE. I mean setting pgQuery.active=true. I achieve a connection to a remote DB.

When I load my DLL using an independent EXE I create the pgConnection and pgQuery using
mypgConnection = TpgConnection.create(self).
I also fill properties in mypgConnection. Port, Server, username, ... (everything I see in the object inspector).

In the DLL code, when I try mypgConnection.connect, no connection is achieved. The remote server reports the fatal error message I reported above.

Why am I having this problem? My guess is this. Using a DLL I need to manually create the pgConnection and pgQuery objects. I may be doing too little and some of the pgConnection component is absent or not performing correctly; bcause the server is reporting "SSL off".

Dmitry, you give no indication that you have used pgConnection in a DLL so this may be why you are not seeing the problem as I see it.

Perhaps I am missing something?

DemetrionQ
Devart Team
Posts: 271
Joined: Wed 23 Jan 2013 11:21

Re: using pgDAC in a DLL

Post by DemetrionQ » Tue 19 Mar 2013 15:08

Hello.

Creation and connecting of TPgConnection executed in DLL is similar to creation and connecting of TPgConnection in a normal VCL Form Appliction. I have sent to your e-mail a simple sample of using TPgConnection and TPgQuery via DLL. In addition, PgDAC_DEMOS\Miscellaneous\Dll contains a sample of work with PgDAC via DLL.
If our sample cannot reproduce the problem, please modify our sample, so that the problem repeats, and send it to us.

russell
Posts: 4
Joined: Thu 14 Mar 2013 08:31

Re: using pgDAC in a DLL

Post by russell » Tue 19 Mar 2013 21:40

Thank you Dmitry. I modified your example using my connection parameters and it connectets correctly.

Post Reply