How to use function SQLGetPrivateProfileString? Or an alternative?

35 views
Skip to first unread message

dobe...@gmx.net

unread,
Jul 13, 2016, 10:30:09 AM7/13/16
to
Dear All,
I found this thread http://www.delphigroups.info/2/ad/345347.html dating back to the year 2004. It shows how to retrieve a list of DSN entries without poking into the registry directly. So far no problem with Delphi. BTW, I'm using version 2007 on Windows 7. Now I'd like to select only the entries which refer to an MS Access database, so I tried to add only a few lines to the example.

First I declared this:
function SQLGetPrivateProfileString(
lpszSection, lpszEntry, lpszDefault, lpszRetBuffer: PChar;
cbRetBuffer: Integer;
lpszFilename: PChar): integer; stdcall;

and under implementation:
function SQLGetPrivateProfileString; external 'odbccp32.dll' name 'SQLGetPrivateProfileString';

Then I added the following line somewhere in the middle of the example:
Result := SQLGetPrivateProfileString('ODBC Data Sources', Buffer1, 'Driver', RetBuf, 100, 'odbc.ini');

with RetBuf also declared as PChar. I was able to retrieve the Driver strings as shown in the registry under ODBC. Unfortunately, the memory is not cleaned up and at the end of it all and execution stops because of this exception: Access violation ...

I tried to handle this exception but failed. What am I doing wrong? I tried running the code also as Administrator but saw no difference. Any help will be appreciated!

dobe...@gmx.net

unread,
Jul 13, 2016, 11:54:56 AM7/13/16
to
Dear All,
It appeared to be a simple problem: initialising the PChar variable RetBuf and later on freeing it, solved the problem of the access violations:
GetMem(RetBuf, 100);
...
FreeMem(RetBuf);

My next question is now: how to retrieve the path to the *. MDB file involved (or *.ACCDB file). I tried this, but to no avail:
SQLGetPrivateProfileString('', Buffer1, 'DBQ', RetBuf, 128, 'odbc.ini')

Any help will be appreciated ...
Reply all
Reply to author
Forward
0 new messages