Dynamically Set Database Parameters For RAD Server - delphi

I have developed an application that uses RAD Server over IIS. So far I have successfully created my Server and client applications. The app provides some information that needs to be verified against a MSSQL database on the Server Side. Everything works fine in a test environment as my Database Connection parameters are set in my FDConnection component.
However, I would like to change the Connection parameters by reading an ini file when the Server is accessed.
On my development system I can place the ini file in the directory where my bpl output is located. (ie. C:\Users\Username\Projects\Application\Server\Win32\Debug). The server then reads the ini file correctly and updates the component parameters.
I have created the directories on the server in accordance with the RAD Server documentation and have placed the required EMS files in the directory. (ie: C:\inetpub\RADServer\EMSServer) Since this is where the emsserver.ini file resides, I thought this would be the correct place to put my ini file. If I launch the EMSDevServer.exe from this directory, the ini file gets read properly and FDConnection parameters get updated.
However, when I launch the RAD Server through IIS using the ISAPI dlls, it appears the ini file is not found as my database connections fail.
I have tried putting the ini file in the C:\Users\Public\Documents\Embarcadero\EMS directory and that did not work either.
Following is my code to access the ini file which is called on DataModuleCreate.
procedure TdmSecurity.DataModuleCreate(Sender: TObject);
begin
SetConnectionStr(FDConnectionSTIKS);
end;
procedure SetConnectionStr(var FDConnectionSTIKS: TFDConnection);
var ConfigIni: TInifile;
DBServerName, DBName, Path: string;
begin
Path := GetCurrentDir;
ConfigIni := TIniFile.Create(System.IOUtils.TPath.Combine(Path, 'Config.ini'));
// ConfigIni := TIniFile.Create(System.IOUtils.TPath.Combine(System.IOUtils.TPath.GetDocumentsPath, 'Config.ini'));
// ConfigIni := TIniFile.Create('C:\Users\leonard\Projects\LumberNowEMS\Server\Win32\Debug\config.ini');
DBServerName := ConfigIni.ReadString(AppNode, 'ServerName', 'ZEUS');
DBName := ConfigIni.ReadString(AppNode, 'DataBase', 'NOTHING');
// showmessage(configini.FileName);
with FDConnectionSTIKS.Params as TFDPhysMSSQLConnectionDefParams do
begin
DriverID := 'MSSQL';
Server := DBServerName;
Database := DBName;
UserName := DBUserID;
Password := DBPassword;
end;
ConfigIni.Free;
// showmessage(Path + '; DBName:' +DBName);
// Result := Format('DriverID=MSSQL;Server=%s;Database=%s;User_name=%s;Password=%s', [DBServerName, DBName, DBUserID, DBPassword]);
end;
I expected that the IIS would read the ini file from the same place but it does not appear to be so. Can someone tell me where I should put the ini file so IIS can access it correctly or perhaps a better way of setting the database connection in Rad Server? Perhaps I could put my parameters in the emsserver.ini if there is a variable I can access.

#nolaspeaker - I would give you credit for the answer but you only posted a comment. Your response prompted me to find a way to determine the path that was being used. So I wrote the value of the path variable to a known text file.
Path := GetCurrentDir;
AssignFile(F, 'C:\Temp\Data.txt');
Rewrite(F);
WriteLn(F, Path);
CloseFile(F);
The path that is being used is 'C:\Windows\SysWOW64\inetsrv';
I moved my Config.ini file to this directory and it was found and read correctly.
Thanks for the responses.

Quite late, still posting an answer.
Functions in System.SysUtils will be helpful here.
following function call would give you the path to the directory where the .bpl file resides.
ExtractFilePath(GetModuleName(HInstance));
place the ini along with .bpl file, combining the above function's result with the Ini filename will help you to target an ini.

Related

How to share an application and associated datafile

I have written a program (firemonkey) using delphi community edition. I would like to share the program, but the .exe file that my friends will be downloading has to access a text file from time to time to retrieve strings. When writing the program I used an event handler, to load the text file at startup:
tform3.formCreate (Sender:Tobject);
...
assignfile(myfile,('C:**********.txt));
...
Worked just fine during the
design stage.
As a hobbyist, I now find myself stuck. If I use INNO setup compiler to create an installation program, which I plan to do, I can't have this same hardwired reference ('C:*****) to the data file's location. What I need is to change the above code such that the .exe file can locate the supporting datafile irrespective of where that .exe file (and datafile) ends up on someone else's PC.
How can I do this? i.e. What code do I need (in place of the above) to ensure that the installation program I hand out will install an .exe file that can locate the data file it references?
Any help, much appreciated. Still learning.
Read-only access
If the data file should always be opened in read-only mode, the simplest solution is to place it next to the *.exe file. Then, at runtime, you dynamically find the path to the *.exe file and modify it to find the path to the data file. For instance,
uses
IOUtils;
procedure TForm1.FormCreate(Sender: TObject);
var
FileName: string;
begin
FileName := TPath.Combine(ExtractFilePath(ParamStr(0)), 'data.txt');
ShowMessage(TFile.ReadAllText(FileName, TEncoding.UTF8));
end;
ParamStr(0) contains the path to the *.exe file, such as
'C:\Users\Andreas Rejbrand\Documents\Embarcadero\Studio\Projects\test\Win32\Debug\Project1.exe'
Then ExtractFilePath(ParamStr(0)) is
'C:\Users\Andreas Rejbrand\Documents\Embarcadero\Studio\Projects\test\Win32\Debug\'
and, finally, TPath.Combine(ExtractFilePath(ParamStr(0)), 'data.txt') is
'C:\Users\Andreas Rejbrand\Documents\Embarcadero\Studio\Projects\test\Win32\Debug\data.txt'
Make sure the installer puts the data file next to the *.exe file.
Read and write access
If we are talking about a settings file or some other file that each user needs to change (via the software), you cannot place it next to the *.exe file, because the *.exe file typically resides in the Program Files folder, which is read only. Also, there is only one Program Files folder, but possibly many users on the PC, and each user should have his or her own copy.
The solution is to save the file in the user's own folders, specifically, the AppData folder:
FileName := TPath.GetHomePath + '\Mariner\My Word Processor App\Settings\settings.ini';
(using a different approach to path building).
On my system, this becomes
'C:\Users\Andreas Rejbrand\AppData\Roaming\Mariner\My Word Processor App\Settings\settings.ini'
Your installer (Inno Setup) has built-in support for placing files in this location.
If it is only accessed read only, you could also consider adding it as a resource to the executable. Which would then allow you to simply distribute this executable without the need for an installer.
Delphi Dabbler has an example, but I found it a bit confusing. I'll link to it (PDF) anyway.
You can let the user select the place where the file has to be saved. Proposing AppData folder if the file is for each individual user or CommonAppData if the file has to be shared between different users.
When the use selected the data file destination, you can save it to an INI file. The INI file can be stored, without asking the user, either to the registry or to an INI file saved in the AppData folder or ProgramData folder.
Here is a snipped of source code to get hand on those special folders:
const
SectionWindow = 'Window';
SectionData = 'Data';
CompanyFolder = 'YourCompanyName';
constructor TForm1.Create(AOwner: TComponent);
var
CommonPath : array [0..MAX_PATH] of Char;
LocalPath : array [0..MAX_PATH] of Char;
LangFileName : String;
begin
SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, #CommonPath[0]);
SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, SHGFP_TYPE_CURRENT, #LocalPath[0]);
FIniSection := SectionWindow;
FIniSectionData := SectionData;
FAppName := ChangeFileExt(ExtractFileName(Application.ExeName), '');
FCommonAppData := IncludeTrailingPathDelimiter(CommonPath) +
CompanyFolder + '\' + FAppName + '\';
FLocalAppData := IncludeTrailingPathDelimiter(LocalPath) +
CompanyFolder + '\' + FAppName + '\';
FIniFileName := FLocalAppData + FAppName + '.ini';
ForceDirectories(FCommonAppData);
ForceDirectories(FLocalAppData);
inherited Create(AOwner);
end;

How to solve [FireDAC][Phys][SQLite] ERROR: unable to open database file, When app is installed in programFiles?

I developed a tool using Firedac with the database as SQLite.
after finishing the project and making an installer (InnoSetup) I get an error
[FireDAC][Phys][SQLite] ERROR: unable to open database file
when I launch the app (double click).
This is the connection parameters I use
constructor TDbInteract.Create(const aDatabasePath: string; const aOnNeedCredentials: TOnNeedCredentials);
var
aParams: array of string;
begin
if not TFile.Exists(aDatabasePath) then
raise Exception.Create('Database file not found');
aParams := ['DriverID=SQLite',
'Database=' + aDatabasePath,
'OpenMode=CreateUTF16',
'LockingMode=Normal',
'JournalMode=WAL',
'StringFormat=Unicode',
'Synchronous=Full',
'UpdateOptions.LockWait=True',
'BusyTimeout=30000',
'SQLiteAdvanced=temp_store=MEMORY;page_size=4096;auto_vacuum=FULL'];
InitiateResource(aParams, aOnNeedCredentials);
end;
procedure TDbInteract.InitiateResource(const aParams: array of string; const aOnNeedCredentials: TOnNeedCredentials);
var
I: Integer;
Credentials: TStringDynArray;
begin
FRowsAffected := 0;
FIsForeignKeyHonored := True;
FOwnsResultDataSets := True;
FDataSetContainer := TDataSetContainer.Create(nil);
FConnection := TFDConnection.Create(nil);
try
for I := Low(aParams) to High(aParams) do
begin
FConnection.Params.Add(aParams[I]);
end;
if Assigned(aOnNeedCredentials) then
begin
aOnNeedCredentials(Self, Credentials);
for I := Low(Credentials) to High(Credentials) do
begin
FConnection.Params.Add(Credentials[I]);
end;
end;
FConnection.Open;
except
raise;
end;
end;
**Identified problems:
I read somewhere (do not remember the page I was in) that SQLite engine requires full lock on the directory that it wants to write to. and this is the problem. How ever I run the tool as invoker and my account is an admin so that is not a problem. Also I have the same tool written in c# and this problem never occurs.
Solutions I found:
Run the tool as administrator
Do not Install the tool in ProgramFiles directory
I really don't like these solutions. and would like to run my tool from program Files directory as it is part of a bigger project.
Note: The database file is in programdata directory. It is created by the tool (this works).
Edit: I just tried putting the DB file in C:\Users\Nacereddine\AppData\Roaming\MyTool And I still have the same problem when the tool is installed in C:\Program Files (x86)\MyTool
This how I create the DB file
class procedure TDbInteract.CreateSQLiteDb(const aDatabasePath: string; const aTables: TStringDynArray);
var
I: Integer;
aParams: array of string;
aConnection: TFDConnection;
begin
aParams := ['DriverID=SQLite',
'Database=' + aDatabasePath,
'OpenMode=CreateUTF16',
'LockingMode=Normal',
'JournalMode=WAL',
'StringFormat=Unicode',
'Synchronous=Full',
'UpdateOptions.LockWait=True',
'BusyTimeout=30000',
'SQLiteAdvanced=temp_store=MEMORY;page_size=4096;auto_vacuum=FULL'];
aConnection := TFDConnection.Create(nil);
try
for I := Low(aParams) to High(aParams) do
begin
aConnection.Params.Add(aParams[I]);
end;
aConnection.Open();
for I := Low(aTables) to High(aTables) do
begin
aConnection.ExecSQL(aTables[I]);
end;
finally
aConnection.Close;
aConnection.Free;
end;
end;
Note: I do not know if this makes any difference but the Db file is encrypted.
Sorry for the trouble folks.
The problem was that we had a Localization db file installed with the tool in ProgramFiles.
What made me exclude that from my investigation is that, when opening this file I set the OpenMode to ReadOnly
FConnection.Params.Add('OpenMode=ReadOnly');
but as I said before in my question SQLite engine requires full access to the folder containing the db file so it preforms a lock on it (still did not find the page I read this on).
I checked this by playing around with open modes and debugging the tool each time.
once I changed the permissions of both the file and the directory the error was gone.
at the end I decided to move the localization file to the programData directory with the main db file and all is well.
I realized (Thank you for this #Ken and #David) that the programData directory also requires admin permissions to write to, and therefore I will move the db files to a more appropriate dir (i.e Users).
What is useful from this problem is that even if you connect to the Sqlite db file with OpenMode=ReadOnly, you still need write access for the path to that file.

Why does a simple Delphi FTP upload of a zip file produce a corrupted file? [duplicate]

I'm having a problem downloading a file using the TidFTP component in Delphi XE2. I am able to get a connection to the FTP site, get a list of files and perform the get command. However, when I try to download a file using the get command the file always downloads larger than the source file. Then the subsequent file is corrupt.
Additionally, if I try to download multiple files, the first file downloads (larger than the source) and the remaining files are skipped. No error is thrown from the get command, it just quits. I tried to hook into some of the events on the TidFTP control like AfterGet and OnStatus but everything appears normal.
I tried using a 3rd party FTP client to access the file and download it just to make sure it was not a problem with our FTP server and that download worked as expected. So I'm thinking it might be related to the TidFTP control or perhaps I'm doing something incorrectly.
Here is the routine I'm using to download the file:
var
ftp: TIdFTP;
strDirectory: string;
begin
ftp := TIdFTP.Create(nil);
try
ftp.Host := 'ftp.myftpserver.com'
ftp.Passive := false;
ftp.Username := 'TestUser';
ftp.Password := 'TestPassword';
ftp.ConnectTimeout := 1000;
ftp.Connect();
ftp.BeginWork(wmRead);
ftp.ChangeDir('/TestArea/');
strDirectory := 'c:\test\';
if not DirectoryExists(strDirectory) then
CreateDir(strDirectory);
ftp.Get('Test.zip', strDirectory + '\' + 'Test.zip', true, false);
ftp.Disconnect();
except
on e: exception do
showMessage(e.message);
end;
end;
You need to set the TIdFTP.TransferType. Its default value is Id_TIdFTP_TransferType, which is ftASCII. You need to use ftBinary instead, and set it before executing the Get:
ftp.Connect();
...
ftp.TransferType := ftBinary;
ftp.Get('Test.zip', strDirectory + '\' + 'Test.zip', true, false);
ftp.Disconnect();
The documentation for TIdFTP.TransferType says in one place that it's automatically set to ftBinary when Login is executed or when Connect is called when AutoLogin is set to true, but you're not executing Login in your code and haven't set AutoLogin. The paragraph immediately following the above statement says:
According to #RemyLebeau in the comments below, the documentation quoted is in error, and TransferType was never set to ftBinary in Login. Leaving the stricken content for future reference.
According to the documentation:
The default value for TransferType is Id_TIdFTP_TransferType, as assigned during initialization of the component.

Application cant access file on startup

I'm doing an application(XE6 , Firemonkey) to synchronize files between a shared folder and a computer/s. This application checks every x hours if there are new files to be synchronized, and it starts on windows start-up.
I can do everything, my application starts on start-up, and it does the synchronization, as long as i'm the one starting it. Whem the application auto starts on start up it gives me an exception "EINOUTERROR" - File Access Denied.
On starting the application reads a small .txt file to set up it self (shared folder location, rate of synchronization etc), my guess is that since its the windows starting the app runs it without privileges to read the .txt, but even after changing the .txt permissions to full control on everyone it gives the same error.
File open code:
AssignFile(myFile,'Dados.txt');
if FileExists('Dados.txt') then
Append(myFile)
else
Rewrite(myFile);
FileMode := fmOpenRead;
Reset(myFile);
Code of placing the app on startup programs :
procedure TSyncM.RunOnStartup(const sCmdLine: string; bRunOnce: boolean; Remove: Boolean) ;
var sKey: string;
Section: string;
const ApplicationTitle = 'GEN4Sync';
begin
if (bRunOnce) then
sKey := 'Once'
else
sKey := '';
Section := 'Software\Microsoft\Windows\CurrentVersion\Run' + sKey + #0;
with TRegIniFile.Create('') do
try
RootKey := HKEY_CURRENT_USER;
if Remove then
DeleteKey(Section, ApplicationTitle)
else
WriteString(Section, ApplicationTitle, sCmdLine) ;
finally
Free;
end;
end;
If i comment the piece of code that calls the reading of that .txt my app starts and executes well, but i don't want to set it up everytime.
Thanks in advance
I think that the issue is related to your use of relative paths. You have written the code under the assumption that the working directory is the same directory as contains the executable. That is not necessarily so.
When you start the application by double clicking on the executable file, for instance, the shell ensures that the initial working directory is the directory containing the executable file. However, when Windows starts your program at startup I suspect that the working directory is the system directory. And of course your file is not found there, and you don't have rights to write there.
Instead of using relative paths, use the full path to the file.
FileName := ExtractFilePath(ParamStr(0)) + 'Dados.txt';
Or perhaps
FileName := TPath.Combine(ExtractFilePath(ParamStr(0)), 'Dados.txt');
Note that this does also assume that your executable file is located in a folder which you can write to. That is often not the case so you may need to find a different location.
I do have to comment that I find it somewhat incongruous that you are mixing the very modern (FireMonkey) with the ancient (Pascal I/O). Perhaps it is time to move to a more modern I/O technique.

Firebird 2.5 embedded - 'Database name is missing' - client dll chaos

I cannot get firebird embedded server to work.
Get an exception at line
datamodule1.IBQuery1.Prepare;
".exe raised exeption class EIBClientError with message 'Database name is missing'."
(IBQuery1 is tied to IBDatabase1)
Im using the server/client dll from this package:
Firebird-2.5.3.26778-0_Win32_embed.zip (x86)
I copied the following files (as written in the firebird manual) to my application's folder:
ib_util.dll
icudt30.dll
icuin30.dll
icuuc30.dll
fbembed.dll
firebird.msg
('firebird.conf' does not have to be copied in case you are ok with firebirds default configuration.)
I renamed the fbembed.dll to gds32.dll because im using Delphi's interbase components, but tried the file names fbclient.dll and fbembed.dll also.
For IBDatabase1's 'DatabaseName' property i dont use hostname bacause its stated in the manual that you dont have to in case of using the embedded server (local XNET protocol).
I create the path in runtime, been debug this and double checked the path, its correct.
At design time i can connect to the database setting the IBDatabase1's 'Connected' parameter to 'True'. (After manually filling the 'DatabaseName' property with the correct path.)
I searched the system for other firebird client dll's (gds32.dll) and found it at four different location:
c:\Program Files (x86)\HK-Software\IBExpertLive\gds32.dll
c:\Windows\System32\gds32.dll
c:\Windows\SysWOW64\gds32.dll
c:\InCash\GDS32.DLL
It seems that the system using the one that is located in SysWOW64, but even if i replace the dll there to the embedded (fbembed.dll renamed to gds32.dll), nothing changes.
The goal would be not to touch any of the already installed dlls and environment variables/registry entries, but using the embedded dll that is bundled with my application and is located beside that, so making the deployment of the software as easy as you can get.
Some more info:
Tried to rename database XYZ.FDB to XYZ.GDB
Username and password is given at IBDatabase1's 'Params' property although its not necessary with the embedded version
Op. system: Win7 x64
I have a firebird service installed, but its been stoped
Im using Delphi's InterBase components
Datamodule1.IBQuery1 is tied to Datamodule1.IBDatabase1
The DatabaseName property of IBDatabase1 been set to 'e:\X\XYZ.FDB' by code (the path is correct, i double checked)
IDE: Delphi 2010
Was searching for similar topics but yet i could not find solution.
Connect to database:
procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
IBDatabase1 := TIBDatabase.Create(self);
//
app_path := ExtractFilePath(Application.ExeName);
//
IBDatabase1.DatabaseName := app_path + 'db\XYZ.GDB';
IBDatabase1.LoginPrompt := false;
IBDatabase1.Params.add('lc_ctype=UTF8');
IBDatabase1.Params.add('user_name=xyz'); //not necessary with embedded
IBDatabase1.Params.add('password=xyz'); //not necessary with embedded
//
IBDatabase1.Connected := true;
IBDatabase1.Open;
end;
Then trying to insert a record:
with datamodule1.IBQuery1 do
begin
close;
With SQL do
begin
clear;
Add( 'INSERT INTO MST_EVENTS (index, state, event, param, date, time, devID, gateway)' );
Add( 'VALUES :index, :state, :event, :param, :date, :time, :devid, :gateway');
end;
//
Params[0].AsInteger := FMaster.EventRecordIndex;
Params[1].AsSmallInt := FMaster.EventRecordState;
Params[2].AsString := eventToStr(FMaster.EventRecordEvent);
Params[3].AsSmallInt := 0;
Params[4].AsDate := FMaster.EventRecordDate;
Params[5].AsTime := FMaster.EventRecordTime;
Params[6].AsLongWord := FMaster.EventRecordDevID;
Params[7].AsString := FMaster.EventRecordIP;
//
if ( prepared = false ) then
prepare; //Throws the exception here
open;
end;
This line was the problem.
IBDatabase1 := TIBDatabase.Create(self);
It's totally needless, because the class instance had already been created by the datamodule 'form'.

Resources