Ah, your Handle will be 'TheWindow'...
var
TheWindow : Hwnd;
Main Topics
Browse All TopicsI'm transforming a program into a system service. It uses a DLL that sends data to my program via a memory file mapping, which needs my program's Handle
MemShare:=OpenFileMapping(
if MemShare=0 then MemShare:=CreateFileMappin
type THookData=record
Handle,Msg,
hHook,Instances:Cardinal;
end;
MyData:PHookData; //Library-Shared Data
My program calls a procedure in the DLL passing it the handle which is neede by MyData
Now the problem, since I made the program a system service with not window etc, I get an error Undeclared Identifier 'Handle'
How should I solve that ?
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Well... look, I have done for you an example of this with two applications: one receiver and other sender:
The Receiver application with a custom Allocated Window:
unit rUnit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
const
WM_CUSTOM_MSG = WM_USER + $1000;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
procedure HookWindowProc(var M :TMessage);
public
{ Public declarations }
end;
type
TShared = record
OtherStuff : integer;
WinHandle : hwnd;
end;
PShared = ^TShared;
var
Form1 : TForm1;
Shared : PShared;
FicheroM : THandle;
TheWindow : Hwnd;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
//First, allocate a Window for to have a handle and a
//winproc for catch the received messages:
TheWindow := AllocateHWnd(HookWindowPro
{Create the memory mapped file}
FicheroM:=CreateFileMappin
nil,
PAGE_READWRITE,
0,
SizeOf(TShared),
'MyFile');
if FicheroM=0 then
raise Exception.Create( 'Error while create file');
{Adrress our record in the memory mapped file}
Shared:=MapViewOfFile(Fich
{Write data in the memory shared zone:}
Shared^.OtherStuff:=666; //Just an example...
Shared^.WinHandle:=TheWind
end;
procedure TForm1.HookWindowProc(var M :TMessage);
begin
case M.msg of
WM_CUSTOM_MSG:
begin
Caption:='Custom Message received...';
end;
else
m.Result := DefWindowProc(TheWindow , m.Msg, m.WParam, m.LParam);
end;
end;
procedure TForm1.FormDestroy(Sender:
begin
//Deallocate the receiver window
if (TheWindow<>0) then DeAllocateHWnd(TheWindow);
{Close the MapView}
if Assigned(Shared) then UnmapViewOfFile(Shared);
{Close the file}
if FicheroM<>0 then CloseHandle(FicheroM);
end;
end.
An the Sender Application:
- Just have a TButton called Button1
unit sUnit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
WM_CUSTOM_MSG = WM_USER + $1000;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TShared = record
OtherStuff : integer;
WinHandle : hwnd;
end;
PShared = ^TShared;
var
Form1 : TForm1;
Shared : PShared;
FicheroM : THandle;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
{Open the memory mapped file}
FicheroM:=OpenFileMapping(
if FicheroM=0 then
raise Exception.Create( 'Error while open file');
{Adrress our record in the memory mapped file}
Shared:=MapViewOfFile(Fich
end;
procedure TForm1.FormDestroy(Sender:
begin
{Close the MapView}
if Assigned(Shared) then UnmapViewOfFile(Shared);
{Close the file}
if FicheroM<>0 then CloseHandle(FicheroM);
end;
procedure TForm1.Button1Click(Sender
begin
//Send the message to the receiver application:
SendMessage(Shared^.WinHan
end;
end.
Working explanation:
The receiver makes this operations:
- Define a new user message
const
WM_CUSTOM_MSG = WM_USER + $1000;
- Allocate a Window for get a handle and a WindProc with AllocateWindow
- Create a Memory mapped file
- Address a record (Shared) to the memory shared zone (with MapViewOfFile)
- Write the handle of the window allocated as receptor of the messages sended by the sender application
The sender application makes:
- Define the same user message
- Open the memory mapped file
- Adrress the same structure to the same memory zone
- Read the hwnd of the allocated window in the receiver application
- Send a message to that readed handle
I hope that with this example you can make work your application... :)
This tecnique of allocate window is also used in Non visual components when it's neccessary receive messages...
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
[Accept - RadikalQ3]
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
[Elvin]
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: RadikalQ3Posted on 2005-08-11 at 08:49:09ID: 14652630
Hi!
c);
indowProc( var M :TMessage);
In your service application, you must create the minimun thing that have a Hwnd, with:
TheWindow := AllocateHWnd(HookWindowPro
Anf you must provide a procedure for hook the received messages:
in the private part, for example:
procedure HookWindowProc(var M :TMessage);
and, in your implementation the function:
procedure YourServiceClasssApp.HookW
begin
case M.msg of
WM_Paint:
begin
//Bla, bla, bla
end;
else
m.Result := DefWindowProc(TheWindow , m.Msg, m.WParam, m.LParam);
end;
end;