1. 函数功能
该函数发送消息给指定的接受者。接受者可以是一个应用程序、安装驱动器、网络驱动器、系统级设备驱动器或这些系统组件的组合。
2. 函数原型
long BroadcastSystemMessage(DWORD dwFIags,LPDWORD IpdwRecipients,UINT UiMessage,WPARAMwParam,LPARAM IParam)
3. 参数
dwFlags:选项标志。可取下列值的组合:
BSF_FLUSHDISK:接受者处理消息之后清洗磁盘。
BSF_FORCEIFHUNG:继续广播消息,即使超时周期结束或一个接受者已挂起。 BSF_IGNORECURRENTTASK:不发送消息给属于当前任务的窗口。这样,应用程序就不会接收自己的消息。
BSF_NOHANG:强制挂起的应用程序超时。如果一个接受者超时,不再继续广播消息。 BSF_NOTIMEOUTIFNOTHUNG:只要接受者没挂起,一直等待对消息的响应。不会出现超时。 BSF_POSTMESSAGE:寄送消息。不能和BSF_QUERY组合使用。
BSF_QUERY:每次发送消息给一个接受者,只有当前接受者返回TRUE后,才能发送给下一个接受者。 lpdwRecipients:指向变量的指针,该变量含有和接收消息接受者的信息。此变量可为下列值的组合: BSM_ALLCOMPONENTS:广播到所有的系统组件。
BSM_ALLDESKTOPS:Windows NT下,广播到所有的桌面。要求SE_TCB_NAME特权。
BSM_APPLICATIONS:广播到应用程序。
BSM_INSTALLABLEDRIVERS:Windows 95下,广播到安装驱动器。
BSM_INTDRIVER:Windows 95下,广播到网络驱动器。
BSM_VXDS:Windows 95下,广播到所有系统级设备驱动器。
当函数返回时,此变量接受上述值的组合,以确定真正接受消息的接受者。如果此参数为NULL,则将消息广播到所有的组件。
uiMessage:系统消息标识符。
WParam:32位消息特定值。
IParam:32位消息特定值。
4. 返回值:如果函数调用成功,返回值是正数。如果函数不能广播消息,返回值是C1。如果参数dwFlags为BSF_QUERY且至少一个接受者返回BROADCAST_QUERY_DENY给相应的消息,返回值是零。若想获得更多的错误信息,请调用GetLastError函数。 备注:如果BSF_QUERY没指定,函数发送指定的消息给所有请求的接受者,并忽略这些接受者返回的值。
速查:Windows NT:4.0及以上版本:Windows:95及以上版本;Windows CE:不支持;头文件:winuser.h;输入库:user32.lib;Unicode:在Windows NT环境下以Unicode和ANSI方式实现。
实例:
发送端:
1 var 2 UserMessage: UINT; 3 4 procedure TForm1.Button1Click(Sender: TObject); 5 var 6 Recipients: DWORD; // holds the recipient flags 7 begin 8 {set the recipients to all applications} 9 Recipients := BSM_APPLICATIONS; 10 11 {send the user defined message to all applications on the system by 12 posting it to their message queues} 13 BroadcastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @Recipients, 14 UserMessage, 0, 0); 15 end; 16 17 procedure TForm1.FormCreate(Sender: TObject); 18 begin 19 {register a user defined message} 20 UserMessage := RegisterWindowMessage('CallWindowProc Test Message'); 21 end;
接收端:
1 {the prototype for the new window procedure} 2 function NewWindowProc(TheWindow: HWND; Msg: Integer; wParam: WPARAM; 3 lParam: LPARAM): Longint; stdcall; 4 5 var 6 UserMessage: UINT; // holds a user defined message identifier 7 OldWindowProc: TFNWndProc; // holds a pointer to the previous window procedure 8 9 function NewWindowProc(TheWindow: HWND; Msg: Integer; wParam: WPARAM; lParam: LPARAM): Longint; 10 var 11 iLoop: Integer; // a general loop counter 12 begin 13 {if the user defined message has been received...} 14 if Msg=UserMessage then 15 begin 16 ShowMessage('收到消息'); 17 Result := 1; 18 end 19 else 20 {any other message must be passed to the previous window procedure} 21 Result := CallWindowProc(OldWindowProc, TheWindow, Msg, wParam, lParam); 22 end; 23 24 procedure TForm1.FormCreate(Sender: TObject); 25 begin 26 {register a user defined message} 27 UserMessage := RegisterWindowMessage('CallWindowProc Test Message'); 28 29 {subclass this window. replace the window procedure with one of 30 ours. this window procedure will receive messages before the 31 previous one, allowing us to intercept and process any message 32 before the rest of the application ever sees it.} 33 OldWindowProc := TFNWndProc(SetWindowLong(Form1.Handle, GWL_WNDPROC, 34 Longint(@NewWindowProc))); 35 end; 36 37 procedure TForm1.FormDestroy(Sender: TObject); 38 begin 39 {reset the window procedure to the previous one} 40 SetWindowLong(Form1.Handle, GWL_WNDPROC, Longint(OldWindowProc)); 41 end;