I
ilovedelphi3
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
function LocalHandle(Mem: Pointer): HLOCAL
stdcall;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function LocalHandle
external kernel32 name 'LocalHandle';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
MyMessageSize = 200;
var
MyMessageDefinition: Pchar
// message format
OutputMessage: PChar
// holds a formatted message
MyArguments: array[1..5] of PChar
// pointers to numeric arguments
MyMessageID: Integer
// holds a message identifier
MyLanguageID: Integer
// holds a language identifier
MyMemoryHandle: HLOCAL
// a handle to a formatted message
begin
{Get space for output message}
GetMem(OutputMessage,MyMessageSize);
{format a message}
MyMessageDefinition := 'Delphi %1. I like %2.';
MyArguments[1] := 'rocks';
MyArguments[2] := 'Delphi 3';
FormatMessage(FORMAT_MESSAGE_FROM_STRING or
FORMAT_MESSAGE_ARGUMENT_ARRAY,
MyMessageDefinition,0,0,
OutputMessage,MyMessageSize,
@MyArguments);
MyArguments[1] := '';
MyArguments[2] := '';
{displays: Delphi rocks. I like Delphi 3}
Label1.Caption := string(OutputMessage);
{examples of numeric substitution. Arguments contain the data.
This uses "C"
style printf formatting}
Integer(MyArguments[1]) := 54;
Integer(MyArguments[2]) := 49;
Integer(MyArguments[3]) := -100;
Integer(MyArguments[4]) := -37;
Integer(MyArguments[5]) := -37;
{format the message}
MyMessageDefinition :=
'character:%1!c! decimal:%2!d! unsigned hexadecimal:%3!x!'
+ ' unsigned int:%4!u! or:%5!lu!';
FormatMessage(FORMAT_MESSAGE_FROM_STRING or
FORMAT_MESSAGE_ARGUMENT_ARRAY,
MyMessageDefinition,0,0,
OutputMessage,MyMessageSize,
@MyArguments);
Label2.Caption := string(OutputMessage);
{format the message differently}
MyMessageDefinition :=
'unsigned hexadecimal:%3!x! character:%1!c! decimal:%2!d!'
+ ' unsigned int:%4!u! or:%5!lu!';
FormatMessage(FORMAT_MESSAGE_FROM_STRING or
FORMAT_MESSAGE_ARGUMENT_ARRAY,
MyMessageDefinition,0,0,
OutputMessage,MyMessageSize,
@MyArguments);
Label3.Caption := string(OutputMessage);
{free output message space}
Freemem(OutputMessage);
{retrieve the system string for an error message}
MyMessageID := ERROR_INVALID_FLAGS
// any system message ID
MyLanguageID := 0
// default language
{Use the option where FormatMessage allocates it's own message buffer.}
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or
FORMAT_MESSAGE_ALLOCATE_BUFFER,nil,
MyMessageID,MyLanguageID,
@OutputMessage,0,nil);
Label4.Caption := string(OutputMessage);
{return message memory}
MyMemoryHandle := LocalHandle(OutputMessage);
if (LocalFree(MyMemoryHandle) <> 0) then
ShowMessage('Error freeing memory');
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
function LocalHandle(Mem: Pointer): HLOCAL
stdcall;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function LocalHandle
external kernel32 name 'LocalHandle';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
MyMessageSize = 200;
var
MyMessageDefinition: Pchar
// message format
OutputMessage: PChar
// holds a formatted message
MyArguments: array[1..5] of PChar
// pointers to numeric arguments
MyMessageID: Integer
// holds a message identifier
MyLanguageID: Integer
// holds a language identifier
MyMemoryHandle: HLOCAL
// a handle to a formatted message
begin
{Get space for output message}
GetMem(OutputMessage,MyMessageSize);
{format a message}
MyMessageDefinition := 'Delphi %1. I like %2.';
MyArguments[1] := 'rocks';
MyArguments[2] := 'Delphi 3';
FormatMessage(FORMAT_MESSAGE_FROM_STRING or
FORMAT_MESSAGE_ARGUMENT_ARRAY,
MyMessageDefinition,0,0,
OutputMessage,MyMessageSize,
@MyArguments);
MyArguments[1] := '';
MyArguments[2] := '';
{displays: Delphi rocks. I like Delphi 3}
Label1.Caption := string(OutputMessage);
{examples of numeric substitution. Arguments contain the data.
This uses "C"
style printf formatting}
Integer(MyArguments[1]) := 54;
Integer(MyArguments[2]) := 49;
Integer(MyArguments[3]) := -100;
Integer(MyArguments[4]) := -37;
Integer(MyArguments[5]) := -37;
{format the message}
MyMessageDefinition :=
'character:%1!c! decimal:%2!d! unsigned hexadecimal:%3!x!'
+ ' unsigned int:%4!u! or:%5!lu!';
FormatMessage(FORMAT_MESSAGE_FROM_STRING or
FORMAT_MESSAGE_ARGUMENT_ARRAY,
MyMessageDefinition,0,0,
OutputMessage,MyMessageSize,
@MyArguments);
Label2.Caption := string(OutputMessage);
{format the message differently}
MyMessageDefinition :=
'unsigned hexadecimal:%3!x! character:%1!c! decimal:%2!d!'
+ ' unsigned int:%4!u! or:%5!lu!';
FormatMessage(FORMAT_MESSAGE_FROM_STRING or
FORMAT_MESSAGE_ARGUMENT_ARRAY,
MyMessageDefinition,0,0,
OutputMessage,MyMessageSize,
@MyArguments);
Label3.Caption := string(OutputMessage);
{free output message space}
Freemem(OutputMessage);
{retrieve the system string for an error message}
MyMessageID := ERROR_INVALID_FLAGS
// any system message ID
MyLanguageID := 0
// default language
{Use the option where FormatMessage allocates it's own message buffer.}
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or
FORMAT_MESSAGE_ALLOCATE_BUFFER,nil,
MyMessageID,MyLanguageID,
@OutputMessage,0,nil);
Label4.Caption := string(OutputMessage);
{return message memory}
MyMemoryHandle := LocalHandle(OutputMessage);
if (LocalFree(MyMemoryHandle) <> 0) then
ShowMessage('Error freeing memory');
end;
end.