一个程序运行后不能注销,也不能关机,也不能重新启动。只能拔掉电源或者按住机箱的电源键才能关闭计算机!高手请进!先来100分表示诚意!解决问题后再加100分! (

W

wanxu

Unregistered / Unconfirmed
GUEST, unregistred user!
一个程序运行后不能注销,也不能关机,也不能重新启动。只能拔掉电源或者按住机箱的电源键才能关闭计算机!高手请进!先来100分表示诚意!解决问题后再加100分! (100分)<br />情况是这样:我做了一个局域网客户端程序,当该程序正常运行后就不能用任何软方法
关闭该程序。除非拔掉电源或者通过机箱上面的电源键才能关机!呜乎哀哉。谁能帮我?

附源代码如下:


unit client;

interface

uses
; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
; StdCtrls, ExtCtrls, ScktComp,shellapi,registry, NMUDP,jpeg;

{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}
const MY_MESSAGE = WM_USER + 100;
Const RSP_SIMPLE_SERVICE = 1;
Const RSP_UNREGISTER_SERVICE = 0;

type
; Tclient_pc = class(TForm)
; ; Panel1: TPanel;
; ; Label2: TLabel;
; ; sex: TEdit;
; ; Label3: TLabel;
; ; cclass: TEdit;
; ; department: TEdit;
; ; Label4: TLabel;
; ; Label5: TLabel;
; ; Client_client: TClientSocket;
; ; m_recev: TMemo;
; ; M_send: TMemo;
; ; Label1: TLabel;
; ; Edit1: TEdit;
; ; Label9: TLabel;
; ; name: TEdit;
; ; show_message: TLabel;
; ; Label7: TLabel;
; ; Timer2: TTimer;
; ; CUDP: TNMUDP;
; ; machine_name: TEdit;
; ; Timer1: TTimer;
; ; Label6: TLabel;
; ; halt: TTimer;
; ; procedure FormCreate(Sender: TObject);
; ; procedure Client_clientRead(Sender: TObject; Socket: TCustomWinSocket);
; ; procedure Edit1KeyDown(Sender: TObject; var Key: Word;
; ; ; Shift: TShiftState);
; ; procedure Client_clientConnect(Sender: TObject;
; ; ; Socket: TCustomWinSocket);
; ; procedure FormClose(Sender: TObject; var Action: TCloseAction);
; ; procedure Client_clientError(Sender: TObject; Socket: TCustomWinSocket;
; ; ; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
; ; procedure Timer2Timer(Sender: TObject);
; ; procedure CUDPDataReceived(Sender: TComponent; NumberBytes: Integer;
; ; ; FromIP: String; Port: Integer);
; ; procedure ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);
; ; procedure Timer1Timer(Sender: TObject);
; ; procedure Client_clientDisconnect(Sender: TObject;
; ; ; Socket: TCustomWinSocket);
; ; procedure haltTimer(Sender: TObject);
; ; procedure departmentKeyDown(Sender: TObject; var Key: Word;
; ; ; Shift: TShiftState);
; ; procedure FormDestroy(Sender: TObject);
; private
; ; { Private declarations }
; ; mach_true_name:string;
; ; close_time:integer;
; ;//procedure OnIconNotify(var Message: TMessage);
; ;//message MY_MESSAGE;
; public
; ; { Public declarations }
; end;

var
; client_pc: Tclient_pc;
; ; Function GetCurrentProcessId():Longint;stdcall;far;external 'kernel32';
; ; Function GetCurrentProcess():Longint;stdcall;far;external 'kernel32';
; ; Function RegisterServiceProcess(dwProcessID:Longint;dwType:Longint):Longint;stdcall;far;external 'kernel32';
implementation
const BufSize=2048;{ 发送每一笔数据的缓冲区大小 }
var
; BmpStream:TMemoryStream;
; myjpeg: TJPEGImage;
; LeftSize:Longint;{ 发送每一笔数据后剩余的字节数 }
; Bitmap:TBitmap;
{$R *.DFM}

{当小图标捕捉到鼠标事件时进入此过程}
{procedure Tclient_pc.OnIconNotify(var Message: TMessage);
const
; ;Busy: Boolean = false;
begin
; ;if not Busy then
; ;begin
; ; ; Busy := true;
; ; ; if Message.LParam=WM_LBUTTONDOWN then
; ; ; //这里编写关于信息提示的代码
; ; ; if Application.MessageBox('Are you sure close?',
; ; ; 'Exit', MB_YESNO)=IDYES then close;
; ; ; Busy := false;
; ;end;
end;}

{将程序strExeFileName置为自动启动 }
function StartUpMyProgram( strPrompt,strExeFileName : string ) : boolean;
var
;registerTemp : TRegistry;
begin
;registerTemp := TRegistry.Create;
;with registerTemp do
; ; begin
; ;RootKey:=HKEY_LOCAL_MACHINE;
; ; ; ; ; //设置根键值为HKEY_LOCAL_MACHINE
; ;//找到Software/Microsoft/Windows/CurrentVersion/Run
; ;if OpenKey('Software/Microsoft/Windows/CurrentVersion/Run',True) then
; ;//写入自己程序的快捷方式信息
; ; ; ; ; begin
; ; ; ; ; ; ;WriteString( strPrompt, strExeFileName );
; ; ; ; ; ; ;result := true;
; ; ; ; ; end
; ; ; ; ; else result := false;
; ;//善后处理
; ;CloseKey;
; ;Free;
; ; end;
end;

procedure Tclient_pc.FormCreate(Sender: TObject);
var
; c: array[0..127] of Char;
; sz: dword;
; temp:integer;
; nid: TNotifyIconData;
begin
; ;//屏蔽ctr+alt+del
; ;SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1, @temp, 0);
; ;{将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}
; ;SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
; ;{调用StartUpMyProgram,使Delphi随WINDOWS启动而自动运行 }
; ;StartUpMyProgram('systray','C:/WINDOWS/systray.exe');
; ;{当主Form建立时通知Windows加入小图标}
; ;nid.cbSize := sizeof(nid); // nid变量的字节数
; ;nid.Wnd := Handle; // 主窗口句柄
; ;nid.uID :=1; // 内部标识,可设为任意数
; ;nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指定
; ;nid.szTip := 'IC卡管理系统'; // 提示字符串
; ;nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息
; ;nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有效
; ;if not Shell_NotifyIcon(NIM_ADD, @nid) then begin
; ;Application.Terminate;
; ;end;

; ;//连接到服务器
; ;client_client.Active:=true;

; ;//确定机器的名字
; ;sz:= SizeOf(c);
; ;GetComputerName(c, sz);
; ;machine_name.text:=c;
; ;mach_true_name:=machine_name.text;
; ;machine_name.text:=copy(machine_name.text,3,sz-2);

; BmpStream:=TMemoryStream.Create;
; myjpeg := TJPEGImage.Create; ;// 创建JPEG图象
; Bitmap:=TBitmap.Create; ;
end;

procedure Tclient_pc.Client_clientRead(Sender: TObject;
; Socket: TCustomWinSocket);
var
; ;receive_text:string;
; ;len,i:integer;
; ;msg,mach_name,cmd:string;
begin
; ;m_recev.Clear;
; ;m_recev.Lines.Add(Socket.ReceiveText);
; ;receive_text:=m_recev.Lines[0];
; ;cmd:=m_recev.Lines[1];


if cmd&lt;&gt;'cmd' then
begin
; ;if receive_text='no' then
; ;begin
; ; ; //保留用户登陆
; ; ; timer1.Enabled:=false;
; ; ; client_pc.Hide;
; ; ; ClipCursor(nil);
; ; ; exit;
; ;end;

; ;if receive_text='ok' then
; ;begin
; ; ; timer1.Enabled:=true;
; ; ; sex.text:=m_recev.Lines[1];
; ; ; name.text:=m_recev.Lines[2];
; ; ; department.text:=m_recev.Lines[3];
; ; ; cclass.text:=m_recev.Lines[4];
; ; ; exit;
; ;end;
; ;if receive_text='维修'then
; ;begin
; ; ; show_message.Caption:='该机器已坏,维修中...';
; ; ; timer1.Enabled:=true;
; ; ; exit;
; ;end;
; ;if receive_text='没刷卡'then
; ;begin
; ; ; show_message.Caption:='该机器没有刷卡,请刷卡后使用!';
; ; ; timer1.Enabled:=true;
; ; ; exit;
; ;end;
; ;if receive_text='可运行'then
; ;begin
; ; ; //这里添加正常运行的代码
; ; ; timer1.Enabled:=false;
; ; ; client_pc.Hide;
; ; ; ClipCursor(nil);
; ; ; exit;
; ;end;
; ;if receive_text='试一次' then
; ;begin
; ; ; timer1.Enabled:=true; ;
; ; ; show_message.Caption:='输入学号错误,再试一次!';
; ; ; exit;
; ;end;
end;

if cmd='cmd' then
begin
; ;//接收来自服务器对单个客户端的命令!
; ;if (copy(receive_text,1,1)='0') or (copy(receive_text,1,1)='1') or (copy(receive_text,1,1)='2') or (copy(receive_text,1,1)='3') or (copy(receive_text,1,1)='4')
; ;or (copy(receive_text,1,1)='5') or (copy(receive_text,1,1)='6') or (copy(receive_text,1,1)='7') or (copy(receive_text,1,1)='8') or (copy(receive_text,1,1)='9') then
; ;begin
; ; ; len:=length(receive_text);
; ; ; i:=strtoint(copy(receive_text,1,1));
; ; ; mach_name:=copy(receive_text,2,i);
; ; ; if mach_true_name&lt;&gt;mach_name then
; ; ; ; exit;

; ; ; msg:=copy(receive_text,i+2,len);
; ; ; if msg='halt' then
; ; ; begin
; ; ; ; ;client_client.Close;
; ; ; ; ;halt.Enabled:=true;
; ; ; ; ;timer2.Enabled:=false;
; ; ; //断开与服务器的连接
; ; ; ; ;exit;
; ; ; end
; ; ; else
; ; ; if msg='lock' then
; ; ; begin
; ; ; ; ;timer1.Enabled:=true;
; ; ; ; ;client_pc.show;
; ; ; ; ;edit1.Text:='';
; ; ; ; ;edit1.Enabled:=false;
; ; ; ; ;name.ReadOnly:=true;
; ; ; ; ;sex.ReadOnly:=true;
; ; ; ; ;cclass.ReadOnly:=true;
; ; ; ; ;department.ReadOnly:=false;
; ; ; ; ;department.Enabled:=true;
; ; ; ; ;show_message.Caption:='你的机器已经被锁定!';
; ; ; ; ;exit;
; ; ; end
; ; ; else
; ; ; if msg='unlock' then
; ; ; begin
; ; ; ; ;client_pc.Hide;
; ; ; ; ;ClipCursor(nil);
; ; ; ; ;timer1.Enabled:=false;
; ; ; ; ;exit;
; ; ; end
; ; ; else
; ; ; showmessage(msg);
; ; ; exit;
; ;end;

; ;//对整个网络发送命令参数
; ;if receive_text='halt' then
; ;begin
; ; ; ; client_client.Close;
; ; ; ; close_time:=strtoint(m_recev.Lines[2]);
; ; ; ; halt.Enabled:=true;
; ; ; ; timer1.Enabled:=false;
; ; ; ; timer2.Enabled:=false;
; ; ; ; ClipCursor(nil);
; ; ; ; exit;
; ;end
; ;else
; ;if receive_text='lock' then
; ;begin
; ; ; ; ;client_pc.show;
; ; ; ; ;timer1.Enabled:=true;
; ; ; ; ;edit1.Text:='';
; ; ; ; ;edit1.Enabled:=false;
; ; ; ; ;name.Enabled:=false;
; ; ; ; ;sex.Enabled:=false;
; ; ; ; ;cclass.Enabled:=false;
; ; ; ; ;department.Enabled:=false;
; ; ; ; ;show_message.Caption:='你的机器已经被锁定!';
; ; ; ; ;exit;
; ;end
; ;else
; ;if receive_text='unlock' then
; ;begin
; ; ; ; ;client_pc.Hide;
; ; ; ; ;ClipCursor(nil);
; ; ; ; ;timer1.Enabled:=false;
; ; ; ; ;exit;
; ;end
; ;else
; ;showmessage(receive_text);
end;
end;

procedure Tclient_pc.Edit1KeyDown(Sender: TObject; var Key: Word;
; Shift: TShiftState);
begin
; ; ;//当按下回车键时,就向服务器传送数据
; ; ;if key=vk_return then
; ; ;begin
; ; ; ; M_send.Lines.Clear;
; ; ; ; M_send.Lines.Add(edit1.Text);
; ; ; ; M_send.Lines.Add(machine_name.text);
; ; ; ; client_client.Socket.SendText(M_send.Lines.Text);
; ; ; ; exit;
; ; ;end;
; ; ;if key=8 then
; ; ;begin
; ; ; ; if edit1.Text='LegendGhost' then
; ; ; ; begin
; ; ; ; ; ;ClipCursor(nil);
; ; ; ; ; ;timer1.Enabled:=false;
; ; ; ; ; ;client_pc.Hide;
; ; ; ; end;
; ; ;end;
end;

procedure Tclient_pc.Client_clientConnect(Sender: TObject;
; Socket: TCustomWinSocket);
var
; cn: array[0..127] of Char;
; sz: dword;

begin
; ;sz:= SizeOf(cn);
; ;GetComputerName(cn, sz);
; ;M_send.Lines.Clear;
; ;M_send.Lines.Add(cn);
; ;M_send.Lines.Add(' ');
; ;client_client.Socket.SendText(M_send.Lines.text);
; ;timer2.Enabled:=false;
end;


{程序被关闭时通知Windows去掉小图标}
procedure Tclient_pc.FormClose(Sender: TObject; var Action: TCloseAction);
var
; ;nid: TNotifyIconData;
begin
; ;nid.cbSize := sizeof(nid); // nid变量的字节数
; ;nid.uID := 1; //内部标识,与加入小图标时的数一致
; ;nid.Wnd := Handle; //主窗口句柄
; ;Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
; ;//断开与服务器的连接
; ;client_client.Destroy;
; ;BmpStream.Free;
; ;myjpeg.Free; ;// 释放资源
; ;Bitmap.Free;
; ;close;
end;

procedure Tclient_pc.Client_clientError(Sender: TObject;
; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
; var ErrorCode: Integer);
begin
; ;errorcode:=0;
; ;show_message.Caption:='正在联接到服务器....';
; ;name.Text:='CSCC';
; ;sex.Text:='CSCC';
; ;department.Text:='CSCC';
; ;cclass.Text:='CSCC';
; ;client_client.Close;
; ;timer2.Enabled:=true;
end;

procedure Tclient_pc.Timer2Timer(Sender: TObject);
var
; cn: array[0..127] of Char;
; sz: dword;
begin
; ;timer1.Enabled:=true;
; ;client_client.Close;
; ;sz:= SizeOf(cn);
; ;GetComputerName(cn, sz);
; ;M_send.Lines.Clear;
; ;M_send.Lines.Add(cn);
; ;M_send.Lines.Add(' ');
; ;client_client.Open;
; ;client_client.Active:=true;
; ;client_client.Socket.SendText(M_send.Lines.text);
end;

procedure Tclient_pc.CUDPDataReceived(Sender: TComponent;
; NumberBytes: Integer; FromIP: String; Port: Integer);
var
; CtrlCode:array[0..29] of char;
; Buf:array[0..BufSize-1] of char;
; TmpStr:string;
; SendSize,LeftPos,TopPos,RightPos,BottomPos:integer;
begin
; CUDP.ReadBuffer(CtrlCode,NumberBytes);{ 读取控制码 }
; if CtrlCode[0]+CtrlCode[1]+CtrlCode[2]+CtrlCode[3]='show' then
; begin { 控制码前4位为“show”表示主控机发出了抓屏指令 }
; ; ; ; ; ; ;if BmpStream.Size=0 then { 没有数据可发,必须截屏生成数据 }
; ; ; ; ; ; ;begin
; ; ; ; ; ; ; ; ; TmpStr:=StrPas(CtrlCode);
; ; ; ; ; ; ; ; ; TmpStr:=Copy(TmpStr,5,Length(TmpStr)-4);
; ; ; ; ; ; ; ; ; LeftPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
; ; ; ; ; ; ; ; ; TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr));
; ; ; ; ; ; ; ; ; TopPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
; ; ; ; ; ; ; ; ; TmpStr:=Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr));
; ; ; ; ; ; ; ; ; RightPos:=StrToInt(Copy(TmpStr,1,Pos(':',TmpStr)-1));
; ; ; ; ; ; ; ; ; BottomPos:=StrToInt(Copy(TmpStr,Pos(':',TmpStr)+1,Length(TmpStr)-Pos(':',TmpStr)));
; ; ; ; ; ; ; ; ; ScreenCap(LeftPos,TopPos,RightPos,BottomPos); {截取屏幕 }
; ; ; ; ; ; ;end;
; ; ; ; ; ; ;if LeftSize&gt;BufSize then
; ; ; ; ; ; ; ; SendSize:=BufSize
; ; ; ; ; ; ;else
; ; ; ; ; ; ; ; SendSize:=LeftSize;
; ; ; ; ; ; ;BmpStream.ReadBuffer(Buf,SendSize);
; ; ; ; ; ; ;LeftSize:=LeftSize-SendSize;
; ; ; ; ; ; ;if LeftSize=0 then
; ; ; ; ; ; ; ; BmpStream.Clear;{ 清空流 }
; ; ; ; ; ; ;CUDP.RemoteHost:=FromIP; { FromIP为主控机IP地址 }
; ; ; ; ; ; ;cUDP.SendBuffer(Buf,SendSize); { 将数据发到主控机的2222口 }
; end;

end;

procedure TClient_pc.ScreenCap(LeftPos,TopPos,
RightPos,BottomPos:integer);
var
; RectWidth,RectHeight:integer;
; SourceDC,DestDC,Bhandle:integer;
; Bitmap:TBitmap;
begin
; RectWidth:=RightPos-LeftPos;
; RectHeight:=BottomPos-TopPos;
; SourceDC:=CreateDC('DISPLAY','','',nil);
; DestDC:=CreateCompatibleDC(SourceDC);
; Bhandle:=CreateCompatibleBitmap(SourceDC,RectWidth,RectHeight);
; SelectObject(DestDC,Bhandle);
; BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,LeftPos,TopPos,SRCCOPY);
; Bitmap:=TBitmap.Create;
; Bitmap.Handle:=BHandle;
; //myjpeg.Assign(bitmap);
; //Application.ProcessMessages;
; //myjpeg.CompressionQuality := 6;
; bitmap.SaveToStream(BmpStream);
; BmpStream.Position:=0;
; LeftSize:=BmpStream.Size;
; Bitmap.Free;
; DeleteDC(DestDC);
; ReleaseDC(Bhandle,SourceDC);
end;

procedure Tclient_pc.Timer1Timer(Sender: TObject);
var
; r:trect;
begin
; ;//限制鼠标在窗体内运动
; ;r:=boundsrect;
; ;clipcursor(@R);
end; ; ; ; ; ; ; ; ; ; ;

procedure Tclient_pc.Client_clientDisconnect(Sender: TObject;
; Socket: TCustomWinSocket);
begin
; ;name.Text:='CSCC';
; ;sex.Text:='CSCC';
; ;department.Text:='CSCC';
; ;cclass.Text:='CSCC';
; ;timer2.Enabled:=true;
end;

procedure Tclient_pc.haltTimer(Sender: TObject);
begin
; ; ; ; close_time:=close_time-1;
; ; ; ; if close_time&lt;=0 then
; ; ; ; begin
; ; ; ; ; ;exitwindowsex(ewx_shutdown+ewx_force,0);
; ; ; ; end
; ; ; ; else
; ; ; ; begin
; ; ; ; ; ;ClipCursor(nil);
; ; ; ; ; ;client_pc.show;
; ; ; ; ; ;edit1.Text:='';
; ; ; ; ; ;edit1.Enabled:=false;
; ; ; ; ; ;name.Enabled:=false;
; ; ; ; ; ;sex.Enabled:=false;
; ; ; ; ; ;cclass.Enabled:=false;
; ; ; ; ; ;department.Enabled:=false;
; ; ; ; ; ;show_message.Caption:='离关机还有:'+inttostr(close_time)+'秒钟';
; ; ; ; end;
end;

procedure Tclient_pc.departmentKeyDown(Sender: TObject; var Key: Word;
; Shift: TShiftState);
begin
; ; ;if key=8 then
; ; ;begin
; ; ; ; if edit1.Text='LegendGhost' then
; ; ; ; begin
; ; ; ; ; ;timer1.Enabled:=false;
; ; ; ; ; ;client_pc.Hide;
; ; ; ; ; ;ClipCursor(nil);
; ; ; ; end;
; ; ;end;
end;

procedure Tclient_pc.FormDestroy(Sender: TObject);
var
; ;nid: TNotifyIconData;
begin
; ;nid.cbSize := sizeof(nid); // nid变量的字节数
; ;nid.uID := 1; //内部标识,与加入小图标时的数一致
; ;nid.Wnd := Handle; //主窗口句柄
; ;Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
; ;//断开与服务器的连接
; ;client_client.Destroy;
; ;BmpStream.Free;
; ;myjpeg.Free;// 释放资源
; ;Bitmap.Free;
; ;close;
end;

end.
 
一看这个题目我就怀疑你用了FastNet的那一套东西,这套东西有BUG的,在NT下还可以强行
关闭,在98下是没有办法关闭的。
赶快换个控件包吧。
DFW里有很多贴子讲NMXXX的,你可以搜索一下看看。
 
看看这个问题吧
http://www.delphibbs.com/delphibbs/dispq.asp?lid=390229
 
呵呵,就是那个Nudp的控件有Bug,这样解决:
程序自己处理WM_QUERYENDSESSION,接收到这个消息之后,关闭UDP连接,就可以了。
 
kingron:感谢回答问题!你好,能说得详细点吗?
 
有一个fastNet的升级包。
www.51delphi.com 有
FastNet563
1M多。
 
在主窗体创建时加
function RegisterServiceProcess (dwProcessID, dwType: DWord) : DWord; stdcall; external 'KERNEL32.DLL';
RegisterServiceProcess(GetCurrentProcessID,1);将从windows的任务列表中删除进程(即ctrl+alt+del不显示),
oncloseuqery事件中加
canclose:=false
如果这里不行就载获关机或重启的事件。消息句柄定义如下.
procedure WMQueryEndSession (var Message: TMessage); message WM_QUERYENDSESSION ;
 
多人接受答案了。
 
顶部