Thursday, June 25, 2009

Locate Open Unit on Disk

Delphi can open Windows Explorer and highlight the open file as well. Here is how to setup

Tools -> Configure Tools -> Add
Title: Explorer
Program: Explorer.exe
Working Dir:
Parameters: /select, $EDNAME

Next time you want to locate a file from IDE on disk. Select Tools->Explorer.

Friday, April 3, 2009

Validating Email ID

Sometimes we are in a situation where we need to validate an email id in our applications. Recently I had one such requirement in one of my project and found this piece of code that validates an email id (string) and returns a boolean value indicating whether the email id is a well formed email id.


function InvalidEmailFormat(const AEmailAddress: String): Boolean;
var
LIndex: Integer;
LTemp : string;
begin
// ' ', ä, ö, ü, ß, [, ], (, ), : in EMail-Address

Result := (Trim(AEmailAddress) = '') or
(Pos(' ', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos('ä', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos('ö', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos('ü', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos('ß', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos('[', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos(']', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos('(', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos(')', AnsiLowerCase(AEmailAddress)) > 0) or
(Pos(':', AnsiLowerCase(AEmailAddress)) > 0);
if Result then Exit; // @ not in EMail-Address;

LIndex := Pos('@', AEmailAddress);
Result := (LIndex = 0) or (LIndex = 1) or (LIndex = Length(AEmailAddress));
if Result then Exit;

Result := (Pos('@', Copy(AEmailAddress, LIndex + 1, Length(AEmailAddress) - 1)) > 0);
if Result then Exit; // Domain <= 1
LTemp := Copy(AEmailAddress, LIndex + 1, Length(AEmailAddress));
Result := Length(LTemp) <= 1;
if Result then Exit;
LIndex := Pos('.', LTemp);
Result := (LIndex = 0) or (LIndex = 1) or (LIndex = Length(LTemp));
end;

This piece of code proved to be very handy for me whenever I needed to validate the email address entered by user.

Until Next time.

Monday, March 9, 2009

Media Player Play State in Delphi

Ever wondered or spent lots of time figuring out how to manipulate the Windows Media Player in Delphi. I mean TWindowsMediaPlayer (ActiveX control).

Today I had to retrieve the status of media being played in Media player to find out whether the media is paused or not. After hunting for almost 30 mins I found a way to read the same.

type
WMPPlayState = TOleEnum;
const
wmppsUndefined = $00000000;
wmppsStopped = $00000001;
wmppsPaused = $00000002;
wmppsPlaying = $00000003;
wmppsScanForward = $00000004;
wmppsScanReverse = $00000005;
wmppsBuffering = $00000006;
wmppsWaiting = $00000007;
wmppsMediaEnded = $00000008;
wmppsTransitioning = $00000009;
wmppsReady = $0000000A;
wmppsReconnecting = $0000000B;
wmppsLast = $0000000C;

WMPlayer.playState can be used to retrieve the same. The possible values are the ones mentioned above.

Until Next Time.

Tuesday, February 24, 2009

Memory Mapped File in Delphi

One of the recent learning of mine is to use Memory Mapped File in Delphi. These are the great ways to share data among various libraries within the same application instance.

Step 1: Define a Record Structure and a Pointer to the record.
type
PRecordInfo = ^TRecordInfo;
TRecordInfo = packed record
Field1 : ShortString;
Field2 : Integer;
Field3 : Integer;
end; //TInstanceInfo

Step 2 : Variable Declaration. I prefer to do it in implementation section for many good reasons known to Delphi Programmers.
var
FMappingHandle : THandle = 0;
FRecordInfo : PRecordInfo= nil;
FMappingName : String = '';

Step 3: Manipulate Memory mapped file. Use the code snippets below to perform the operation as per your business logic.

{ To Create a Mapping File }
FMappingHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(TRecordInfo), @FMappingName[1]);
FRecordInfo := MapViewOfFile(FMappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TRecordInfo));
FRecordInfo.Field1 := 'Value 1';
FRecordInfo.Field2 := 0;
FRecordInfo.Field3 := 1;

{ To Open the Memory Map File }
FMappingName := 'SomeNametoUniquelyIdentifyTheMappingFile';
FMappingHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, false, @FMappingName[1]);

{ To Read the File Contents}
FRecordInfo := MapViewOfFile(FMappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TRecordInfo));
LVariable1 := FConfigInfo^.Field1;//Read the Contents
LVariable2 := FConfigInfo^.Field2;//Read the Contents
LVariable3 := FConfigInfo^.Field3;//Read the Contents

Enjoy working with Memory mapped files. I found them best way to share data among different modules of the application. They are quite useful if you have one EXE and multiple DLLs in an application.

Thursday, February 19, 2009

Vista - Lost Network Icon and Volume Icon

For sometime I'm having this problem with my Vista machine. I am loosing the Volume Icon, Power , Network icon located in the task bar (System Tray). I tried multiple ways to get them back but so far only one resource helped more than anything else. This is the help from Microsoft Knowledgebase.

But last week after I lost my icon even the trick mentioned in this document did not bring it back. Now I am searching for some ways to get those icons back. Has anyone experienced the similar problem and have a full-proof solutions? I would love to hear from you.

Wednesday, February 18, 2009

Introducing Geek's Voice

A Warm Hello and Welcome to my readers on this new blog of mine. For past 2+ years I've been blogging about various things be it Semantic Database, Mithila Cuisine or various other blogs. But one thing that was missing in all those blogs were the voice of the inner geek of mine. The opinion about the technology I play with sometime and some of the findings of mine.

I as a geek have my opinions about the technology I use and also what I learn in my day to day life. Some of those I can share with people I work and some of them are kept aside. But here I am going to share the unbiased opinion about what I learn in my day-to-day life as a geek.

With these ideas in mind, I started this blog. I hope to share lots of technical insights with the readers in coming days / weeks / months / years.

Until Next Time....