SendKeys from WM_HOTKEY handler - delphi

Have AppActivate and SendKeys functions.
When use: AppActivate('*WordPad'); SendKeys('Test");
this works fine - application activated and text pasted
but then use it from WM_HOTKEY handler from the same program,
this is not worked.
Any ideas?
procedure wm_hotkeyhandler(var Msg: TWMHotkey); message WM_HOTKEY;
...
procedure TFormMain.wm_hotkeyhandler(var Msg: TWMHotkey);
var
Pt: TPoint;
begin
inherited;
if (Msg.HotKey = HotKeyId_L) then SendKeys('Test'); // not pasted to active app
if (Msg.HotKey = HotKeyId_M) then begin
// invoke context menu and paste text after click to menu item, works fine
GetCursorPos(Pt);
popPaste.Popup(Pt.x, Pt.y);
end;
end;
Update 1:
// this code works fine
procedure TFormTest.btnAppActivateClick(Sender: TObject);
var
sTitle, sKeys: string;
begin
sTitle := '*WordPad';
sKeys := 'Hello{ENTER}World!';
AppActivate(sTitle);
SendKeys(PChar(sKeys), False);
end;
menu item handler (which invoked by hotkey HotKeyId_M):
procedure TFormMain.mnPasteLoginClick(Sender: TObject);
begin
SendKeys('Hello{ENTER}World!', False);
end;
hotkeys:
HotKeyId_L: Integer;
HotKeyId_M: Integer;
initialization of hotkeys:
HotKeyId_L := GlobalAddAtom('HotKeyL');
RegisterHotKey(Handle, HotKeyId_L, MOD_CONTROL + MOD_ALT, Byte('L'));
HotKeyId_M := GlobalAddAtom('HotKeyM');
RegisterHotKey(Handle, HotKeyId_L, MOD_CONTROL + MOD_ALT, Byte('M'));
Update 2: (full code for test)
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus;
type
TForm2 = class(TForm)
btnActivate: TButton;
popPopup: TPopupMenu;
Paste1: TMenuItem;
procedure btnActivateClick(Sender: TObject);
procedure Paste1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
HotKeyId_L: Integer;
HotKeyId_M: Integer;
procedure wm_hotkeyhandler(var Msg: TWMHotkey); message WM_HOTKEY;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
type
TCompareDirection = (cdHead, cdTail, cdNone);
TWindowObj = class(TObject)
private
targetTitle : PChar;
compareLength: Integer;
FCompareDirection: TCompareDirection;
FWindowHandle: THandle;
public
constructor Create;
destructor Destroy; override;
function Equal(ATitle: PChar): Boolean;
function SetTitle(const Title: string ): Boolean;
property WindowHandle: THandle read FWindowHandle write FWindowHandle;
end;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM):Bool; export; stdcall;
var
WinObj: TWindowObj;
aWndName: array[0..MAX_PATH] of Char;
begin
Result := True;
WinObj := TWindowObj(lParam);
GetWindowText(hWnd, aWndName, MAX_PATH);
if WinObj.Equal(aWndName) then begin
WinObj.WindowHandle := hWnd;
Result := False; // Stop Enumerate
end;
end;
function GetWindowHandleByTitle(const Title: string): THandle;
var
WinObj: TWindowObj;
begin
Result := 0;
WinObj := TWindowObj.Create;
try
if WinObj.SetTitle(Title) then begin
EnumWindows(#EnumWindowsProc, Integer(WinObj));
Result := WinObj.WindowHandle;
end;
finally
WinObj.Free;
end;
end;
function AppActivate(const Title: string ): Boolean;
var
hWnd: THandle;
begin
hWnd := GetWindowHandleByTitle(Title);
Result := (hWnd > 0);
if Result then begin
SendMessage(hWnd, WM_SYSCOMMAND, SC_HOTKEY, hWnd);
SendMessage(hWnd, WM_SYSCOMMAND, SC_RESTORE, hWnd);
SetForegroundWindow(hWnd);
end;
end;
constructor TWindowObj.Create;
begin
TargetTitle := nil;
FWindowHandle := 0;
end;
destructor TWindowObj.Destroy;
begin
inherited Destroy;
if Assigned(TargetTitle) then
StrDispose(TargetTitle) ;
end;
function TWindowObj.Equal(ATitle: PChar): Boolean;
var
p : Pchar;
stringLength : integer;
begin
Result := False;
if (TargetTitle = nil) then
Exit;
case FCompareDirection of
cdHead: begin
if StrLIComp(ATitle, TargetTitle, compareLength) = 0 then
Result := True;
end;
cdTail: begin
stringLength := StrLen(ATitle);
p := #ATitle[stringLength - compareLength];
if (StrLIComp(p, Targettitle, compareLength) = 0) then
Result := True;
end;
cdNone: begin
Result := True;
end;
end;
end;
function TWindowObj.SetTitle(const Title: string ): Boolean;
var
pTitle, p: PChar;
begin
Result := False;
pTitle := StrAlloc(Length(Title) + 1);
StrPCopy(pTitle, Title);
p := StrScan(pTitle, '*');
if Assigned(p) then begin
if StrLen(pTitle) = 1 then begin {full matching }
FCompareDirection := cdNone;
compareLength := 0;
TargetTitle := nil;
StrDispose(pTitle);
end
else
if (p = pTitle) then begin {tail matching }
Inc(p);
if StrScan(p, '*') <> nil then begin
{MessageDlg( 'Please 1 wild char ', mtError, [mbOK],0 ); }
StrDispose( pTitle);
TargetTitle := nil;
FCompareDirection := cdNone;
Comparelength := 0;
exit;
end;
FCompareDirection := cdTail;
CompareLength := StrLen(PTitle) - 1;
TargetTitle := StrAlloc(StrLen(p) + 1 );
StrCopy(targetTitle, p);
StrDispose(PTitle);
end
else begin
p^ := #0;
FCompareDirection := cdHead;
CompareLength := Strlen( pTitle );
Targettitle := pTitle;
end;
end
else begin
FCompareDirection := cdHead;
compareLength := Strlen( pTitle );
TargetTitle := pTitle;
end;
Result := True;
end;
//========================================
// SendKeys
//
// Converts a string of characters and key names to keyboard events and passes them to Windows.
//
// Example syntax:
// SendKeys('abc123{left}{left}{left}def{end}456{left 6}ghi{end}789', True);
function SendKeys(SendStr: PChar; Wait: Boolean): Boolean;
type
WBytes = array[0..pred(SizeOf(Word))] of Byte;
TSendKey = record
Name : ShortString;
VKey : Byte;
end;
const
// Array of keys that SendKeys recognizes.
// If you add to this list, you must be sure to keep it sorted alphabetically
// by Name because a binary search routine is used to scan it.}
MaxSendKeyRecs = 41;
SendKeyRecs : array[1..MaxSendKeyRecs] of TSendKey = (
(Name:'BACKSPACE'; VKey:VK_BACK),
(Name:'BKSP'; VKey:VK_BACK),
(Name:'BREAK'; VKey:VK_CANCEL),
(Name:'BS'; VKey:VK_BACK),
(Name:'CAPSLOCK'; VKey:VK_CAPITAL),
(Name:'CLEAR'; VKey:VK_CLEAR),
(Name:'DEL'; VKey:VK_DELETE),
(Name:'DELETE'; VKey:VK_DELETE),
(Name:'DOWN'; VKey:VK_DOWN),
(Name:'END'; VKey:VK_END),
(Name:'ENTER'; VKey:VK_RETURN),
(Name:'ESC'; VKey:VK_ESCAPE),
(Name:'ESCAPE'; VKey:VK_ESCAPE),
(Name:'F1'; VKey:VK_F1),
(Name:'F10'; VKey:VK_F10),
(Name:'F11'; VKey:VK_F11),
(Name:'F12'; VKey:VK_F12),
(Name:'F13'; VKey:VK_F13),
(Name:'F14'; VKey:VK_F14),
(Name:'F15'; VKey:VK_F15),
(Name:'F16'; VKey:VK_F16),
(Name:'F2'; VKey:VK_F2),
(Name:'F3'; VKey:VK_F3),
(Name:'F4'; VKey:VK_F4),
(Name:'F5'; VKey:VK_F5),
(Name:'F6'; VKey:VK_F6),
(Name:'F7'; VKey:VK_F7),
(Name:'F8'; VKey:VK_F8),
(Name:'F9'; VKey:VK_F9),
(Name:'HELP'; VKey:VK_HELP),
(Name:'HOME'; VKey:VK_HOME),
(Name:'INS'; VKey:VK_INSERT),
(Name:'LEFT'; VKey:VK_LEFT),
(Name:'NUMLOCK'; VKey:VK_NUMLOCK),
(Name:'PGDN'; VKey:VK_NEXT),
(Name:'PGUP'; VKey:VK_PRIOR),
(Name:'PRTSC'; VKey:VK_PRINT),
(Name:'RIGHT'; VKey:VK_RIGHT),
(Name:'SCROLLLOCK'; VKey:VK_SCROLL),
(Name:'TAB'; VKey:VK_TAB),
(Name:'UP'; VKey:VK_UP)
);
{Extra VK constants missing from Delphi's Windows API interface}
VK_NULL=0;
VK_SemiColon=186;
VK_Equal=187;
VK_Comma=188;
VK_Minus=189;
VK_Period=190;
VK_Slash=191;
VK_BackQuote=192;
VK_LeftBracket=219;
VK_BackSlash=220;
VK_RightBracket=221;
VK_Quote=222;
VK_Last=VK_Quote;
ExtendedVKeys : set of byte =
[VK_Up,
VK_Down,
VK_Left,
VK_Right,
VK_Home,
VK_End,
VK_Prior, {PgUp}
VK_Next, {PgDn}
VK_Insert,
VK_Delete];
const
INVALIDKEY = $FFFF {Unsigned -1};
VKKEYSCANSHIFTON = $01;
VKKEYSCANCTRLON = $02;
VKKEYSCANALTON = $04;
UNITNAME = 'SendKeys';
var
UsingParens, ShiftDown, ControlDown, AltDown, FoundClose : Boolean;
PosSpace : Byte;
I, L : Integer;
NumTimes, MKey : Word;
KeyString : String[20];
procedure DisplayMessage(Msg: PChar);
begin
MessageBox(0, Msg, UNITNAME, 0);
end;
function BitSet(BitTable, BitMask: Byte): Boolean;
begin
Result := ByteBool(BitTable and BitMask);
end;
procedure SetBit(var BitTable : Byte; BitMask : Byte);
begin
BitTable:=BitTable or Bitmask;
end;
procedure KeyboardEvent(VKey, ScanCode: Byte; Flags: DWORD);
var
KeyboardMsg : TMsg;
begin
keybd_event(VKey, ScanCode, Flags, 0);
if Wait then
while PeekMessage(KeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) do begin
TranslateMessage(KeyboardMsg);
DispatchMessage(KeyboardMsg);
end;
end;
procedure SendKeyDown(VKey: Byte; NumTimes: Word; GenUpMsg: Boolean);
var
Cnt: Word;
ScanCode: Byte;
NumState: Boolean;
KeyBoardState: TKeyboardState;
begin
if (VKey = VK_NUMLOCK) then begin
NumState := ByteBool(GetKeyState(VK_NUMLOCK) and 1);
GetKeyBoardState(KeyBoardState);
if NumState then
KeyBoardState[VK_NUMLOCK] := (KeyBoardState[VK_NUMLOCK] and not 1)
else
KeyBoardState[VK_NUMLOCK] := (KeyBoardState[VK_NUMLOCK] or 1);
SetKeyBoardState(KeyBoardState);
Exit;
end;
ScanCode := Lo(MapVirtualKey(VKey, 0));
for Cnt := 1 to NumTimes do
if (VKey in ExtendedVKeys) then begin
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY);
if GenUpMsg then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)
end
else begin
KeyboardEvent(VKey, ScanCode, 0);
if GenUpMsg then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);
end;
end;
procedure SendKeyUp(VKey: Byte);
var
ScanCode : Byte;
begin
ScanCode := Lo(MapVirtualKey(VKey, 0));
if (VKey in ExtendedVKeys)then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)
else
KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);
end;
procedure SendKey(MKey: Word; NumTimes: Word; GenDownMsg: Boolean);
begin
if BitSet(Hi(MKey), VKKEYSCANSHIFTON) then SendKeyDown(VK_SHIFT, 1, False);
if BitSet(Hi(MKey), VKKEYSCANCTRLON) then SendKeyDown(VK_CONTROL, 1, False);
if BitSet(Hi(MKey), VKKEYSCANALTON) then SendKeyDown(VK_MENU, 1, False);
SendKeyDown(Lo(MKey), NumTimes, GenDownMsg);
if BitSet(Hi(MKey), VKKEYSCANSHIFTON) then SendKeyUp(VK_SHIFT);
if BitSet(Hi(MKey), VKKEYSCANCTRLON) then SendKeyUp(VK_CONTROL);
if BitSet(Hi(MKey), VKKEYSCANALTON) then SendKeyUp(VK_MENU);
end;
// Implements a simple binary search to locate special key name strings
function StringToVKey(KeyString: ShortString): Word;
var
Found, Collided : Boolean;
Bottom, Top, Middle : Byte;
begin
Result := INVALIDKEY;
Bottom := 1;
Top := MaxSendKeyRecs;
Found := False;
Middle := (Bottom + Top) div 2;
repeat
Collided:=((Bottom=Middle) or (Top=Middle));
if (KeyString=SendKeyRecs[Middle].Name) then begin
Found:=True;
Result:=SendKeyRecs[Middle].VKey;
end
else begin
if (KeyString>SendKeyRecs[Middle].Name) then
Bottom:=Middle
else
Top:=Middle;
Middle:=(Succ(Bottom+Top)) div 2;
end;
until (Found or Collided);
if (Result = INVALIDKEY) then
DisplayMessage('Invalid Key Name');
end;
procedure PopUpShiftKeys;
begin
if (not UsingParens) then begin
if ShiftDown then SendKeyUp(VK_SHIFT);
if ControlDown then SendKeyUp(VK_CONTROL);
if AltDown then SendKeyUp(VK_MENU);
ShiftDown := False;
ControlDown := False;
AltDown := False;
end;
end;
var
AllocationSize : integer;
begin
AllocationSize := MaxInt;
Result := False;
UsingParens := False;
ShiftDown := False;
ControlDown := False;
AltDown := False;
I := 0;
L := StrLen(SendStr);
if (L > AllocationSize) then
L := AllocationSize;
if (L = 0) then
Exit;
while (I < L) do begin
case SendStr[I] of
'(': begin
UsingParens := True;
Inc(I);
end;
')': begin
UsingParens := False;
PopUpShiftKeys;
Inc(I);
end;
'%': begin
AltDown := True;
SendKeyDown(VK_MENU, 1, False);
Inc(I);
end;
'+': begin
ShiftDown := True;
SendKeyDown(VK_SHIFT, 1, False);
Inc(I);
end;
'^': begin
ControlDown := True;
SendKeyDown(VK_CONTROL, 1, False);
Inc(I);
end;
'{': begin
NumTimes := 1;
if (SendStr[Succ(I)] = '{') then begin
MKey := VK_LEFTBRACKET;
SetBit(WBytes(MKey)[1], VKKEYSCANSHIFTON);
SendKey(MKey, 1, True);
PopUpShiftKeys;
Inc(I, 3);
Continue;
end;
KeyString := '';
FoundClose := False;
while (I <= L) do begin
Inc(I);
if (SendStr[I] = '}') then begin
FoundClose := True;
Inc(I);
Break;
end;
KeyString := KeyString + Upcase(SendStr[I]);
end;
if Not FoundClose then begin
DisplayMessage('No Close');
Exit;
end;
if (SendStr[I] = '}') then begin
MKey := VK_RIGHTBRACKET;
SetBit(Wbytes(MKey)[1], VKKEYSCANSHIFTON);
SendKey(MKey, 1, True);
PopUpShiftKeys;
Inc(I);
Continue;
end;
PosSpace:=Pos(' ', KeyString);
if (PosSpace <> 0) then begin
NumTimes := StrToInt(Copy(KeyString, Succ(PosSpace), Length(KeyString) - PosSpace));
KeyString := Copy(KeyString, 1, Pred(PosSpace));
end;
If (Length(KeyString)=1) then
MKey := vkKeyScan(KeyString[1])
else
MKey := StringToVKey(KeyString);
If (MKey <> INVALIDKEY) then begin
SendKey(MKey, NumTimes, True);
PopUpShiftKeys;
Continue;
end;
end;
'~': begin
SendKeyDown(VK_RETURN, 1, True);
PopUpShiftKeys;
Inc(I);
end;
else
MKey := vkKeyScan(SendStr[I]);
if (MKey <> INVALIDKEY) then begin
SendKey(MKey, 1, True);
PopUpShiftKeys;
end
else
DisplayMessage('Invalid KeyName');
Inc(I);
end;
end;
Result := True;
PopUpShiftKeys;
end;
procedure TForm2.btnActivateClick(Sender: TObject);
var
sTitle, sKeys: string;
begin
sTitle := '*WordPad';
sKeys := 'Hello{ENTER}World!';
AppActivate(sTitle);
SendKeys(PChar(sKeys), False);
end;
procedure TForm2.Paste1Click(Sender: TObject);
begin
SendKeys('Hello{ENTER}World!', False);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
HotKeyId_L := GlobalAddAtom('HotKeyP');
RegisterHotKey(Handle, HotKeyId_L, MOD_CONTROL or MOD_ALT, Byte('L'));
HotKeyId_M := GlobalAddAtom('HotKeyM');
RegisterHotKey(Handle, HotKeyId_M, MOD_CONTROL or MOD_ALT, Byte('M'));
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle, HotKeyId_L);
GlobalDeleteAtom(HotKeyId_L);
end;
procedure TForm2.wm_hotkeyhandler(var Msg: TWMHotkey);
var
Pt: TPoint;
begin
inherited;
if (Msg.HotKey = HotKeyId_L) then SendKeys('Hello{ENTER}World!', False);
if (Msg.HotKey = HotKeyId_M) then begin
GetCursorPos(Pt);
popPopup.Popup(Pt.x, Pt.y);
end;
end;
end.

Related

Delphi RichEdit - add multiple CFE_LINK effect via CHARFORMAT2

I have made a code, that inserts CFE_LINK into RichEdit text, but it works only for last inserted text. All previous insertions of Links are Undone.
I want to insert multiple Link-texts, but I cant figure out how to do that.
Here is a working code (with no errors):
unit uRichEditExtended;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, RichEdit, WinApi.ShellApi, Vcl.Controls, Vcl.ComCtrls,
Generics.Collections;
type
TZ_RichEditClickEvent = reference to procedure(const ALinkText: string);
TZ_RichEditLink = class
IsDefaultEvent: boolean;
Text: string;
OnLinkClickEvent: TZ_RichEditClickEvent;
end;
TZ_RichEditLinks = TList<TZ_RichEditLink>;
TRichEditExtended = class
protected
class var FInstance: TRichEditExtended;
private
FPrevRichEditWndProc: TWndMethod;
FRichEdit: TRichEdit;
FRichEditLinks: TZ_RichEditLinks;
procedure InsertLinkText(const LinkText: string; SelStart: integer = -1);
procedure SetRichEditMasks;
procedure RichEditWndProc(var Message: TMessage);
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
public
class function This: TRichEditExtended;
class procedure ApplyRichEdit(ARichEdit: TRichEdit); // -1 - inserts to the end of text, otherwise into a position indicated by SelStart
class function AddLinkText(AText: string; AOnLinkClickEvent: TZ_RichEditClickEvent; SelStart: integer = -1): integer;
class function AddLinkTextWithDefaultEvent(AText: string; SelStart: integer = -1): integer;
class procedure AddDefaultLinkTextEvent(AOnLinkClickEvent: TZ_RichEditClickEvent);
end;
implementation
{ TRichEditExtended }
uses StrUtils;
class procedure TRichEditExtended.AddDefaultLinkTextEvent(AOnLinkClickEvent: TZ_RichEditClickEvent);
var
REL: TZ_RichEditLink;
begin
if (This.FRichEditLinks.Count > 0) and This.FRichEditLinks[0].IsDefaultEvent then
This.FRichEditLinks[0].OnLinkClickEvent := AOnLinkClickEvent
else
begin
REL := TZ_RichEditLink.Create;
REL.IsDefaultEvent := true;
REL.Text := '';
REL.OnLinkClickEvent := AOnLinkClickEvent;
This.FRichEditLinks.Insert(0, REL);
REL := nil;
end;
end;
class function TRichEditExtended.AddLinkText(AText: string; AOnLinkClickEvent: TZ_RichEditClickEvent; SelStart: integer = -1): integer;
var REL: TZ_RichEditLink;
begin
REL := TZ_RichEditLink.Create;
REL.IsDefaultEvent := false;
REL.Text := AText;
REL.OnLinkClickEvent := AOnLinkClickEvent;
Result := This.FRichEditLinks.Add(REL);
This.InsertLinkText(AText, SelStart);
REL := nil;
end;
class function TRichEditExtended.AddLinkTextWithDefaultEvent(AText: string; SelStart: integer): integer;
begin
This.AddLinkText(AText, nil, SelStart);
end;
procedure TRichEditExtended.AfterConstruction;
begin
inherited;
FRichEdit := nil;
FRichEditLinks := TZ_RichEditLinks.Create;
end;
class procedure TRichEditExtended.ApplyRichEdit(ARichEdit: TRichEdit);
begin
This.FRichEdit := ARichEdit;
This.FPrevRichEditWndProc := This.FRichEdit.WindowProc;
This.FRichEdit.WindowProc := This.RichEditWndProc;
This.FRichEditLinks.Clear;
This.SetRichEditMasks;
end;
procedure TRichEditExtended.BeforeDestruction;
begin
if Assigned(FRichEdit) then
FRichEdit.WindowProc := FPrevRichEditWndProc;
FRichEdit := nil;
FRichEditLinks.Clear;
FRichEditLinks.Free;
inherited;
end;
procedure TRichEditExtended.InsertLinkText(const LinkText: string; SelStart: integer = -1);
var
Fmt: CHARFORMAT2;
begin
if SelStart = -1 then
begin
SelStart := FRichEdit.Lines.Text.Length - 1;
FRichEdit.Text := FRichEdit.Text + LinkText;
dec(SelStart,2 * (FRichEdit.Lines.Text.CountChar(#$D) - 1));
end
else
begin
FRichEdit.SelStart := SelStart;
FRichEdit.SelText := LinkText;
end;
FRichEdit.SelStart := SelStart;
FRichEdit.SelLength := Length(LinkText);
FillChar(Fmt, SizeOf(Fmt), 0);
Fmt.cbSize := SizeOf(Fmt);
Fmt.dwMask := CFM_LINK;
Fmt.dwEffects := CFE_LINK;
SendMessage(FRichEdit.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(#Fmt));
FRichEdit.SelStart := SelStart + Length(LinkText);
FRichEdit.SelLength := 0;
end;
procedure TRichEditExtended.RichEditWndProc(var Message: TMessage);
type
PENLINK = ^ENLINK;
var
tr: TEXTRANGE;
str: string;
p: PENLINK;
i: integer;
begin
FPrevRichEditWndProc(Message);
case Message.Msg of
CN_NOTIFY: begin
if TWMNotify(Message).NMHdr.code = EN_LINK then
begin
P := PENLINK(Message.LParam);
if p.msg = WM_LBUTTONDOWN then
begin
SetLength(str, p.chrg.cpMax - p.chrg.cpMin);
tr.chrg := p.chrg;
tr.lpstrText := PChar(str);
SendMessage(FRichEdit.Handle, EM_GETTEXTRANGE, 0, LPARAM(#tr));
for I := 0 to FRichEditLinks.Count - 1 do
if str.ToUpper.Equals(FRichEditLinks[I].Text.ToUpper) then
begin
if not Assigned(FRichEditLinks[I].OnLinkClickEvent) then
begin
if not FRichEditLinks[0].IsDefaultEvent then
raise Exception.Create('No default event is set.')
else
FRichEditLinks[0].OnLinkClickEvent(str)
end
else
FRichEditLinks[I].OnLinkClickEvent(str);
exit;
end;
end;
end;
end;
CM_RECREATEWND: begin
SetRichEditMasks;
end;
end;
end;
procedure TRichEditExtended.SetRichEditMasks;
var
Mask: DWORD;
begin
Mask := SendMessage(FRichEdit.Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(FRichEdit.Handle, EM_SETEVENTMASK, 0, Mask or ENM_LINK);
SendMessage(FRichEdit.Handle, EM_AUTOURLDETECT, 1, 0);
end;
class function TRichEditExtended.This: TRichEditExtended;
begin
if not Assigned(TRichEditExtended.FInstance) then
TRichEditExtended.FInstance := TRichEditExtended.Create;
Result := TRichEditExtended.FInstance;
end;
{ TRichEditExList }
initialization
finalization
if Assigned(TRichEditExtended.FInstance) then
TRichEditExtended.FInstance.Free;
end.
To run this code you should create a new Application, add TRichEdit on the Form and type the following in a FormCreate method:
TRichEditExtended.ApplyRichEdit(ed1);
TRichEditExtended.AddDefaultLinkTextEvent(procedure (const T: String)begin showmessage(T); end);
TRichEditExtended.AddLinkTextWithDefaultEvent('Link');
ed1.Text := ed1.Text + '1231232 ';
TRichEditExtended.AddLinkTextWithDefaultEvent('Link2');
InsertLinkText() is replacing FRichEdit.Text with a completely new string when inserting a link with SelStart=-1, thus losing all previous text and formatting.
Use FRichEdit.GetTextLen() instead of FRichEdit.Lines.Text.Length to get the length of the existing text. And regardless of the input SelStart, always use the FRichEdit.SelStart|SelLength|SelText properties to add the new link into FRichEdit, preserving all existing text and formatting.
procedure TRichEditExtended.InsertLinkText(const LinkText: string; SelStart: integer = -1);
var
Fmt: CHARFORMAT2;
//Range: CHARRANGE;
begin
if SelStart = -1 then SelStart := FRichEdit.GetTextLen;
FRichEdit.SelStart := SelStart;
FRichEdit.SelLength := 0;
{ or:
Range.cpMin := SelStart;
Range.cpMax := SelStart;
SendMessage(FRichEdit.Handle, EM_EXSETSEL, 0, LPARAM(#Range));
}
FRichEdit.SelText := LinkText;
FRichEdit.SelStart := SelStart;
FRichEdit.SelLength := Length(LinkText);
{ or:
Range.cpMin := SelStart;
Range.cpMax := SelStart + Length(LinkText);
SendMessage(FRichEdit.Handle, EM_EXSETSEL, 0, LPARAM(#Range));
}
FillChar(Fmt, SizeOf(Fmt), 0);
Fmt.cbSize := SizeOf(Fmt);
Fmt.dwMask := CFM_LINK;
Fmt.dwEffects := CFE_LINK;
SendMessage(FRichEdit.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(#Fmt));
FRichEdit.SelStart := SelStart + Length(LinkText);
FRichEdit.SelLength := 0;
{ or:
Range.cpMin := SelStart + Length(LinkText);
Range.cpMax := Range.cpMax;
SendMessage(FRichEdit.Handle, EM_EXSETSEL, 0, LPARAM(#Range));
}
end;
As suggested by #KenWhite I will post my reseach upon the topic with edits about correct text highlighting due to issue of CRLF symbols in the text. As I found out line breaks are counted as one character for SelStart and GetTextLen, so you need to find the line where you are going to place a text and count all breaks before it and substract it from the desired SelStart position. For that purpose a function GetReilableSelStart is.
unit uRichEditExtended;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, RichEdit, WinApi.ShellApi, Vcl.Controls, Vcl.ComCtrls,
Generics.Collections, Vcl.Graphics;
type
TZ_RichEditClickEvent = reference to procedure(const ALinkText: string; LinkClickAccepted: boolean; var OutData: string);
TZ_RichEditLink = class
IsDefaultEvent: boolean;
Text: string;
OnLinkClickEvent: TZ_RichEditClickEvent;
end;
TZ_RichEditLinks = TList<TZ_RichEditLink>;
TZ_RichEditInsertOptions = set of (rioAppendBeforeCRLF);
TRichEditExtended = class
protected
class var FInstance: TRichEditExtended;
procedure SetRichEditMasks;
procedure RichEditWndProc(var Message: TMessage);
function GetReilableSelStart(SelStart: integer; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]): integer;
private
FLastPressedLinkText: string;
FLinkClickAccepted: boolean;
FPrevRichEditWndProc: TWndMethod;
FRichEdit: TRichEdit;
FRichEditLinks: TZ_RichEditLinks;
function InsertText(const AText: string; const SelStart: integer = -1; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]; Font: TFont = nil;
IsLink: boolean = false): integer;
procedure InsertLinkText(const LinkText: string; SelStart: integer = -1; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]);
procedure AddText(const AText: string; AddCRLF: boolean; Font: TFont = nil);
procedure AddFmtText(const AText: string; FontSizeDelta: integer; FontStyle: TFontStyles; FontColor: TColor; AddCRLF: boolean);
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
public
class function This: TRichEditExtended;
class procedure ApplyRichEdit(ARichEdit: TRichEdit);
class procedure AppendText(AText: string);
class procedure AppendTextLine(AText: string);
class procedure AppendFmtText(AText: string; FontSizeDelta: integer; FontStyle: TFontStyles; FontColor: TColor);
class procedure AppendFmtTextLine(AText: string; FontSizeDelta: integer; FontStyle: TFontStyles; FontColor: TColor);
class function AppendLinkText(AText: string; AOnLinkClickEvent: TZ_RichEditClickEvent; SelStart: integer = -1; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]): integer;
class function AppendLinkTextWithDefaultEvent(AText: string; SelStart: integer = -1; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]): integer;
class procedure AddDefaultLinkClickEvent(AOnLinkClickEvent: TZ_RichEditClickEvent);
class function LastLinkText: string;
class procedure PerformDefaultLinkClickEvent(const LinkText: string; CanOpen: boolean; var FullFilePath: string);
class procedure SetDefaultLinkClickReaction(ALinkClickAccepted: boolean);
end;
implementation
{ TRichEditExtended }
uses StrUtils, Math;
class procedure TRichEditExtended.AddDefaultLinkClickEvent(AOnLinkClickEvent: TZ_RichEditClickEvent);
var
REL: TZ_RichEditLink;
begin
if (This.FRichEditLinks.Count > 0) and This.FRichEditLinks[0].IsDefaultEvent then
This.FRichEditLinks[0].OnLinkClickEvent := AOnLinkClickEvent
else
begin
REL := TZ_RichEditLink.Create;
REL.IsDefaultEvent := true;
REL.Text := '';
REL.OnLinkClickEvent := AOnLinkClickEvent;
This.FRichEditLinks.Insert(0, REL);
REL := nil;
end;
end;
procedure TRichEditExtended.AddFmtText(const AText: string; FontSizeDelta: integer; FontStyle: TFontStyles; FontColor: TColor; AddCRLF: boolean);
var Font: TFont;
begin
try
Font := TFont.Create;
Font.Size := This.FRichEdit.Font.Size + FontSizeDelta;
Font.Style := FontStyle;
Font.Color := FontColor;
Font.Name := This.FRichEdit.Font.Name;
This.AddText(AText, AddCRLF, Font);
finally
FreeAndNil(Font);
end;
end;
class function TRichEditExtended.AppendLinkText(AText: string; AOnLinkClickEvent: TZ_RichEditClickEvent; SelStart: integer = -1;
InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]): integer;
var REL: TZ_RichEditLink;
begin
REL := TZ_RichEditLink.Create;
REL.IsDefaultEvent := false;
REL.Text := AText;
REL.OnLinkClickEvent := AOnLinkClickEvent;
Result := This.FRichEditLinks.Add(REL);
This.InsertLinkText(AText, SelStart, InsertOptions);
REL := nil;
end;
class function TRichEditExtended.AppendLinkTextWithDefaultEvent(AText: string; SelStart: integer = -1; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]): integer;
begin
This.AppendLinkText(AText, nil, SelStart, InsertOptions);
end;
procedure TRichEditExtended.AfterConstruction;
begin
inherited;
FRichEdit := nil;
FRichEditLinks := TZ_RichEditLinks.Create;
FLinkClickAccepted := false;
end;
procedure TRichEditExtended.AddText(const AText: string; AddCRLF: boolean; Font: TFont);
begin
if AddCRLF then
InsertText(Format('%s'#13#10,[AText]), -1, [rioAppendBeforeCRLF], Font)
else
InsertText(AText, -1, [rioAppendBeforeCRLF], Font);
end;
class procedure TRichEditExtended.AppendFmtText(AText: string; FontSizeDelta: integer; FontStyle: TFontStyles; FontColor: TColor);
begin
This.AddFmtText(AText, FontSizeDelta, FontStyle, FontColor, false);
end;
class procedure TRichEditExtended.AppendFmtTextLine(AText: string; FontSizeDelta: integer; FontStyle: TFontStyles; FontColor: TColor);
begin
This.AddFmtText(AText, FontSizeDelta, FontStyle, FontColor, true);
end;
class procedure TRichEditExtended.AppendText(AText: string);
begin
This.AddText(AText, false);
end;
class procedure TRichEditExtended.AppendTextLine(AText: string);
begin
This.AddText(AText, true);
end;
class procedure TRichEditExtended.ApplyRichEdit(ARichEdit: TRichEdit);
begin
This.FRichEdit := ARichEdit;
This.FPrevRichEditWndProc := This.FRichEdit.WindowProc;
This.FRichEdit.WindowProc := This.RichEditWndProc;
This.FRichEditLinks.Clear;
This.SetRichEditMasks;
end;
procedure TRichEditExtended.BeforeDestruction;
begin
if Assigned(FRichEdit) then
FRichEdit.WindowProc := FPrevRichEditWndProc;
FRichEdit := nil;
FRichEditLinks.Clear;
FRichEditLinks.Free;
inherited;
end;
function TRichEditExtended.GetReilableSelStart(SelStart: integer; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]): integer;
var
LineNo, LinesCount: integer;
begin
LinesCount := FRichEdit.Lines.Count;
if SelStart = -1 then
begin
Result := Max(FRichEdit.GetTextLen - Max((LinesCount - ord(not String(FRichEdit.Text).EndsWith(#$D#$A))),0), 0);
end
else
begin
LineNo := FRichEdit.Perform(EM_LINEFROMCHAR, SelStart, 0);
Result := Max(SelStart - (Max(LineNo - ord(rioAppendBeforeCRLF in InsertOptions) * ord(FRichEdit.Lines[LineNo].EndsWith(#$D#$A)),0)), 0);
end;
end;
procedure TRichEditExtended.InsertLinkText(const LinkText: string; SelStart: integer = -1; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF]);
var
Fmt: CHARFORMAT2;
begin
SelStart := InsertText(LinkText, SelStart, InsertOptions, nil, true);
FillChar(Fmt, SizeOf(Fmt), 0);
Fmt.cbSize := SizeOf(Fmt);
Fmt.dwMask := CFM_LINK;
Fmt.dwEffects := CFE_LINK;
SendMessage(FRichEdit.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(#Fmt));
FRichEdit.SelStart := FRichEdit.GetTextLen;
FRichEdit.SelLength := 0;
end;
function TRichEditExtended.InsertText(const AText: string; const SelStart: integer = -1; InsertOptions: TZ_RichEditInsertOptions = [rioAppendBeforeCRLF];
Font: TFont = nil; IsLink: boolean = false): integer;
var
Fmt: CHARFORMAT2;
begin
Result := GetReilableSelStart(SelStart, InsertOptions);
FRichEdit.SelStart := Result;
FRichEdit.SelText := Format('%s%s',[AText, DupeString(#32,ord(IsLink))]);
FRichEdit.SelStart := Result;
FRichEdit.SelLength := Length(AText);
FRichEdit.SelAttributes.Color := FRichEdit.DefAttributes.Color;
FRichEdit.SelAttributes.Name := FRichEdit.DefAttributes.Name;
FRichEdit.SelAttributes.Size := FRichEdit.DefAttributes.Size;
FRichEdit.SelAttributes.Style := FRichEdit.DefAttributes.Style;
if Assigned(Font) then
begin
FRichEdit.SelAttributes.Color := Font.Color;
FRichEdit.SelAttributes.Name := Font.Name;
FRichEdit.SelAttributes.Size := Font.Size;
FRichEdit.SelAttributes.Style := Font.Style;
FRichEdit.SelStart := FRichEdit.GetTextLen;
FRichEdit.SelLength := 0;
end;
end;
class function TRichEditExtended.LastLinkText: string;
begin
Result := This.FLastPressedLinkText;
end;
class procedure TRichEditExtended.PerformDefaultLinkClickEvent(const LinkText: string; CanOpen: boolean; var FullFilePath: string);
begin
if (This.FRichEditLinks.Count = 0) or not This.FRichEditLinks[0].IsDefaultEvent then
raise Exception.Create('A default action should be set.');
This.FRichEditLinks[0].OnLinkClickEvent(LinkText, CanOpen, FullFilePath);
end;
procedure TRichEditExtended.RichEditWndProc(var Message: TMessage);
type
PENLINK = ^ENLINK;
var
tr: TEXTRANGE;
p: PENLINK;
i: integer;
OutDat: string;
function GetLinkText: string;
begin
SetLength(Result, p.chrg.cpMax - p.chrg.cpMin);
tr.chrg := p.chrg;
tr.lpstrText := PChar(Result);
SendMessage(FRichEdit.Handle, EM_GETTEXTRANGE, 0, LPARAM(#tr));
end;
begin
FPrevRichEditWndProc(Message);
case Message.Msg of
CN_NOTIFY: begin
if TWMNotify(Message).NMHdr.code = EN_LINK then
begin
P := PENLINK(Message.LParam);
case p.msg of
WM_LBUTTONDOWN:
begin
FLastPressedLinkText := GetLinkText;
for I := 0 to FRichEditLinks.Count - 1 do
if FLastPressedLinkText.ToUpper.Equals(FRichEditLinks[I].Text.ToUpper) then
begin
if not Assigned(FRichEditLinks[I].OnLinkClickEvent) then
begin
if not FRichEditLinks[0].IsDefaultEvent then
raise Exception.Create('A default action should be set.')
else
FRichEditLinks[0].OnLinkClickEvent(FLastPressedLinkText, FLinkClickAccepted, OutData)
end
else
FRichEditLinks[I].OnLinkClickEvent(FLastPressedLinkText, FLinkClickAccepted, OutData);
exit;
end;
end;
WM_RBUTTONDOWN:
begin
FLastPressedLinkText := GetLinkText;
if Assigned(FRichEdit.PopupMenu) then
begin
FRichEdit.PopupMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;
end;
end;
end;
end;
CM_RECREATEWND: begin
SetRichEditMasks;
end;
end;
end;
class procedure TRichEditExtended.SetDefaultLinkClickReaction(ALinkClickAccepted: boolean);
begin
This.FLinkClickAccepted := ALinkClickAccepted;
end;
procedure TRichEditExtended.SetRichEditMasks;
var
Mask: DWORD;
begin
Mask := SendMessage(FRichEdit.Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(FRichEdit.Handle, EM_SETEVENTMASK, 0, Mask or ENM_LINK);
SendMessage(FRichEdit.Handle, EM_AUTOURLDETECT, 1, 0);
end;
class function TRichEditExtended.This: TRichEditExtended;
begin
if not Assigned(TRichEditExtended.FInstance) then
TRichEditExtended.FInstance := TRichEditExtended.Create;
Result := TRichEditExtended.FInstance;
end;
{ TRichEditExList }
initialization
finalization
if Assigned(TRichEditExtended.FInstance) then
TRichEditExtended.FInstance.Free;
end.
Any suggestions are appreciated.
How to use:
TRichEditExtended.ApplyRichEdit(edMessages);
TRichEditExtended.SetDefaultLinkClickReaction(true); //Link click accepted by default or not
TRichEditExtended.AddDefaultLinkClickEvent(
procedure (const LinkText: string; LinkClickAccepted: boolean; var OutData: string)
begin
if LinkClickAccepted then
DoSomething;
DoAnythingTo(OutData); // if you call somewhere after TRichEditExtended.PerformDefaultLinkClickEvent then you get the OutData there
end
);

How to disable CTRL ALT key on windows 7?

I know the only way to do that is to remap keys with regedit.
Have someone has done that with delphi ? (disable it and enable it again)
http://www.northcode.com/blog.php/2007/07/25/Securing-Windows-For-Use-As-A-Kiosk
The information in the article would translate to Delphi as follows:
uses
Registry;
const
DisableScancodes: packed array[0..11] of DWORD = (
$00000000, // version = 0
$00000000, // flags = 0
$00000009, // # of mappings = 9
$E05B0000, // disable Windows key
$E05C0000, // disable Windows key
$E05D0000, // disable Windows menu key
$00440000, // disable F10 key
$001D0000, // disable Left Ctrl key
$00380000, // disable Left Alt key
$E01D0000, // disable Right Ctrl key
$E0380000, // disable Right Alt key
$00000000 // end of list
);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create(KEY_WRITE);
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('\SYSTEM\CurrentControlSet\Control\Keyboard Layout', True) then
begin
try
// to enable the mapping
Reg.WriteBinaryData('Scancode Map', DisableScancodes, SizeOf(DisableScancodes));
// to disable the mapping
Reg.DeleteValue('Scancode Map');
finally
Reg.CloseKey;
end;
end;
finally
Reg.Free;
end;
end;
If you need to be more dynamic about which scancodes you enable/disable, you will have to use TRegistry.ReadBinaryData() to read the current Scancode Map value (if it exists), modify it as needed, and then save the changes using TRegistry.WriteBinaryData(). Try something like this:
unit ScanCodeMap;
interface
type
TMappedScancode = record
Scancode: WORD;
MappedTo: WORD;
end;
procedure AddScancodeMappings(const Values: array of TMappedScancode);
procedure AddScancodeMapping(const Value: TMappedScancode);
procedure AddScancodeMapping(Scancode, MappedTo: WORD);
procedure RemoveScancodeMappings(const Scancodes: array of WORD);
procedure RemoveScancodeMapping(Scancode: WORD);
procedure DisableScancodes(Scancodes: array of WORD);
procedure DisableScancode(Scancode: WORD);
implementation
uses
Windows, Registry;
type
PScancodeMapHdr = ^TScancodeMapHdr;
TScancodeMapHdr = packed record
Version: DWORD;
Flags: DWORD;
NumMappings: DWORD;
end;
TScancodeMap = record
Version: DWORD;
Flags: DWORD;
Mappings: array of TMappedScancode;
end;
procedure AddScancodesToMap(var Map: TScancodeMap; const Values: array of TMappedScancode);
var
I, J, Idx: Integer;
begin
for I := 0 to High(Values) do
begin
Idx := -1;
for J := 0 to High(Map.Mappings) do
begin
if Map.Mappings[J].Scancode = Values[I].Scancode then
begin
Idx := J;
Break;
end;
end;
if Idx = -1 then
begin
SetLength(Map.Mappings, Length(Map.Mappings)+1);
Idx := High(Map.Mappings);
end;
Map.Mappings[Idx].MappedTo := Values[I].MappedTo;
end;
end;
procedure RemoveScancodesFromMap(var Map: TScancodeMap; const Scancodes: array of WORD);
var
I, J: Integer;
begin
for I := 0 to High(Scancodes) do
begin
for J := 0 to High(Map.Mappings) do
begin
if Map.Mappings[J].Scancode = Scancodes[I] then
begin
if J < High(Map.Mappings) then
Move(Map.Mappings[J+1], Map.Mappings[J], (High(Mappings)-J) * SizeOf(TMappedScancode));
SetLength(Map.Mappings, Length(Map.Mappings)-1);
Break;
end;
end;
end;
end;
procedure WriteScanCodeMap(const Map: TScancodeMap);
var
Reg: TRegistry;
Data: array of Byte;
Tmp: PByte;
Hdr: PScancodeMapHdr;
I: Integer;
begin
if Length(Map.Mappings) > 0 then
begin
SetLength(Data, sizeof(TScancodeMapHdr) + (Length(Map.Mappings) + 1) * SizeOf(DWORD));
Tmp := PByte(Data);
Hdr := PScancodeMapHdr(Tmp);
Hdr.Version := Map.Version;
Hdr.Flags := Map.Flags;
Hdr.NumMappings := Length(Map.Mappings) + 1;
Inc(Tmp, SizeOf(TScancodeMapHdr));
for I := 0 to High(Map.Mappings) do
begin
PDWORD(Tmp)^ := (DWORD(Map.Mappings[0].Scancode) shr 16) or DWORD(Map.Mappings[0].MappedTo);
Inc(Tmp, SizeOf(DWORD));
end;
PDWORD(Tmp)^ := 0;
end;
Reg := TRegistry.Create(KEY_WRITE);
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('\SYSTEM\CurrentControlSet\Control\Keyboard Layout', True) then
begin
try
if Length(Data) > 0 then
Reg.WriteBinaryData('Scancode Map', Data[0], Length(Data))
else
Reg.DeleteValue('Scancode Map');
finally
Reg.CloseKey;
end;
end;
finally
Reg.Free;
end;
end;
procedure ReadScanCodeMap(var Map: TScancodeMap);
var
Reg: TRegistry;
Size: Integer;
Data: array of Byte;
Tmp: PByte;
Hdr: PScancodeMapHdr;
I: Integer;
begin
Map.Version := 0;
Map.Flags := 0;
SetLength(Map.Mappings, 0);
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('\SYSTEM\CurrentControlSet\Control\Keyboard Layout') then
begin
try
Size := Reg.GetDataSize('Scancode Map');
if Size > SizeOf(TScancodeMapHdr) then
begin
SetLength(Data, Size);
Reg.ReadBinaryData('Scancode Map', Data[0], Size);
Tmp := PByte(Data);
Hdr := PScancodeMapHdr(Tmp);
Map.Version := Hdr.Version;
Map.Flags := Hdr.Flags;
Inc(Tmp, SizeOf(TScancodeMapHdr));
if Hdr.NumMappings > 1 then
begin
SetLength(Map.Mappings, Hdr.NumMappings-1);
for I := 0 to High(Map.Mappings) do
begin
Map.Mappings[I].Scancode := HIWORD(PDWORD(Tmp)^);
Map.Mappings[I].MappedTo := LOWORD(PDWORD(Tmp)^);
end;
end;
end;
finally
Reg.CloseKey;
end;
end;
finally
Reg.Free;
end;
end;
procedure AddScancodeMappings(const Values: array of TMappedScancode);
var
Map: TScancodeMap;
begin
ReadScanCodeMap(Map);
AddScancodesToMap(Map, Values);
WriteScanCodeMap(Map);
end;
procedure AddScancodeMapping(const Value: TMappedScancode);
begin
AddScancodeMappings([Value]);
end;
procedure AddScancodeMapping(Scancode, MappedTo: WORD);
var
Value: array[0..0] of TMappedScancode;
begin
Value[0].Scancode := Scancode;
Value[0].MappedTo := MappedTo;
AddScancodeMappings([Value]);
end;
procedure RemoveScancodeMappings(const Scancodes: array of WORD);
var
Map: TScancodeMap;
begin
ReadScanCodeMap(Map);
RemoveScancodesFromMap(Map, Scancodes);
WriteScanCodeMap(Map);
end;
procedure RemoveScancodeMapping(Scancode: WORD);
begin
RemoveScancodeMappings([Scancode]);
end;
procedure DisableScancodes(Scancodes: array of WORD);
var
Values: array of TMappedScancode;
I: Integer;
begin
SetLength(Values, Length(Scancodes));
for I := 0 to High(Mappings) do
begin
Values[I].Scancode := Scancodes[I];
Values[I].MappedTo := $0000;
end;
AddScancodeMappings(Values);
end;
procedure DisableScancode(Scancode: WORD);
begin
AddScancodeMapping(Scancode, $0000);
end;
end.
Then you can do this:
uses
ScanCodeMap;
const
Scancodes: packed array[0..7] of WORD = (
$E05B, // Windows key
$E05C, // Windows key
$E05D, // Windows menu key
$0044, // F10 key
$001D, // Left Ctrl key
$0038, // Left Alt key
$E01D, // Right Ctrl key
$E038 // Right Alt key
);
procedure DisableCtrlAltDel;
begin
DisableScancodes(Scancodes);
end;
procedure EnableCtrlAltDel;
begin
RemoveScancodeMappings(Scancodes);
end;

Delphi XE3 Invalid Pointer when trying to free FSQL (TStringList)

I'm creating a new app in XE3 but using some units created in D2007.
I"m getting an error when freeing a TStringList data item. Here's the code that creates the data item FSQL:
procedure TPayorDM.DataModuleCreate(Sender: TObject);
begin
FPayorDRM := TDRM.Create;
FSQL := TStringList.Create;
end;
Here's the code that is getting the error:
procedure TPayorDM.DataModuleDestroy(Sender: TObject);
begin
FreeAndNil(FSQL);
if T_Payor.Active then T_Payor.Close;
FreeAndNil(FPayorDRM);
end;
The error occurs on 'FreeAndNil(FSQL);'. I have tried 'FSQL.Free' and I get the same result.
Here's the error I'm getting:
Project: PayorUpdate.exe raised exception class EInvalidPointer with message 'Invalid pointer operation.
When I break blue arrows (debug mode) is pointing to _FreeMem(Pointer(Self)); in the procedure TObject.FreeInstance in System unit as follows:
procedure TObject.FreeInstance;
begin
CleanupInstance;
_FreeMem(Pointer(Self));
end;
If I don't free the TStringList data item I would have a memory leak in the app.
Is there possibly a configuration option I need to set? I've search with google and have not found anything that explains what I am doing wrong other than one of the three possibilities:
It was allocated by some other memory manager.
It had already been freed once before.
It had never been allocated by anything.
If I put a try ... except... I'm able to get around the issue, but I prefer not to do this.
BTW, I have another TStringList in different unit and I create and FreeAndNil and I do not get any error.
Here is entire source:
unit PayorDataMgr;
interface
uses
SysUtils,
Classes,
Dialogs,
NativeXML,
adscnnct,
DB,
adsdata,
adsfunc,
adstable,
ace,
cbs.drm,
cbs.utils,
cbs.LogFiles;
const
POLICY_TYPES: array[1..3] of string = ('Primary','Secondary','Tertiary');
type
TPayorRecord = Record
ASSIGNBENEFITS: Boolean;
AUTHORIZE: Boolean;
BATCHBILL: Boolean;
CLAIMMAX: Integer;
DISCONTINUED: TDateTime;
DISPENSEUPDATE: Boolean;
EHRSIGNOFF: Boolean;
EMCDEST: String;
FORM: String;
GOVASSIGN: Boolean;
HIDE: Boolean;
IGRPUNIQUE: Integer;
LEGACYPLAN: String;
LEGACYTYPE: String;
LOCALATTN: String;
LOCALCITY: String;
LOCALNAME: String;
LOCALPHONE: String;
LOCALSTATE: String;
LOCALSTREET: String;
LOCALZIP: String;
MASTERATTN: String;
MASTERCITY: String;
MASTERNAME: String;
MASTERPHONE: String;
MASTERSTATE: String;
MASTERSTREET: String;
MASTERZIP: String;
MEDIGAPCODE: String;
MEDIGAPPAYOR: Boolean;
MEDPLANGUID: String;
MODIFIED: TDateTime;
NEICCODE: String;
NEICTYPESTDC: Integer;
OWNER: String;
PAYORGUID: String;
PAYORSUBTYPESTDC: Integer;
PAYORTYPESTDC: Integer;
PAYORUNIQUE: Integer;
PAYPERCENT: Integer;
RTCODE: String;
SRXPLANGUID: String;
STATEFILTER: String;
procedure Clear;
End;
TPayors = Record
private
function _pGetCount: Integer;
public
Items: Array of TPayorRecord;
procedure Add(const aItem:TPayorRecord);
function CarriersList:TStrings;
procedure Free;
function GetPayorGuid(const aPAYORUNIQUE:Integer):String;
function IndexOfIgrpUnique(Const aIGRPUNIQUE:Integer):Integer;
function IndexOfPayorUnique(Const aPAYORUNIQUE:Integer):Integer;
procedure SortByName;
property Count:Integer Read _pGetCount;
End;
TPayorDM = class(TDataModule)
CommonConnection: TAdsConnection;
T_Payor: TAdsTable;
Q_Payor: TAdsQuery;
procedure DataModuleDestroy(Sender: TObject);
procedure DataModuleCreate(Sender: TObject);
private
FPayorDRM: TDRM;
FSQL: TStringList;
function _LoadRecordFromTable:TPayorRecord;
function _newIDSTRING(const aFormat:String='F'):String;
{ Private declarations }
procedure _pSetConnectionHandle(const Value: Integer);
procedure _pSetErrorMessage(const Value: String);
procedure _psetSQL(const Value: TStringList);
{ Private properties }
property ErrorMessage:String Write _pSetErrorMessage;
public
function AddPayor(var aPAYORRECORD:TPAYORRECORD):Boolean;
function ExecuteScript(const aTo,aFrom:string):Boolean;
function FindPayor(const aPAYORGUID:String):Boolean;overload;
function FindPayor(const aPAYORUNIQUE:Integer):Boolean;overload;
function GetPayorData:TDRM;
function GetRecordCount(const aData:String):Integer;
function LoadCarriers(const aHide:boolean = False):TPayors;
function LoadPayor:TPayorRecord;
function OpenTable:Boolean;
function UpdateFromXML(const aPayorNode:TXMLNode):boolean;
{ Public declarations }
property ConnectionHandle:Integer Write _pSetConnectionHandle;
property DynamicPayorFields:TDRM Read FPayorDRM;
property SQL:TStringList Read FSQL Write _psetSQL;
end;
var
PayorDM: TPayorDM;
implementation
{$R *.dfm}
function TPayorDM.AddPayor(var aPAYORRECORD: TPAYORRECORD): Boolean;
begin
Result := False;
if IsNull(aPAYORRECORD.LOCALNAME) then Exit;
{ Create uniques }
{ Add Record }
if not T_Payor.Active then
if not OpenTable then Exit;
with T_Payor do
try
Insert;
FieldByName('PAYORGUID').AsString := _newIDSTRING;
FieldByName('MASTERNAME').AsString := aPAYORRECORD.MASTERNAME;
FieldByName('MASTERSTREET').AsString := aPAYORRECORD.MASTERSTREET;
FieldByName('MASTERCITY').AsString := aPAYORRECORD.MASTERCITY;
FieldByName('MASTERSTATE').AsString := aPAYORRECORD.MASTERSTATE;
FieldByName('PAYORTYPESTDC').AsInteger := aPAYORRECORD.PAYORTYPESTDC;
FieldByName('MASTERZIP').AsString := aPAYORRECORD.MASTERZIP;
FieldByName('MASTERATTN').AsString := aPAYORRECORD.MASTERATTN;
FieldByName('MASTERPHONE').AsString := aPAYORRECORD.MASTERPHONE;
FieldByName('NEICCODE').AsString := aPAYORRECORD.NEICCODE;
FieldByName('RTCODE').AsString := aPAYORRECORD.RTCODE;
FieldByName('STATEFILTER').AsString := aPAYORRECORD.STATEFILTER;
FieldByName('NEICTYPESTDC').AsInteger := aPAYORRECORD.NEICTYPESTDC;
FieldByName('PAYORSUBTYPESTDC').AsInteger := aPAYORRECORD.PAYORSUBTYPESTDC;
FieldByName('OWNER').AsString := aPAYORRECORD.OWNER;
FieldByName('HIDE').AsBoolean := aPAYORRECORD.HIDE;
FieldByName('IGRPUNIQUE').AsInteger := aPAYORRECORD.IGRPUNIQUE;
FieldByName('FORM').AsString := aPAYORRECORD.FORM;
FieldByName('GOVASSIGN').AsBoolean := aPAYORRECORD.GOVASSIGN;
FieldByName('CLAIMMAX').AsInteger := aPAYORRECORD.CLAIMMAX;
FieldByName('MEDIGAPCODE').AsString := aPAYORRECORD.MEDIGAPCODE;
FieldByName('EMCDEST').AsString := aPAYORRECORD.EMCDEST;
FieldByName('ASSIGNBENEFITS').AsBoolean := aPAYORRECORD.ASSIGNBENEFITS;
FieldByName('BATCHBILL').AsBoolean := aPAYORRECORD.BATCHBILL;
FieldByName('MEDIGAPPAYOR').AsBoolean := aPAYORRECORD.MEDIGAPPAYOR;
FieldByName('MEDPLANGUID').AsString := aPAYORRECORD.MEDPLANGUID;
FieldByName('SRXPLANGUID').AsString := aPAYORRECORD.SRXPLANGUID;
FieldByName('PAYPERCENT').AsInteger := aPAYORRECORD.PAYPERCENT;
FieldByName('LOCALNAME').AsString := aPAYORRECORD.LOCALNAME;
FieldByName('LOCALSTREET').AsString := aPAYORRECORD.LOCALSTREET;
FieldByName('LOCALCITY').AsString := aPAYORRECORD.LOCALCITY;
FieldByName('LOCALSTATE').AsString := aPAYORRECORD.LOCALSTATE;
FieldByName('LOCALZIP').AsString := aPAYORRECORD.LOCALZIP;
FieldByName('LOCALATTN').AsString := aPAYORRECORD.LOCALATTN;
FieldByName('LOCALPHONE').AsString := aPAYORRECORD.LOCALPHONE;
FieldByName('EHRSIGNOFF').AsBoolean := aPAYORRECORD.EHRSIGNOFF;
FieldByName('DISCONTINUED').AsDateTime := aPAYORRECORD.DISCONTINUED;
FieldByName('MODIFIED').AsDateTime := Now;
FieldByName('LEGACYPLAN').AsString := aPAYORRECORD.LEGACYPLAN;
FieldByName('LEGACYTYPE').AsString := aPAYORRECORD.LEGACYTYPE;
FieldByName('AUTHORIZE').AsBoolean := aPAYORRECORD.AUTHORIZE;
FieldByName('DISPENSEUPDATE').AsBoolean := aPAYORRECORD.DISPENSEUPDATE;
Post;
aPAYORRECORD.PAYORUNIQUE := FieldByName('PAYORUNIQUE').AsInteger;
aPAYORRECORD.PAYORGUID := FieldByName('PAYORGUID').AsString;
Close;
Result := True;
except on E: EADSDatabaseError do
begin
ErrorMessage := 'AddPayor: ERRORCODE: ' + IntToStr(e.ACEErrorCode) +
' ERROR: ' + e.Message;
end;
end;
end;
procedure TPayorDM.DataModuleCreate(Sender: TObject);
begin
FPayorDRM := TDRM.Create;
FSQL := TStringList.Create; { FSQL Created }
end;
procedure TPayorDM.DataModuleDestroy(Sender: TObject);
begin
try
FSQL.Free; { FSQL destroyed - work around to get unit to run without error}
except
end;
if T_Payor.Active then T_Payor.Close;
FreeAndNil(FPayorDRM);
end;
function TPayorDM.ExecuteScript(const aTo, aFrom: string):Boolean;
begin
Result := False;
if FSQL.Count = 0 then exit;
with Q_Payor do
try
if Active then Close;
SQL := FSQL;
ParamByName('to').Text := aTo;
ParambyName('from').Text := aFrom;
ExecSQL;
if Active then Close;
Result := True;
except on E: EADSDatabaseError do
begin
ErrorMessage := 'ExecuteScript: ERRORCODE: ' + IntToStr(e.ACEErrorCode) +
' ERROR: ' + e.Message + ' SQL: ' + Q_Payor.SQL.Text;
end;
end;
end;
function TPayorDM.FindPayor(const aPAYORUNIQUE: Integer): Boolean;
begin
T_Payor.IndexName := 'PAYORUNIQUE';
Result := T_Payor.FindKey([aPAYORUNIQUE]);
end;
function TPayorDM.FindPayor(const aPAYORGUID: String): Boolean;
begin
T_Payor.IndexName := 'PAYORGUID';
Result := T_Payor.FindKey([aPAYORGUID]);
end;
function TPayorDM.GetPayorData: TDRM;
begin
if FPayorDRM.Count = 0 then
FPayorDRM.BuildDRMList(T_Payor);
Result := FPayorDRM;
end;
function TPayorDM.GetRecordCount(const aData:string): Integer;
begin
Result := 0;
if FSQL.Count = 0 then exit;
with Q_Payor do
try
if Active then Close;
SQL := FSQL;
ParamByName('data').AsString := aData;
Open;
Result := RecordCount;
Close;
except on E: EADSDatabaseError do
begin
ErrorMessage := 'GetRecordCount: ERRORCODE: ' + IntToStr(e.ACEErrorCode) +
' ERROR: ' + e.Message;
end;
end;
end;
function TPayorDM.LoadCarriers(const aHide: boolean): TPayors;
begin
OpenTable;
Result.Free;
with T_Payor do
begin
First;
while not EOF do
begin
if T_Payor.FieldByName('HIDE').AsBoolean = aHide then
Result.Add(_LoadRecordFromTable);
Next;
end;
First;
Result.SortByName;
end;
end;
function TPayorDM.LoadPayor: TPayorRecord;
begin
Result.Clear;
try
if not T_Payor.active then exit;
if T_Payor.RecNo = 0 then exit;
Result := _LoadRecordFromTable;
except on E: EADSDatabaseError do
begin
ErrorMessage := 'LoadPayor: ERRORCODE: ' + IntToStr(e.ACEErrorCode) +
' ERROR: ' + e.Message;
end;
end;
end;
function TPayorDM.OpenTable: Boolean;
begin
Result := False;
with T_Payor do
try
if not Active then Open;
FPayorDRM.BuildDRMList(T_Payor);
FPayorDRM.LoadValues(T_Payor); { test }
FPayorDRM.ExportDRMList; { test }
Result := True;
except on E: EADSDatabaseError do
begin
ErrorMessage := 'OpenTable: ERRORCODE: ' + IntToStr(e.ACEErrorCode) +
' ERROR: ' + e.Message;
end;
end;
end;
function TPayorDM.UpdateFromXML(const aPayorNode: TXMLNode): boolean;
var
fKeyData:TXMLNode;
Idx,fPAYORUNIQUE:Integer;
begin
Result := False;
if not Assigned(aPayorNode) then Exit;
try
if FPayorDRM.Count = 0 then
FPayorDRM.BuildDRMList(T_Payor);
FPayorDRM.ClearValues;
fKeyData := aPayorNode.FindNode('KeyData');
FPayorDRM.FindRecordFromKeyData(fKeyData,T_Payor);
fPAYORUNIQUE := FPayorDRM.FieldByName('PAYORUNIQUE').AsInteger;
FPayorDRM.LoadValues(aPayorNode);
if fPAYORUNIQUE = 0 then
begin
FPayorDRM.FieldByName('PAYORUNIQUE').AsInteger := 0;
FPayorDRM.FieldByName('PAYORGUID').AsString := _newIDSTRING;
FPayorDRM.FieldByName('MODIFIED').AsDate := Now;
FPayorDRM.AddRecord(T_Payor)
end
else
begin
FPayorDRM.FieldByName('MODIFIED').AsDate := Now;
FPayorDRM.UpdateRecord(T_Payor);
end;
except on e:exception do
begin
ErrorMessage := 'UpdateFromXML: ERROR: ' + e.Message;
end;
end;
end;
function TPayorDM._LoadRecordFromTable: TPayorRecord;
begin
with T_Payor do
begin
Result.PAYORUNIQUE := FieldByName('PAYORUNIQUE').AsInteger;
Result.PAYORGUID := FieldByName('PAYORGUID').AsString;
Result.MASTERNAME := FieldByName('MASTERNAME').AsString;
Result.MASTERSTREET := FieldByName('MASTERSTREET').AsString;
Result.MASTERCITY := FieldByName('MASTERCITY').AsString;
Result.MASTERSTATE := FieldByName('MASTERSTATE').AsString;
Result.PAYORTYPESTDC := FieldByName('PAYORTYPESTDC').AsInteger;
Result.MASTERZIP := FieldByName('MASTERZIP').AsString;
Result.MASTERATTN := FieldByName('MASTERATTN').AsString;
Result.MASTERPHONE := FieldByName('MASTERPHONE').AsString;
Result.NEICCODE := FieldByName('NEICCODE').AsString;
Result.RTCODE := FieldByName('RTCODE').AsString;
Result.STATEFILTER := FieldByName('STATEFILTER').AsString;
Result.NEICTYPESTDC := FieldByName('NEICTYPESTDC').AsInteger;
Result.PAYORSUBTYPESTDC := FieldByName('PAYORSUBTYPESTDC').AsInteger;
Result.OWNER := FieldByName('OWNER').AsString;
Result.HIDE := FieldByName('HIDE').AsBoolean;
Result.IGRPUNIQUE := FieldByName('IGRPUNIQUE').AsInteger;
Result.FORM := FieldByName('FORM').AsString;
Result.GOVASSIGN := FieldByName('GOVASSIGN').AsBoolean;
Result.CLAIMMAX := FieldByName('CLAIMMAX').AsInteger;
Result.MEDIGAPCODE := FieldByName('MEDIGAPCODE').AsString;
Result.EMCDEST := FieldByName('EMCDEST').AsString;
Result.ASSIGNBENEFITS := FieldByName('ASSIGNBENEFITS').AsBoolean;
Result.BATCHBILL := FieldByName('BATCHBILL').AsBoolean;
Result.MEDIGAPPAYOR := FieldByName('MEDIGAPPAYOR').AsBoolean;
Result.MEDPLANGUID := FieldByName('MEDPLANGUID').AsString;
Result.SRXPLANGUID := FieldByName('SRXPLANGUID').AsString;
Result.PAYPERCENT := FieldByName('PAYPERCENT').AsInteger;
Result.LOCALNAME := FieldByName('LOCALNAME').AsString;
Result.LOCALSTREET := FieldByName('LOCALSTREET').AsString;
Result.LOCALCITY := FieldByName('LOCALCITY').AsString;
Result.LOCALSTATE := FieldByName('LOCALSTATE').AsString;
Result.LOCALZIP := FieldByName('LOCALZIP').AsString;
Result.LOCALATTN := FieldByName('LOCALATTN').AsString;
Result.LOCALPHONE := FieldByName('LOCALPHONE').AsString;
Result.EHRSIGNOFF := FieldByName('EHRSIGNOFF').AsBoolean;
Result.DISCONTINUED := FieldByName('DISCONTINUED').AsDateTime;
Result.MODIFIED := FieldByName('MODIFIED').AsDateTime;
Result.LEGACYPLAN := FieldByName('LEGACYPLAN').AsString;
Result.LEGACYTYPE := FieldByName('LEGACYTYPE').AsString;
Result.AUTHORIZE := FieldByName('AUTHORIZE').AsBoolean;
Result.DISPENSEUPDATE := FieldByName('DISPENSEUPDATE').AsBoolean;
end;
end;
function TPayorDM._newIDSTRING(const aFormat: String): String;
begin
Result := '';
try
with Q_Payor do
try
SQL.Clear;
SQL.Add('SELECT NEWIDSTRING( "' + aFormat + '" ) AS GUID FROM system.iota');
Open;
Result := FieldByName('GUID').AsString;
Close;
except on E: EADSDatabaseError do
begin
ErrorMessage := '_newIDSTRING: ERRORCODE: ' + IntToStr(e.ACEErrorCode) +
' ERROR: ' + e.Message;
end;
end;
finally
end;
end;
procedure TPayorDM._pSetConnectionHandle(const Value: Integer);
begin
if T_Payor.Active then T_Payor.Close;
CommonConnection.SetHandle(Value);
OpenTable;
end;
procedure TPayorDM._pSetErrorMessage(const Value: String);
begin
WriteError('[TPayorDM]' + Value,LogFilename);
end;
procedure TPayorDM._psetSQL(const Value: TStringList);
begin
FSQL := Value;
end;
{ TPayorRecord }
procedure TPayorRecord.Clear;
begin
PAYORUNIQUE := 0;
PAYORGUID := '';
MASTERNAME := '';
MASTERSTREET := '';
MASTERCITY := '';
MASTERSTATE := '';
PAYORTYPESTDC := 0;
MASTERZIP := '';
MASTERATTN := '';
MASTERPHONE := '';
NEICCODE := '';
RTCODE := '';
STATEFILTER := '';
NEICTYPESTDC := 0;
PAYORSUBTYPESTDC := 0;
OWNER := '';
HIDE := False;
IGRPUNIQUE := 0;
FORM := '';
GOVASSIGN := False;
CLAIMMAX := 0;
MEDIGAPCODE := '';
EMCDEST := '';
ASSIGNBENEFITS := False;
BATCHBILL := False;
MEDIGAPPAYOR := False;
MEDPLANGUID := '';
SRXPLANGUID := '';
PAYPERCENT := 0;
LOCALNAME := '';
LOCALSTREET := '';
LOCALCITY := '';
LOCALSTATE := '';
LOCALZIP := '';
LOCALATTN := '';
LOCALPHONE := '';
EHRSIGNOFF := False;
DISCONTINUED := 0;
MODIFIED := 0;
LEGACYPLAN := '';
LEGACYTYPE := '';
AUTHORIZE := False;
DISPENSEUPDATE := False;
end;
{ TPayors }
procedure TPayors.Add(const aItem: TPayorRecord);
begin
SetLength(Items,Count + 1);
Items[Count - 1] := aItem;
end;
function TPayors.CarriersList: TStrings;
var
I: Integer;
begin
Result := TStringList.Create;
Result.Clear;
SortbyName;
try
for I := 0 to Count - 1 do
Result.Add(Items[I].LOCALNAME);
finally
end;
end;
procedure TPayors.Free;
begin
Items := Nil;
end;
function TPayors.GetPayorGuid(const aPAYORUNIQUE: Integer): String;
var
Idx:Integer;
begin
Result := '';
Idx := IndexOfPayorUnique(aPAYORUNIQUE);
if not (Idx = -1) then
Result := Items[Idx].PAYORGUID;
end;
function TPayors.IndexOfIgrpUnique(const aIGRPUNIQUE: Integer): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to Count - 1 do
if Items[I].IGRPUNIQUE = aIGRPUNIQUE then
begin
Result := I;
Break;
end;
end;
function TPayors.IndexOfPayorUnique(const aPAYORUNIQUE: Integer): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to Count - 1 do
if Items[I].PAYORUNIQUE = aPAYORUNIQUE then
begin
Result := I;
Break;
end;
end;
procedure TPayors.SortByName;
var
fSort:TStringList;
fParse:TStrings;
I,Idx: Integer;
fTempPayor:TPayors;
begin
fSort := TStringList.Create;
fParse := TStringList.Create;
fTempPayor.Items := Self.Items;
fSort.Sorted := True;
try
for I := 0 to Count - 1 do
fSort.Add(Items[I].LOCALNAME + #9 + IntToStr(I));
Items := Nil;
for I := 0 to fSort.Count - 1 do
begin
cbs.utils.ParseDelimited(fParse,fSort[I],#9);
Idx := StrToInt(fParse[1]);
Add(fTempPayor.Items[Idx]);
end;
finally
fTempPayor.Free;
fParse.Free;
fSort.Free;
end;
end;
function TPayors._pGetCount: Integer;
begin
Result := Length(Items);
end;
end.
You are (most probably) double freeing a stringlist (and never freeing at least one). The problem is in the setter of your 'SQL' property (which is backed by the 'FSQL' field):
procedure TPayorDM._psetSQL(const Value: TStringList);
begin
FSQL := Value;
end;
Here you are loosing the reference to the already existing stringlist (LHS). Consider the below scenario:
You call
PayorDM.SQL := AStringList;
and the reference to the private field you created in the constructor is gone, instead you keep a reference to 'AStringList'. After that at some point you destroy 'AStringList', now the 'FSQL' field is a stale pointer. When in the destructor you call
FSQL.Free;
you get an invalid pointer operation.
Change your setter to:
procedure TPayorDM._psetSQL(const Value: TStringList);
begin
FSQL.Assign(Value);
end;

Delphi 7 - Check if server is online

I have a program created in Delphi 7 that uses ftp downloading.
How can i insert into that program to check for a server status?
For example if server is online to produce a green image, if server is offline o produce a red image. Here is the code.
unit Download;
interface
uses
Classes, Wininet, Windows, SysUtils, Dialogs, Forms;
type
GFilesThread = class(TThread)
private
LTemp : Longword;
STemp : string;
FilesToGet : TStringList;
FilesSize : Longword;
CBackup : integer;
CRevision : integer;
CForceCheck : boolean;
CSwitch : integer;
UUrl : string;
USelfParam : string;
Dir: string;
FSource: TStream;
protected
procedure Execute; override;
procedure UpdateFileProgress;
procedure SetFileProgressMax;
procedure UpdateStatusLabel;
procedure UpdateFileDecompStat;
procedure UpdateFilesProgress;
procedure CheckFiles(FList : TStringList);
procedure BZProgress(Sender: TObject);
procedure LockFMain;
procedure UNLockFMain;
procedure GetFiles;
procedure SelfUpdate(SelfVal : string);
procedure UpdateRevision;
procedure ModHosts(Lines : TStringList);
procedure DoUncompressStream(ASource, ADest: TStream);
procedure DoUncompress(const ASource, ADest: TFileName);
function HTTPGetFile(const fileURL, FileName: string; sh_progress: boolean): boolean;
public
property CreateBackup : integer write CBackup;
property UpdatesUrl : string write UUrl;
property LocalRevision : integer write CRevision;
property ForceCheck : boolean write CForceCheck;
end;
implementation
uses Launcher, CheckFiles, BZip2, md5, FileList;
// -------- by 667
procedure GFilesThread.UpdateStatusLabel;
begin
FMain.Label3.Caption:=STemp;
end;
procedure GFilesThread.SetFileProgressMax;
begin
if(CSwitch=0) then
FMain.Gauge1.MaxValue:=LTemp;
if(CSwitch=1) then
FMain.Gauge2.MaxValue:=LTemp;
end;
procedure GFilesThread.UpdateFileProgress;
begin
FMain.Gauge1.Progress:=LTemp;
end;
procedure GFilesThread.UpdateFilesProgress;
begin
FMain.Gauge2.Progress:=LTemp;
end;
procedure GFilesThread.UpdateRevision;
begin
FMain.UpdateRevision(IntToStr(CRevision));
end;
procedure GFilesThread.UpdateFileDecompStat;
begin
FMain.Gauge1.Progress:=LTemp;
end;
procedure GFilesThread.BZProgress(Sender: TObject);
begin
LTemp:=FSource.Position;
Synchronize(UpdateFileDecompStat);
end;
procedure GFilesThread.LockFMain;
begin
Fmain.ImgBtn1.Visible:=False;
Fmain.ImgBtn2.Visible:=False;
Fmain.ImgBtn5.Enabled:=False;
end;
procedure GFilesThread.UNLockFMain;
begin
Fmain.ImgBtn1.Visible:=True;
Fmain.ImgBtn2.Visible:=True;
Fmain.ImgBtn5.Enabled:=True;
end;
// --------- by 667
function GFilesThread.HTTPGetFile(const fileURL, FileName: string; sh_progress: boolean): boolean;
const
BufferSize = 1024;
var
hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of Byte;
BufferLen: Longword;
f: file;
sAppName: string;
begin
Result := False;
sAppName := 'L2ClientUpdater';
LTemp:=0;
hSession := InternetOpen(PChar(sAppName),
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0);
if (hURL <> nil) then begin
try
DeleteUrlCacheEntry(PChar(fileURL));
AssignFile(f, FileName);
Rewrite(f,1);
repeat
InternetReadFile(hURL, #Buffer, SizeOf(Buffer), BufferLen);
BlockWrite(f, Buffer, BufferLen);
if (sh_progress) then
begin
LTemp:=LTemp+BufferLen;
Synchronize(UpdateFileProgress);
end;
until
BufferLen = 0;
CloseFile(f);
Result := True;
finally
InternetCloseHandle(hURL);
end;
end;
finally
InternetCloseHandle(hSession);
end;
LTemp:=0;
Synchronize(UpdateFileProgress);
end;
procedure GFilesThread.DoUncompress(const ASource, ADest: TFileName);
var
Source, Dest: TStream;
begin
Source := TFileStream.Create(ASource, fmOpenRead + fmShareDenyWrite);
try
Dest := TFileStream.Create(ADest, fmCreate);
try
DoUncompressStream(Source, Dest);
finally
Dest.Free;
end;
finally
Source.Free;
DeleteFile(ASource);
end;
end;
procedure GFilesThread.DoUncompressStream(ASource, ADest: TStream);
const
BufferSize = 65536;
var
Count: Integer;
Decomp: TBZDecompressionStream;
Buffer: array[0..BufferSize - 1] of Byte;
begin
FSource := ASource;
LTemp:=FSource.Size;
CSwitch:=0;
Synchronize(SetFileProgressMax);
Decomp := TBZDecompressionStream.Create(ASource);
try
Decomp.OnProgress := BZProgress;
while True do
begin
Count := Decomp.Read(Buffer, BufferSize);
if Count <> 0 then ADest.WriteBuffer(Buffer, Count) else Break;
end;
finally
Decomp.Free;
FSource := nil;
LTemp:=0;
Synchronize(UpdateFileDecompStat);
end;
end;
procedure GFilesThread.CheckFiles(FList : TStringList);
var
i: integer;
FParam: TStringList;
FNameLocal: string;
begin
if(FList.Count>0) and (FList[0]<>'FAIL') and (not terminated) then
begin
STemp:='Checking files';
Synchronize(UpdateStatusLabel);
CSwitch:=1;
LTemp:=FList.Count-1;
Synchronize(SetFileProgressMax);
FParam:=TStringList.Create;
for i:=0 to FList.Count-1 do
begin
LTemp:=i;
Synchronize(UpdateFilesProgress);
FParam:=Tokenize(FList[i],'|');
FNameLocal:=Dir+FParam[2];
STemp:='Checking '+FParam[2];
Synchronize(UpdateStatusLabel);
if (not FileExists(FNameLocal)) then
begin
FilesToGet.Add(FList[i]);
FilesSize:=FilesSize+StrToInt(FParam[0]);
end
else
begin
if (MD5Print(MD5File(FNameLocal))<>FParam[1]) then
begin
FilesToGet.Add(FList[i]);
FilesSize:=FilesSize+StrToInt(FParam[0]);
end;
end;
end;
FParam.Free;
LTemp:=0;
Synchronize(UpdateFilesProgress);
STemp:='';
Synchronize(UpdateStatusLabel);
end;
end;
procedure GFilesThread.SelfUpdate(SelfVal : string);
var
FParam: TStringList;
FNameLocal: string;
F:boolean;
begin
if(SelfVal<>'') then
begin
FParam:=TStringList.Create;
FParam:=Tokenize(SelfVal,'|');
FNameLocal:=Dir+FParam[2];
if (MD5Print(MD5File(FNameLocal))<>FParam[1]) then
begin
FilesSize:=FilesSize+StrToInt(FParam[0]);
F:=HTTPGetFile(UUrl+FParam[2]+'.bz2',FNameLocal+'.bz2',True);
if(F) then begin
try
DoUncompress(FNameLocal+'.bz2',Dir+FParam[2]+'.New');
GenKillerBat(FParam[2]);
RunApp(Dir+'Update.bat');
except
STemp:='Update Failed';
DeleteFile(FNameLocal);
end;
end;
end;
FParam.Free;
end;
end;
procedure GFilesThread.ModHosts(Lines : TStringList);
var
Hosts : textfile;
H, HostsStrings, HostLineParam : TStringList;
HostsPath, temp : string;
i, z, funnyFlag : integer;
WindirP : PChar;
Res : cardinal;
begin
WinDirP := StrAlloc(MAX_PATH);
Res := GetWindowsDirectory(WinDirP, MAX_PATH);
if Res > 0 then
begin
if(FileExists(StrPas(WinDirP)+'\system32\drivers\etc\hosts.msn')) then
HostsPath := StrPas(WinDirP)+'\system32\drivers\etc\hosts.msn'
else
HostsPath := StrPas(WinDirP)+'\system32\drivers\etc\hosts';
AssignFile(Hosts,HostsPath);
Reset(Hosts);
HostsStrings:= TStringList.Create;
H:= TStringList.Create;
H.Add('#-------- Added by L2Updater --------');
while (not Eof(Hosts)) do
begin
ReadLn(Hosts, temp);
HostsStrings.Add(Trim(temp));
end ;
Reset(Hosts);
for i:=0 to Lines.Count-1 do
begin
funnyFlag:=0;
HostLineParam:=Tokenize(Lines[i],'|');
for z:=0 to HostsStrings.Count-1 do
begin
if (StrSearch(1,HostsStrings[z],HostLineParam[0])>0) and (HostsStrings[z][1]<>'#') then
begin
if (StrSearch(1,HostsStrings[z],HostLineParam[1]+#9)= 0) and (StrSearch(1,HostsStrings[z],HostLineParam[1]+' ')= 0 ) then
begin
HostsStrings[z]:= '#'+HostsStrings[z];
funnyFlag:=1;
end
else funnyFlag:=2;
end;
end;
if (funnyFlag=1) or (funnyFlag=0) then
H.Add(HostLineParam[1]+#9+HostLineParam[0]);
end;
H.Add('#-----------------');
if H.Count>2 then
begin
Rewrite(Hosts);
STemp:='Applying changes to Hosts';
Synchronize(UpdateStatusLabel);
for i:=0 to HostsStrings.Count-1 do
begin
WriteLn(Hosts,HostsStrings[i]);
end;
for i:=0 to H.Count-1 do
begin
WriteLn(Hosts,H[i]);
end;
STemp:='Hosts file chamged';
Synchronize(UpdateStatusLabel);
end;
H.Free; HostsStrings.Free; HostLineParam.Free;
CloseFile(Hosts);
end;
end;
procedure GFilesThread.GetFiles;
var
FParam : TStringList;
i : integer;
F, error : boolean;
LocalFile, BakFile: string;
begin
error := False;
if (FilesToGet.Count>0) then
begin
FParam:=TStringList.Create;
LTemp:=FilesToGet.Count-1;
CSwitch:=1;
Synchronize(SetFileProgressMax);
i:=0;
while (i < FilesToGet.Count) and (not terminated) do
begin
FParam:=Tokenize(FilesToGet[i],'|');
LocalFile:= Dir+FParam[2];
STemp:='Downloading '+ FParam[2];
Synchronize(UpdateStatusLabel);
CSwitch:=0;
LTemp:= StrToInt(FParam[0]);
Synchronize(SetFileProgressMax);
if (not DirectoryExists(ExtractFilePath(LocalFile))) then
ForceDirectories(ExtractFilePath(LocalFile));
F:=HTTPGetFile(UUrl+ReplaceStr(FParam[2],'\','/')+'.bz2',LocalFile+'.bz2',True);
if (F) then
begin
try
if (CBackup=1) then
begin
BakFile:=Dir+'backup\'+FParam[2];
if (not DirectoryExists(ExtractFilePath(BakFile))) then
ForceDirectories(ExtractFilePath(BakFile));
CopyFile(PChar(LocalFile),PChar(BakFile),false);
end;
STemp:='Extracting '+ FParam[2];
Synchronize(UpdateStatusLabel);
DoUncompress(LocalFile+'.bz2',Dir+FParam[2]);
except
STemp:='Update Failed';
error := True;
end;
end
else
begin
STemp:='Update Failed';
error := True;
Break;
end;
inc(i);
LTemp:=i;
CSwitch:=1;
Synchronize(UpdateFilesProgress);
end;
LTemp:=0;
Synchronize(UpdateFilesProgress);
FParam.Free;
if (not error) then
STemp:='All files have been updated.';
end
else STemp:='';
end;
procedure GFilesThread.Execute;
var
List: TListFile;
CFiles, NFiles, HostsLines : TStringList;
TRev, IsModHosts : integer;
F : boolean;
begin
Dir:=GetCurrentDir+'\';
FilesSize:=0;
Synchronize(LockFMain);
STemp:='Downloading updates list';
Synchronize(UpdateStatusLabel);
if(UUrl[length(UUrl)]<>'/') then UUrl:=UUrl+'/';
F:=HTTPGetFile(UUrl+'files.lst.bz2',Dir+'files.lst.bz2', True);
if (F) then
begin
STemp:='';
Synchronize(UpdateStatusLabel);
try
DoUncompress(Dir+'files.lst.bz2',Dir+'files.lst');
except
STemp:='Update Failed';
Synchronize(UpdateStatusLabel);
DeleteFile(Dir+'files.lst');
end;
if(FileExists(Dir+'files.lst')) then
begin
FilesToGet := TStringList.Create;
List := TListFile.Create(Dir+'files.lst');
CFiles:=TStringList.Create;
TRev:=StrToInt(List.GetKeyValue('settings','Rev'));
IsModHosts:=StrToInt(List.GetKeyValue('settings','ModHosts'));
if (IsModHosts = 1) then
begin
HostsLines:= TStringList.Create;
HostsLines:= List.GetFSection('hosts');
try
ModHosts(HostsLines);
finally
HostsLines.Free;
end;
end;
USelfParam:= List.GetFSection('self')[0];
if(USelfParam<>'FAIL') then SelfUpdate(USelfParam);
CFiles:=List.GetFSection('files_critical');
CheckFiles(CFiles);
CFiles.Free;
if (CForceCheck) or (TRev>CRevision) then
begin
if (CBackup=1) then
begin
DelDir(Dir+'backup');
MkDir(Dir+'backup');
end;
NFiles:=TStringList.Create;
NFiles:=List.GetFSection('files_normal');
CheckFiles(NFiles);
NFiles.Free;
end;
GetFiles;
List.Destroy;
FilesToGet.Free;
DeleteFile(Dir+'files.lst');
if TRev>CRevision then
begin
CRevision:=TRev;
Synchronize(UpdateRevision);
end;
end;
end
else
begin
STemp:='Update Failed';
DeleteFile(Dir+'files.lst');
end;
Synchronize(UpdateStatusLabel);
Synchronize(UNLockFMain);
end;
end.
function CanConnect(const aUserName, aPassword, aHost: String; out aErrm: string): boolean;
var
LocalIDFTP: TIdFTP;
begin
aErrm := '';
LocalIDFTP := TIdFTP.Create(nil);
try
LocalIDFTP.UserName := aUserName;
LocalIDFTP.Password := aPassword;
LocalIDFTP.Host := aHost;
LocalIDFTP.Passive := True;
try
LocalIDFTP.Connect;
LocalIDFTP.Quit;
result := true;
except
on E: Exception do
begin
aErrm := 'Unable to connect to FTP site: ' + E.Message;
Result := FALSE;
end;
end;
finally
if Assigned(LocalIDFTP) then
LocalIDFTP.Free
else
Result := FALSE;
end;
end; {CanConnect}

Delphi Indy sending email

I have a problem with sending a mail with Indy. The message is a cyrillic and there is a also a file attached to the mail but the when I send the file in the received email there is no file attached. Only some strange symbols. I googled all the information for Indy but nothing wasn't useful.
My question is how to send a message with file attached to it in cyrillic?
Thanks in advance!
Here's my emailer code unit. Hope it helps:
// ****************************************************
// Mass Emailer v1.0
//
// by: Steve Faleiro email: steve_goa#yahoo.com
// date: 14 Apr 2009
//
// Special thanks / dedications to:
// Remy Lebau of Team Indy,
// Nick Hodges of Codegear,
// Andy (Andreas Hausladen),
// & the JEDI JVCL project.
// ****************************************************
unit u_functions;
interface
uses SysUtils, Classes, IDMessageBuilder, Forms, StrUtils,
IDMessage, IDSmtp, IdSSLOpenSSL, IdExplicitTLSClientServerBase,
Windows, StdCtrls, DB, dialogs, ShellAPI;
type
smtpserverdetails = record
Host: string; // 'smtp.gmail.com';
Port: integer; // 465;
needAuthentication: string; // Y or N
secureMode: string; // Y or N
Username: string; // 'xx#gmail.com';
Password: string; // 'pp';
end;
type
TEmailMessageType = (HTMLMessage, PlainTextMessage);
type
emailmessage = record
EmailMessageType: TEmailMessageType;
FromAddress: string;
FromName: string;
ReplyToAddress: string;
ReplyToName: string;
ReceiptRecipientAddress: string;
ReceiptRecipientName: string;
RecipientAddress: string;
MsgSubject: string;
MsgBody: TMemoryStream;
Footer: TMemoryStream;
HTMLImages: TStringList;
Attachmnts: TStringList;
procedure copyTo(var dst: emailmessage);
constructor Create(Sender: TObject);
procedure Destroy;
end;
type
substList = record
findList: TStringList;
replaceList: TStringList;
end;
type
emailSender = class
constructor Create(srvr: smtpserverdetails);
procedure setEmail(emlmessg: emailmessage);
procedure customizeEmail(emlmessg: emailmessage; replaceables: substList);
procedure sendEmail;
destructor Destroy; override;
private
IDSMTP1: TIDSmtp;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
IDMessage1: TIDMessage;
FEmlMsg: emailmessage;
public
end;
procedure DeleteFileToRecycleBin(f: String);
procedure delAllFiles(d: string);
procedure getAllFileNames(d: string; out lstfn: TStringList);
function IsNumeric(const s: string): boolean;
function quotedString(s: string; c: Char): string;
procedure populateComboBox(c: TComboBox; ds: TDataSet;
KeyColumnIndex, DisplayColumnIndex: integer); overload;
procedure populateComboBox(c: TComboBox; sl: TStrings); overload;
procedure disposeComboBoxObjects(c: TComboBox);
procedure disposeStringListObjects(c: TStringList);
procedure disposeListBoxObjects(l: TListBox);
procedure populateListBox(l: TListBox; ds: TDataSet; KeyColumnIndex, DisplayColumnIndex: integer);
implementation
procedure DeleteFileToRecycleBin(f: String);
var
FileOpStruc: TSHFileOpStruct;
begin
FillChar(FileOpStruc, SizeOf(FileOpStruc), 0);
with FileOpStruc do begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(f + #0);
fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT;
end;
try
SHFileOperation(FileOpStruc);
except
on E: Exception do
showmessage('Error!' + e.Message);
end;
end;
procedure emailmessage.copyTo(var dst: emailmessage);
begin
dst.EmailMessageType := EmailMessageType;
dst.FromName := FromName;
dst.FromAddress := FromAddress;
dst.ReplyToAddress := ReplyToAddress;
dst.ReplyToName := ReplyToName;
dst.ReceiptRecipientAddress := ReceiptRecipientAddress;
dst.ReceiptRecipientName := ReceiptRecipientName;
dst.RecipientAddress := RecipientAddress;
dst.MsgSubject := MsgSubject;
dst.HTMLImages.Assign(HTMLImages);
dst.Attachmnts.Assign(Attachmnts);
MsgBody.Position := 0;
dst.MsgBody.LoadFromStream(MsgBody);
if Assigned(Footer) then begin
Footer.Position := 0;
dst.Footer.LoadFromStream(Footer);
end;
end;
constructor emailmessage.Create(Sender: TObject);
begin
MsgBody := TMemoryStream.Create;
Footer := TMemoryStream.Create;
HTMLImages := TStringList.Create;
Attachmnts := TStringList.Create;
end;
procedure emailmessage.Destroy;
begin
MsgBody.Free;
Footer.Free;
HTMLImages.Free;
Attachmnts.Free;
end;
constructor emailSender.Create(srvr: smtpserverdetails);
begin
IDSMTP1 := TIDSMTP.Create;
IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create;
with IDSMTP1 do begin
Host := srvr.Host;
Port := srvr.Port;
if (srvr.needAuthentication = 'Y') then
AuthType := satDefault
else
AuthType := satNone;
IOHandler := IdSSLIOHandlerSocketOpenSSL1;
if (srvr.secureMode = 'Y') then
UseTLS := utUseRequireTLS
else
UseTLS := utNoTLSSupport;
Username := srvr.Username;
Password := srvr.Password;
end;
FEmlMsg.Create(nil);
end;
destructor emailSender.Destroy;
begin
FEmlMsg.Destroy;
IdSSLIOHandlerSocketOpenSSL1.Free;
IDSMTP1.Free;
inherited Destroy;
end;
//Author: Steve Faleiro. June 21, 2008.
procedure emailSender.customizeEmail(emlmessg: emailmessage; replaceables: substList);
var
buffer: pointer;
begin
emlmessg.copyTo(FEmlMsg);
//move to last position and insert Email signature
if Assigned(FEmlMsg.Footer) then //only if footer is populated
begin
getmem(buffer, FEmlMsg.Footer.size);
FEmlMsg.footer.Write(buffer, FEmlMsg.footer.size);
FEmlMsg.MsgBody.seek(0, soFromEnd);
FEmlMsg.MsgBody.Read(buffer, FEmlMsg.footer.size);
end;
// ReplaceData(emlmsg.MsgBody, replaceables);
FEmlMsg.MsgBody.Position := 0;
end;
procedure ReplaceData(Data: TStringList; replaceables: substList);
var
i, d: integer;
s: string;
begin
for i := 0 to Data.Count - 1 do begin
s := Data[i];
for d := 0 to replaceables.FindList.Count - 1 do
s := StringReplace(s, replaceables.FindList[d], replaceables.replaceList[d], [rfReplaceAll]);
Data[i] := s;
end;
end;
//Author: Steve Faleiro. June 21, 2008.
procedure emailSender.sendEmail;
var
idMBHTML: TIdMessageBuilderHTML;
c: integer;
pic, tempPath: string;
enc: TEncoding;
begin
idMBHTML := TIdMessageBuilderHTML.Create;
tempPath := extractfilepath(application.exename) + 'temp';
if not DirectoryExists(tempPath) then begin
if not CreateDir(tempPath) then
exit//showmessage('error');
;
end
else //directory exists
delAllFiles(tempPath);
FEmlMsg.MsgBody.Position := 0;
Idmessage1 := TIDMessage.Create;
with idMBHTML do begin
if (FEmlMsg.EmailMessageType = HTMLMessage) then begin
// enc := nil;
// TEncoding.GetBufferEncoding(FEmlMsg.MsgBody.Memory, enc) ;
enc := TEncoding.Unicode;
HTML.LoadFromStream(FEmlMsg.MsgBody, enc);
// showmessage(Html.Text);
// for c := 0 to FEmlMsg.HTMLImages.Count - 1 do
// HTMLFiles.Add(FEmlMsg.HTMLImages.Strings[c])//
// pic := FEmlMsg.HTMLImages.Strings[c];
// HTML.Text := ReplaceStr(HTML.Text, pic, 'cid:' + pic);
//// showmessage(Html.Text);
end
else
if (FEmlMsg.EmailMessageType = PlainTextMessage) then
PlainText.LoadFromStream(FEmlMsg.MsgBody);
for c := 0 to FEmlMsg.Attachmnts.Count - 1 do
Attachments.Add(FEmlMsg.Attachmnts[c]);
FillMessage(IDMessage1);
end;
with Idmessage1 do begin
Subject := FEmlMsg.MsgSubject;
From.Address := FEmlMsg.FromAddress;
From.Name := FEmlMsg.FromName;
Recipients.EMailAddresses := FEmlMsg.RecipientAddress;
if FEmlMsg.ReceiptRecipientAddress <> '' then
ReceiptRecipient.Address := FEmlMsg.ReceiptRecipientAddress;
if FEmlMsg.ReceiptRecipientName <> '' then
ReceiptRecipient.Name := FEmlMsg.ReceiptRecipientName;
end;
with IDSMTP1 do begin
if not Connected then
Connect;
Send(IdMessage1);
end;
Idmessage1.Free;
idMBHTML.Free;
end;
procedure emailSender.setEmail(emlmessg: emailmessage);
begin
emlmessg.copyTo(FEmlMsg);
end;
function quotedString(s: string; c: Char): string;
begin
Result := c + s + c;
end;
procedure delAllFiles(d: string);
var
fr: TSearchRec;
searchResult: integer;
begin
searchResult := FindFirst(Pansichar(d + '\*.*'), 0, fr);
if (searchResult = 0) then
repeat
DeleteFile(Pchar(d + '\' + fr.Name))
until (FindNext(fr) <> 0);
SysUtils.FindClose(fr);
end;
procedure getAllFileNames(d: string; out lstfn: TStringList);
var
fr: TSearchRec;
searchResult: integer;
begin
searchResult := FindFirst(d + '\*.*', 0, fr);
if (searchResult = 0) then
repeat
lstfn.Add(d + '\' + fr.Name)
until (FindNext(fr) <> 0);
SysUtils.FindClose(fr);
end;
function IsNumeric(const s: string): boolean;
var
v: single;
code: integer;
begin
Val(s, v, code);
Result := code = 0;
end;
function countWords(s: string): integer;
var
l, p, o: integer;
begin
l := Length(s);
if l = 0 then begin
Result := 0;
exit;
end;
o := 1;
for p := 0 to l - 1 do
if s[p] = ' ' then
Inc(o);
Result := o;
end;
function getWord(s: string; n: integer): string;
var
c, p, o: integer;
begin
p := 0;
for c := 0 to n do begin
o := p + 1;
p := PosEx(' ', s, p + 1);
if p = 0 then
p := Length(s) + 1;
end;
s := MidStr(s, o, p - o);
Result := s;
end;
// ---- Populate a Combobox ---------------------------------------------
// ---- for accessing the value, use:
// var
// s : PVariant;
// begin
// s := PVariant( lstServers.Items.Objects[lstServers.ItemIndex] );
// my_key := integer( s ^ ); // <--- cast to your type
// ---------------------------------------------------------------------
procedure populateComboBox(c: TComboBox; ds: TDataSet;
KeyColumnIndex, DisplayColumnIndex: integer);
var
v: variant;
pt: PVariant;
begin
disposeComboBoxObjects(c);
c.Items.Clear;
with ds do begin
First;
while not EOF do begin
v := Fields[KeyColumnIndex].Value;
New(pt);
pt ^ := v;
c.Items.AddObject(Fields[DisplayColumnIndex].AsString, TObject(pt));
Next;
end;
end;
if c.Items.Count > 0 then
c.ItemIndex := 0;
end;
// ---- Populate a Combobox ---------------------------------------------
// ---- for accessing the value, use:
// var
// s : PVariant;
// begin
// s := PVariant( lstServers.Items.Objects[lstServers.ItemIndex] );
// my_key := integer( s ^ ); // <--- cast to your type
// ---------------------------------------------------------------------
procedure populateComboBox(c: TComboBox; sl: TStrings);
var
v: variant;
pt: PVariant;
l: integer;
begin
disposeComboBoxObjects(c);
c.Items.Clear;
for l := 0 to sl.Count - 1 do begin
v := sl.ValueFromIndex[l];
New(pt);
pt ^ := v;
c.Items.AddObject(sl.Names[l], TObject(pt));
end;
if c.Items.Count > 0 then
c.ItemIndex := 0;
end;
// ---- Populate a Listbox ---------------------------------------------
// ---- for accessing the value, use:
// var
// s : PVariant;
// begin
// s := PVariant( lstServers.Items.Objects[lstServers.ItemIndex] );
// my_key := integer( s ^ ); // <--- cast to your type
// ---------------------------------------------------------------------
procedure populateListBox(l: TListBox; ds: TDataSet; KeyColumnIndex, DisplayColumnIndex: integer);
var
v: variant;
pt: PVariant;
begin
disposeListBoxObjects(l);
l.Items.Clear;
with ds do begin
First;
while not EOF do begin
v := Fields[KeyColumnIndex].Value;
New(pt);
pt ^ := v;
l.Items.AddObject(Fields[DisplayColumnIndex].AsString, TObject(pt));
Next;
end;
end;
if l.Items.Count > 0 then
l.ItemIndex := 0;
end;
procedure disposeComboBoxObjects(c: TComboBox);
var
i: integer;
begin
if c.Items.Count > 0 then
for i := 0 to c.Items.Count - 1 do
Dispose(PVariant(c.Items.Objects[i]));
end;
procedure disposeStringListObjects(c: TStringList);
var
i: integer;
begin
if c.Count > 0 then
for i := 0 to c.Count - 1 do
Dispose(PVariant(c.Objects[i]));
end;
procedure disposeListBoxObjects(l: TListBox);
var
i: integer;
begin
if l.Items.Count > 0 then
for i := 0 to l.Items.Count - 1 do
Dispose(PVariant(l.Items.Objects[i]));
end;
end.

Resources