Just as Glenn9999 posted a collection of his tips and tricks, I'm posting some of my own...
Check if a specified file is a certain extension
Check if a specified service is installed (by service name, not image path)
Get the full path to a specified service (by service name)
Check if a specified service is enabled (auto start)
Get the version string of a specified EXE
Store database connection information with ability to get connection string
Object Structure:
Object Implementation
Final Implementation:
Execute a SQL script, hide console window, and read result file
This is helpful when installing updates for software that uses a database
Some code is specific to my needs
This could use some cleaning and simplifying
Declare type which holds an array of colors which you can easily extract RGB values
This is helpful when an array of TColor is not enough, and a list of custom color objects is too much.
as opposed to not enough...
or too much...
JD Solutions
Check if a specified file is a certain extension
Code:
function IsFileType(const Filename, Extension: String): Bool;
begin
Result:= (UpperCase(RightStr(Filename, Length(Extension)+1)) = '.'+UpperCase(Extension));
end;
Check if a specified service is installed (by service name, not image path)
Code:
function GetServiceInstalled(ServiceName: String): Bool;
var
Reg: TRegistry;
begin
Reg:= TRegistry.Create(RegAccess);
try
Reg.RootKey:= RegRoot;
Result:= Reg.KeyExists(RegSvcKey+ServiceName);
finally
Reg.Free;
end;
end;
Get the full path to a specified service (by service name)
Code:
function GetServicePath(ServiceName: String): String;
var
Reg: TRegistry;
begin
Reg:= TRegistry.Create(RegAccess);
try
Reg.RootKey:= RegRoot;
if Reg.KeyExists(RegSvcKey+ServiceName) then begin
if Reg.OpenKey(RegSvcKey+ServiceName, False) then begin
Result:= Reg.ReadString('ImagePath');
Reg.CloseKey;
end;
end else begin
Result:= '';
end;
finally
Reg.Free;
end;
end;
Check if a specified service is enabled (auto start)
Code:
function GetServiceEnabled(ServiceName: String): Bool;
var
Reg: TRegistry;
begin
Reg:= TRegistry.Create(RegAccess);
try
Reg.RootKey:= RegRoot;
if Reg.KeyExists(RegSvcKey+ServiceName) then begin
if Reg.OpenKey(RegSvcKey+ServiceName, False) then begin
if Reg.ReadInteger('Start') = 4 then
Result:= False
else
Result:= True;
Reg.CloseKey;
end;
end else begin
Result:= False;
end;
finally
Reg.Free;
end;
end;
Get the version string of a specified EXE
Code:
function GetFileVersion(FileName: string): string;
var
VerInfoSize: DWORD;
VerInfo: Pointer;
VerValueSize: DWORD;
VerValue: PVSFixedFileInfo;
Dummy: DWORD;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(FileName), Dummy);
GetMem(VerInfo, VerInfoSize);
GetFileVersionInfo(PChar(FileName), 0, VerInfoSize, VerInfo);
VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
with VerValue^ do
begin
Result := IntToStr(dwFileVersionMS shr 16);
Result := Result + '.' + IntToStr(dwFileVersionMS and $FFFF);
Result := Result + '.' + IntToStr(dwFileVersionLS shr 16);
Result := Result + '.' + IntToStr(dwFileVersionLS and $FFFF);
end;
FreeMem(VerInfo, VerInfoSize);
end;
Store database connection information with ability to get connection string
Object Structure:
Code:
uses SysUtils, StrUtils;
type
TDatabaseInfo = class(TPersistent)
private
fProvider: String;
fServer: String;
fDatabase: String;
fLogin: String;
fPassword: String;
function GetConnectionString: String;
procedure SetConnectionString(Value: String);
public
constructor Create;
destructor Destroy; override;
published
property Provider: String read fProvider write fProvider;
property Server: String read fServer write fServer;
property Database: String read fDatabase write fDatabase;
property Login: String read fLogin write fLogin;
property Password: String read fPassword write fPassword;
property ConnectionString: String read GetConnectionString write SetConnectionString;
end;
Code:
constructor TDatabaseInfo.Create;
begin
fProvider:= 'SQLOLEDB.1';
fLogin:= 'sa';
fServer:= '(local);
fDatabase:= 'master';
fPassword:= '';
end;
destructor TDatabaseInfo.Destroy;
begin
//Nothing to free
inherited Destroy;
end;
function TDatabaseInfo.GetConnectionString: String;
begin
if fProvider = '' then fProvider:= 'SQLOLEDB.1';
Result:= 'Persist Security Info=True'+
';Provider='+fProvider+
';Data Source='+fServer+
';Initial Catalog='+fDatabase+
';User ID='+fLogin+
';Password='+fPassword;
end;
procedure TDatabaseInfo.SetConnectionString(Value: String);
var
Str, T, N: String;
begin
Str:= Value;
if RightStr(Str, 1) <> ';' then Str:= Str + ';';
while Length(Str) > 0 do begin
T:= Copy(Str, 1, pos(';', Str)-1);
Delete(Str, 1, pos(';', Str));
N:= LowerCase(Copy(T, 1, pos('=', T)-1));
Delete(T, 1, pos('=', T));
if N = 'provider' then fProvider:= T
else if N = 'data source' then fServer:= T
else if N = 'initial catalog' then fDatabase:= T
else if N = 'user id' then fLogin:= T
else if N = 'password' then fPassword:= T;
end;
end;
Code:
var
DBInfo: TDatabaseInfo;
ConnectionString: String;
begin
DBInfo:= TDatabaseInfo.Create;
Q:= TADOQuery.Create(nil);
try
DBInfo.Provider:= 'SQLNCLI';
DBInfo.Server:= '192.168.4.109';
DBInfo.Database:= 'MyDatabase';
DBInfo.Login:= 'sa';
DBInfo.Password:= 'MyPassword';
ConnectionString:= DBInfo.ConnectionString;
finally
DDBInfo.Free;
end;
end;
Execute a SQL script, hide console window, and read result file
This is helpful when installing updates for software that uses a database
Some code is specific to my needs
This could use some cleaning and simplifying
Code:
const
UR_OK = 0;
UR_ABORTED = 1;
UR_MINOR_ERROR = 2;
UR_MAJOR_ERROR = 3;
//Directory: Where to run from
//SqlFile: SQL Script File to be executed (including path)
//DB: Database Information (Previous example)
//AHandle: Window Handle
//Results: Pre-created TStringList to save results to
// Result: Severity of error(s)
function ExecOSQL(Directory, SqlFile: string; DB: TDatabaseInfo;
AHandle: HWND; Results: TStringList): smallInt;
const
ResultFile = '_Results.txt';
var
Dir: String;
lst: TStringList;
s: string;
AppHAND, NextHandle: HWND;
NextTitle: array[0..260] of char;
x, y: smallInt;
z: integer;
begin
Dir:= Directory;
if RightStr(Dir, 1) <> '\' then Dir:= Dir + '\';
Result:= UR_OK;
//if CopyFile(PChar(SQLFile), PChar(Dir+ExtractFileName(SqlFile)), False) then begin
if FileExists(SqlFile) then begin
s:=' -U'+DB.Login+' -P'+DB.Password+' -d'+DB.Database+' -S'+DB.Server+
' -i'+SqlFile+' -o'+ResultFile;
if assigned(Results) then Results.Clear;
if Uppercase(DB.Database) <> 'RUGMSSQL' then begin
lst:= TstringList.Create;
try
lst.LoadFromFile(Dir+SqlFile);
if lst.Count > 15 then y:= 15 else y:= lst.Count - 1;
for x:= 0 to y do begin
if pos('USE RUGMSSQL', Uppercase(lst[x])) > 0 then begin
lst[x]:= 'USE '+DB.Database;
lst.SaveToFile(sqlFile);
Break;
end;
end;
finally
lst.Free;
end;
end;
AppHand:= ShellExecute(AHandle, nil, 'osql.exe',
PChar(s), PChar(Dir), SW_HIDE);
if AppHand > 32 then
begin
Sleep(1000);
AppHAND:= 0;
NextHandle:= GetWindow(AHandle, GW_HWNDFIRST);
while NextHandle > 0 do begin
GetWindowText(NextHandle, NextTitle, 255);
if pos('\osql.exe', StrPas(NextTitle)) > 0 then begin
AppHAND:= NextHandle;
NextHandle:= 0;
end else
NextHandle := GetWindow(NextHandle, GW_HWNDNEXT); // Get the next window
end;
x:= 0;
if AppHAND > 0 then begin //no more then 200 sec
while (IsWindow(AppHAND))and(x < 150) do begin //wait for the window to close
inc(x);
Sleep(1000);
end;
end else begin
for z:= 1 to 5 do begin
sleep(1000);
end;
end;
if FileExists(Dir+ResultFile) then begin
lst:= TstringList.Create;
try
try
lst.LoadFromFile(Dir+ResultFile);
for x:= 0 to lst.count - 1 do begin
if (lst[x][1]<> '<')and(not (lst[x][1] in ['0'..'9'])) then begin
if assigned(Results) then
Results.Append('OSQL Result: '+lst[x]);
s:= lowercase(lst[x]);
if
(pos('error', s)> 0)
or
(pos('invalid column', s)> 0)
or
(pos('invalid object name', s)> 0)
or
(pos('invalid user', s)> 0)
or
(pos('login failed', s)> 0)
or
(pos('server does not exist or access denied', s)> 0)
then
Result:= UR_MAJOR_ERROR;
if
(pos('invalid column', s)> 0)
or
(pos('invalid object name', s)> 0)
or
(pos('cannot find the object', s)> 0)
then
Result:= UR_MINOR_ERROR;
end;
end;
except
on e: exception do begin
if assigned(Results) then
Results.Append('Error: '+e.Message);
end;
end;
finally
lst.Free;
end;
//DeleteFile(PChar(Dir+ResultFile));
end
else Result:= UR_MAJOR_ERROR;
end;
end else begin
if assigned(Results) then
Results.Append('SQL file failed to copy');
Result:= UR_MAJOR_ERROR;
end;
end;
Declare type which holds an array of colors which you can easily extract RGB values
This is helpful when an array of TColor is not enough, and a list of custom color objects is too much.
Code:
uses Windows;
const
PixelCountMax = 32768;
type
pRGBArray = ^TRGBArray;
TRGBArray = array[0..PixelCountMax-1] of TRGBTriple;
as opposed to not enough...
Code:
type
TColorArray = array[0..PixelCountMax-1] of TColor;
Code:
type
TRGB = class(TPersistent)
private
fColor: TColor;
function GetRed: Integer;
function GetGreen: Integer;
function GetBlue: Integer;
procedure SetRed(Value: Integer);
procedure SetGreen(Value: Integer);
procedure SetBlue(Value: Integer);
public
constructor Create;
published
property Red: Integer read GetRed write SetRed;
property Green: Integer read GetGreen write SetGreen;
property Blue: Integer read GetBlue write SetBlue;
property Color: TColor read fColor write fColor;
end;
TRMPColors = class(TPersistent)
private
fList: TList; //of TRGB
function GetRGB(Index: Integer): TRGB;
procedure SetRGB(Index: Integer; Value: TRGB);
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
property Colors[Index: Integer]: TRGB read GetRGB write SetRGB;
procedure Add(Color: TRGB); overload;
procedure Add(Color: TColor); overload;
property Count: Integer read GetCount;
end;
//And a lot more I won't even bother to touch....
JD Solutions