Modify the idapi.cfg settings through code (2) (Views: 700)
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: procedure ModifyCFG(const ACFGFile, AValue, AEntry, ACFGPath: string; SaveAsWin31: bool); var hCfg: hDBICfg; pRecBuf, pTmpRec: pByte; pFields: pFLDDesc; Count: word; i: integer; Save: boolean; Reg: TRegistry; const RegSaveWIN31: array[bool] of string = ('WIN32', 'WIN31'); begin hCfg := nil; pFields := nil; pRecBuf := nil; Save := False; Check(DbiOpenConfigFile(PChar(ACFGFile), False, hCfg)); try Check(DbiCfgPosition(hCfg, PChar(ACfgPath))); {neccessary...?} Check(DbiCfgGetRecord(hCfg, PChar(ACfgPath), Count, nil, nil)); pRecBuf := AllocMem(succ(Count) * 128); {128 additional safety...} pFields := AllocMem(Count * sizeof(FLDDesc)); Check(DbiCfgGetRecord(hCfg, PChar(ACfgPath), Count, pFields, pRecBuf)); for i := 1 to Count do begin if StrPas(pFields^.szName) = AEntry then begin pTmpRec := pRecBuf; Inc(pTmpRec, 128 * (i - 1)); StrPCopy(PChar(pTmpRec), AValue); end; inc(pFields); end; dec(pFields, Count); Check(DbiCfgModifyRecord(hCfg, PChar(ACfgPath), Count, pFields, pRecBuf)); Save := True; finally if hCfg <> nil then Check(DbiCloseConfigFile(hCfg, Save, True, SaveAsWin31)); if pRecBuf <> nil then FreeMem(pRecBuf, succ(Count) * 128); if pFields <> nil then FreeMem(pFields, Count * sizeof(FLDDesc)); end; {update registry SAVECONFIG value} Reg := TRegistry.Create; try Reg.RootKey := HKEY_LOCAL_MACHINE; if not Reg.OpenKey('SOFTWARE\Borland\Database Engine', False) then ShowMessage('Configuration Path not found') else begin Reg.LazyWrite := False; Reg.WriteString('SAVECONFIG', RegSaveWIN31[SaveAsWin31]); Reg.CloseKey; end; finally Reg.Free; end; {DbiExit/Init to re-read cfg... make absolutely sure there are no active DB components when doing this (it's is best done by a loader app)} Session.Close; Session.Open; end; ACFGPath would be '\SYSTEM\INIT\', AEntry would be 'LOCAL SHARE' und AValue would be 'TRUE' or 'FALSE'. |