Delphi – Create a single instance of your app

Hi guys. After searching for a while on how to lock and limit the app clicking. And this is now possible.

Here is the code for the main project file

program Project2;

 

uses

Forms, Windows, Messages, Dialogs,

Unit1 in ‘Unit1.pas’ {Form1};

 

{$R *.res}

 

function CreateSingleInstance(const InstanceName: string): boolean;

var

MutexHandle: THandle;

begin

MutexHandle := CreateMutex(nil, false, PChar(InstanceName));

// if MutexHandle created check if already exists

if (MutexHandle <> 0) then

begin

if GetLastError = ERROR_ALREADY_EXISTS then

begin

Result := false;

CloseHandle(MutexHandle);

end

else Result := true;

end

else Result := false;

end;

 

var

MyInstanceName: string;

begin

MyInstanceName := ‘Project2’;

Application.Initialize;

// Initialize MyInstanceName here

if CreateSingleInstance(MyInstanceName) then

begin

// Form creation

Application.CreateForm(TForm1, Form1);

Application.Run;

end

else Application.Terminate;

end.

Delphi : Open Excel File using ShellExecute

This is a nifty code that i use whenever I do excel conversions. It helps users by seeing the excel file and not have to look for the file once the system has generated the output file

ShellExecute(Handle, 'open', 'c:\MyDocuments\MyFile.doc',nil,nil,SW_SHOWNORMAL);

In this example, the file is located inside the ‘My Documents’ folder with the file name ‘MyFile.xls’. Just in case the file name you are using is dynamic, you can enclose the variable with a PChar.

ShellExecute(Handle, 'open', PChar(varMyFilename),nil,nil,SW_SHOWNORMAL) ;

Enjoy

Coffee Cup

Delphi : Date and Time Format from ddd-mm-yy to mm/dd/yyyy

i had a case a few weeks back of a problem where the computer needs to have a date format of dd-mmm-yy which in today’s date is 21-Feb-18. What i need is for the program to act the same as it was with the format 02/21/2018.

After digging data, i came across these gems of a find. It’s so short and so obvious, i hope this code can help somebody out

Enjoy

Coffee Cup

 

 

 

{
CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator,
DecimalSeparator, CurrencyDecimals, DateSeparator, ShortDateFormat,
LongDateFormat, TimeSeparator, TimeAMString, TimePMString,
ShortTimeFormat, LongTimeFormat

//Change COMPUTER date and time
SetLocaleInfo(GetThreadLocale, LOCALE_SSHORTDATE, ‘MM/dd/yyyy’);
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0,
SMTO_ABORTIFHUNG, 1000, PDWord(Nil)^);

//Change formatting from within the APPLICATION
DateSeparator := ‘/’;
ShortDateFormat := ‘MM/dd/yyyy’;

SetLocaleInfo(DefaultLCID, LOCALE_SSHORTDATE, ‘m/d/yy’) (short form) and
SetLocaleInfo(DefaultLCID, LOCALE_SLONGDATE, ‘mmmm d, yyyy’) (long form)

SetLocaleInfo(GetThreadLocale, LOCALE_SSHORTDATE, ‘MM/dd/yyyy’);
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0,
SMTO_ABORTIFHUNG, 1000, PDWord(Nil)^);
}

// Change the display formatting
DateSeparator := ‘/’;
ShortDateFormat := ‘MM/dd/yyyy’;

What TCP port does InterBase use?

Before it was like clockwork, this port is no mystery to me. But recently, especially today, i had to write it down somethere.

I guess this comes with age.

Magic interbase port is 3050. This must be both in the INCOMING and OUTGOING windows firewall settings.

Coffee Cup