Title:
Author: Jonas Bilinkevicius
Product: Delphi 2.x (or higher)
Post Date: 10/28/2002
Problem/Question/Abstract:
How can my program access the idapi.cfg file and probably change its INIT (Local
Share etc.) section?
Answer:
For 32bit only. You can of course use the registry to determine the default CFG
File instead of passing it as a parameter here:
1
2 procedure ModifyCFG(const ACFGFile, AValue, AEntry, ACFGPath: string; SaveAsWin31:
3 bool);
4 var
5 hCfg: hDBICfg;
6 pRecBuf, pTmpRec: pByte;
7 pFields: pFLDDesc;
8 Count: word;
9 i: integer;
10 Save: boolean;
11 Reg: TRegistry;
12 const
13 RegSaveWIN31: array[bool] of string = ('WIN32', 'WIN31');
14 begin
15 hCfg := nil;
16 pFields := nil;
17 pRecBuf := nil;
18 Save := False;
19 Check(DbiOpenConfigFile(PChar(ACFGFile), False, hCfg));
20 try
21 Check(DbiCfgPosition(hCfg, PChar(ACfgPath))); {neccessary...?}
22 Check(DbiCfgGetRecord(hCfg, PChar(ACfgPath), Count, nil, nil));
23 pRecBuf := AllocMem(succ(Count) * 128); {128 additional safety...}
24 pFields := AllocMem(Count * sizeof(FLDDesc));
25 Check(DbiCfgGetRecord(hCfg, PChar(ACfgPath), Count, pFields, pRecBuf));
26 for i := 1 to Count do
27 begin
28 if StrPas(pFields^.szName) = AEntry then
29 begin
30 pTmpRec := pRecBuf;
31 Inc(pTmpRec, 128 * (i - 1));
32 StrPCopy(PChar(pTmpRec), AValue);
33 end;
34 inc(pFields);
35 end;
36 dec(pFields, Count);
37 Check(DbiCfgModifyRecord(hCfg, PChar(ACfgPath), Count, pFields, pRecBuf));
38 Save := True;
39 finally
40 if hCfg <> nil then
41 Check(DbiCloseConfigFile(hCfg, Save, True, SaveAsWin31));
42 if pRecBuf <> nil then
43 FreeMem(pRecBuf, succ(Count) * 128);
44 if pFields <> nil then
45 FreeMem(pFields, Count * sizeof(FLDDesc));
46 end;
47 {update registry SAVECONFIG value}
48 Reg := TRegistry.Create;
49 try
50 Reg.RootKey := HKEY_LOCAL_MACHINE;
51 if not Reg.OpenKey('SOFTWARE\Borland\Database Engine', False) then
52 ShowMessage('Configuration Path not found')
53 else
54 begin
55 Reg.LazyWrite := False;
56 Reg.WriteString('SAVECONFIG', RegSaveWIN31[SaveAsWin31]);
57 Reg.CloseKey;
58 end;
59 finally
60 Reg.Free;
61 end;
62 {DbiExit/Init to re-read cfg... make absolutely sure there are no active
63 DB components when doing this (it's is best done by a loader app)}
64 Session.Close;
65 Session.Open;
66 end;
|