并口访问方面的编程 程序出错原因 200分相送 在线等 解决问题马上给分 ( 积分: 100 )

  • 主题发起人 主题发起人 fisheryj
  • 开始时间 开始时间
F

fisheryj

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, Buttons, ComCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;GroupBox1: TGroupBox;<br> &nbsp; &nbsp;BitBtn1: TBitBtn;<br> &nbsp; &nbsp;PortIOLabel: TLabel;<br> &nbsp; &nbsp;StatusBar1: TStatusBar;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;PortIOStatusEdit: TEdit;<br> &nbsp; &nbsp;BitBtn2: TBitBtn;<br> &nbsp; &nbsp;SendDataMemo: TMemo;<br> &nbsp; &nbsp;ReadDataMemo: TMemo;<br> &nbsp; &nbsp;BitBtn3: TBitBtn;<br> &nbsp; &nbsp;procedure BitBtn1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure BitBtn2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure BitBtn3Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;Temp:byte;<br> &nbsp; Flag:byte;<br> &nbsp; chr:byte;<br>implementation<br><br>uses<br>UnitPortTalk;<br><br>{$R *.dfm}<br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br><br>begin<br> &nbsp;Temp:=Inportb($000379);//读状态口数据<br> &nbsp;PortIOStatusEdit.Text:=IntToStr(Temp);<br> &nbsp;asm<br> &nbsp; &nbsp;MOV AH,Temp ;<br> &nbsp; &nbsp;MOV AL,AH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //开始判断位0<br> &nbsp; &nbsp;AND AL,01H &nbsp; //与操作<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br><br> &nbsp;if flag&lt;&gt;0 then PortIOLabel.Caption:='连接超时'<br> &nbsp;else<br> &nbsp;begin<br> &nbsp;PortIOLabel.Caption:='已打开';<br> &nbsp;//判断其他状态位<br> &nbsp;asm<br><br> &nbsp;end;<br><br> &nbsp;end;<br>end;<br><br>procedure TForm1.BitBtn2Click(Sender: TObject);<br>//var<br>//N:integer;<br>//chr:char;<br>begin<br> &nbsp;{asm<br> &nbsp; &nbsp;MOV AH,TEMP;<br> &nbsp; &nbsp;MOV AL,AH &nbsp; //判断位7<br> &nbsp; &nbsp;AND AL,80H<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br> &nbsp;IF &nbsp;FLAG=0 THEN StatusBar1.Panels[0].Text:='打印机忙'<br> &nbsp;else<br> &nbsp;begin<br> &nbsp;//确认接受数据<br> &nbsp;asm<br> &nbsp; &nbsp;MOV AH,TEMP<br> &nbsp; &nbsp;MOV AL,AH<br> &nbsp; &nbsp;ADD AL,40H &nbsp;//判断位6<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br> &nbsp;IF FLAG=0 THEN<br> &nbsp; &nbsp; BEGIN<br> &nbsp; &nbsp; &nbsp; StatusBar1.Panels[0].Text:='打印机未确认';<br><br> &nbsp; &nbsp; END<br> &nbsp; &nbsp; ELSE<br> &nbsp; &nbsp; &nbsp; N:=Length(SendDataMemo.Text)<br> &nbsp; &nbsp; &nbsp; for I:=1 to N do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp;end;}<br>end;<br><br>procedure TForm1.BitBtn3Click(Sender: TObject);<br>begin<br><br> &nbsp;asm<br> &nbsp; &nbsp;MOV AH,Temp ;<br> &nbsp; &nbsp;MOV AL,AH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //开始判断位0<br> &nbsp; &nbsp;AND AL,08H &nbsp; //与操作<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br><br> &nbsp;if flag&lt;&gt;0 then StatusBar1.Panels[0].Text:='打印机离线,缺纸或检测出错'<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;StatusBar1.Panels[0].Text:='可以开始读数据';<br> &nbsp; &nbsp;chr:=InPortb($000378);<br> &nbsp; &nbsp;ReadDataMemo.Text:=IntToStr(Chr);<br> &nbsp;end;<br>end;<br><br>end.
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, Buttons, ComCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;GroupBox1: TGroupBox;<br> &nbsp; &nbsp;BitBtn1: TBitBtn;<br> &nbsp; &nbsp;PortIOLabel: TLabel;<br> &nbsp; &nbsp;StatusBar1: TStatusBar;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;PortIOStatusEdit: TEdit;<br> &nbsp; &nbsp;BitBtn2: TBitBtn;<br> &nbsp; &nbsp;SendDataMemo: TMemo;<br> &nbsp; &nbsp;ReadDataMemo: TMemo;<br> &nbsp; &nbsp;BitBtn3: TBitBtn;<br> &nbsp; &nbsp;procedure BitBtn1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure BitBtn2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure BitBtn3Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;Temp:byte;<br> &nbsp; Flag:byte;<br> &nbsp; chr:byte;<br>implementation<br><br>uses<br>UnitPortTalk;<br><br>{$R *.dfm}<br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br><br>begin<br> &nbsp;Temp:=Inportb($000379);//读状态口数据<br> &nbsp;PortIOStatusEdit.Text:=IntToStr(Temp);<br> &nbsp;asm<br> &nbsp; &nbsp;MOV AH,Temp ;<br> &nbsp; &nbsp;MOV AL,AH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //开始判断位0<br> &nbsp; &nbsp;AND AL,01H &nbsp; //与操作<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br><br> &nbsp;if flag&lt;&gt;0 then PortIOLabel.Caption:='连接超时'<br> &nbsp;else<br> &nbsp;begin<br> &nbsp;PortIOLabel.Caption:='已打开';<br> &nbsp;//判断其他状态位<br> &nbsp;asm<br><br> &nbsp;end;<br><br> &nbsp;end;<br>end;<br><br>procedure TForm1.BitBtn2Click(Sender: TObject);<br>//var<br>//N:integer;<br>//chr:char;<br>begin<br> &nbsp;{asm<br> &nbsp; &nbsp;MOV AH,TEMP;<br> &nbsp; &nbsp;MOV AL,AH &nbsp; //判断位7<br> &nbsp; &nbsp;AND AL,80H<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br> &nbsp;IF &nbsp;FLAG=0 THEN StatusBar1.Panels[0].Text:='打印机忙'<br> &nbsp;else<br> &nbsp;begin<br> &nbsp;//确认接受数据<br> &nbsp;asm<br> &nbsp; &nbsp;MOV AH,TEMP<br> &nbsp; &nbsp;MOV AL,AH<br> &nbsp; &nbsp;ADD AL,40H &nbsp;//判断位6<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br> &nbsp;IF FLAG=0 THEN<br> &nbsp; &nbsp; BEGIN<br> &nbsp; &nbsp; &nbsp; StatusBar1.Panels[0].Text:='打印机未确认';<br><br> &nbsp; &nbsp; END<br> &nbsp; &nbsp; ELSE<br> &nbsp; &nbsp; &nbsp; N:=Length(SendDataMemo.Text)<br> &nbsp; &nbsp; &nbsp; for I:=1 to N do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp;end;}<br>end;<br><br>procedure TForm1.BitBtn3Click(Sender: TObject);<br>begin<br><br> &nbsp;asm<br> &nbsp; &nbsp;MOV AH,Temp ;<br> &nbsp; &nbsp;MOV AL,AH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //开始判断位0<br> &nbsp; &nbsp;AND AL,08H &nbsp; //与操作<br> &nbsp; &nbsp;MOV FLAG,AL<br> &nbsp;end;<br><br> &nbsp;if flag&lt;&gt;0 then StatusBar1.Panels[0].Text:='打印机离线,缺纸或检测出错'<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;StatusBar1.Panels[0].Text:='可以开始读数据';<br> &nbsp; &nbsp;chr:=InPortb($000378);<br> &nbsp; &nbsp;ReadDataMemo.Text:=IntToStr(Chr);<br> &nbsp;end;<br>end;<br><br>end.
 
工程文件在 <br>fisheryj.ys168.com<br>2000硬件访问下面
 
没见到你Open这个Porttalk,是不是这个原因?
 
在UNITPORTTALK。PAS<br>单元里面提供了 打开 断口 的函数 <br><br> <br><br><br><br><br><br><br><br><br><br><br>unit UnitPortTalk;<br><br>interface<br><br>uses<br> &nbsp;Windows, SysUtils, Dialogs,WinSvc;<br><br>const<br> &nbsp;PORTTALK_TYPE = 40000; { 32768-65535是保留给用户使用的}<br> &nbsp;METHOD_BUFFERED = 0;<br> &nbsp;FILE_ANY_ACCESS = 0;<br> &nbsp;IOCTL_IOPM_RESTRICT_ALL_ACCESS &nbsp;= PORTTALK_TYPE shl 16 +<br> &nbsp; &nbsp;$900 shl 2 +<br> &nbsp; &nbsp;METHOD_BUFFERED +<br> &nbsp; &nbsp;FILE_ANY_ACCESS shl 14;<br><br> &nbsp;IOCTL_IOPM_ALLOW_EXCUSIVE_ACCESS = PORTTALK_TYPE shl 16 +<br> &nbsp; &nbsp;$901 shl 2 +<br> &nbsp; &nbsp;METHOD_BUFFERED +<br> &nbsp; &nbsp;FILE_ANY_ACCESS shl 14;<br><br><br> &nbsp;IOCTL_SET_IOPM = PORTTALK_TYPE shl 16 +<br> &nbsp; &nbsp;$902 shl 2 +<br> &nbsp; &nbsp;METHOD_BUFFERED +<br> &nbsp; &nbsp;FILE_ANY_ACCESS shl 14;<br><br> &nbsp;IOCTL_ENABLE_IOPM_ON_PROCESSID = PORTTALK_TYPE shl 16 +<br> &nbsp; &nbsp;$903 shl 2 +<br> &nbsp; &nbsp;METHOD_BUFFERED +<br> &nbsp; &nbsp;FILE_ANY_ACCESS shl 14;<br><br> &nbsp;IOCTL_READ_PORT_UCHAR = PORTTALK_TYPE shl 16 +<br> &nbsp; &nbsp;$904 shl 2 +<br> &nbsp; &nbsp;METHOD_BUFFERED +<br> &nbsp; &nbsp;FILE_ANY_ACCESS shl 14;<br><br> &nbsp;IOCTL_WRITE_PORT_UCHAR = PORTTALK_TYPE shl 16 +<br> &nbsp; &nbsp;$905 shl 2 +<br> &nbsp; &nbsp;METHOD_BUFFERED +<br> &nbsp; &nbsp;FILE_ANY_ACCESS shl 14;<br><br> &nbsp;function OpenPortTalk:boolean;<br> &nbsp;procedure ClosePortTalk;<br> &nbsp;procedure outportb(PortAddress:word;byte1:byte);<br> &nbsp;function inportb(PortAddress:word):byte;<br><br> &nbsp;function StartPortTalkDriver:boolean;<br> &nbsp;procedure InstallPortTalkDriver;<br> &nbsp;<br>var<br> &nbsp;PortTalk_Handle:THANDLE; &nbsp; &nbsp; &nbsp; &nbsp;{PortTalk句柄}<br><br>implementation<br><br><br>procedure outportb(PortAddress:word;byte1:byte);<br>var<br> &nbsp; &nbsp;error:boolean;<br> &nbsp; &nbsp;BytesReturned:DWORD;<br> &nbsp; &nbsp;Buffer:array[0..2]of byte;<br> &nbsp; &nbsp;pBuffer:pword;<br>begin<br> &nbsp; &nbsp;pBuffer := pword(@Buffer[0]);<br> &nbsp; &nbsp;pBuffer^ := PortAddress;<br> &nbsp; &nbsp;Buffer[2] := byte1;<br><br> &nbsp; &nbsp;error := DeviceIoControl(PortTalk_Handle,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Cardinal(IOCTL_WRITE_PORT_UCHAR),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Buffer,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BytesReturned,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil);<br><br> &nbsp; &nbsp;if (not error) then showmessagefmt('从PortTalk输出端口数据时出错:%d',[GetLastError]);<br>end;<br><br>function inportb(PortAddress:word):byte;<br>var<br> &nbsp; &nbsp;error:boolean;<br> &nbsp; &nbsp;BytesReturned:DWORD;<br> &nbsp; &nbsp;Buffer:array[0..2]of byte;<br> &nbsp; &nbsp;pBuffer:pword;<br>begin<br> &nbsp; &nbsp;pBuffer := pword(@Buffer[0]);<br> &nbsp; &nbsp;pBuffer^ := PortAddress;<br><br> &nbsp; &nbsp;error := DeviceIoControl(PortTalk_Handle,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cardinal(IOCTL_READ_PORT_UCHAR),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Buffer,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Buffer,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BytesReturned,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil);<br><br> &nbsp; &nbsp;if (not error) then showmessagefmt('从PortTalk输入端口数据时出错:%d',[GetLastError]);<br> &nbsp; &nbsp;result:=Buffer[0];<br>end;<br><br>function OpenPortTalk:boolean;<br>begin<br> &nbsp; &nbsp;{打开PortTalk,如果不能打开,则安装它}<br> &nbsp; &nbsp;PortTalk_Handle := CreateFile('//./PortTalk',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GENERIC_READ,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_ATTRIBUTE_NORMAL,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0);<br><br> &nbsp; &nbsp;if(PortTalk_Handle = INVALID_HANDLE_VALUE) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{启动驱动程序}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StartPortTalkDriver;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{再次打开PortTalk}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PortTalk_Handle := CreateFile('//./PortTalk',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GENERIC_READ,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_ATTRIBUTE_NORMAL,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(PortTalk_Handle = INVALID_HANDLE_VALUE) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('PortTalk: 不能存取PortTalk,请确保驱动程序已安装。');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;result:=true;<br>end;<br><br>procedure ClosePortTalk;<br>begin<br> &nbsp; &nbsp;CloseHandle(PortTalk_Handle);<br>end;<br><br>function StartPortTalkDriver:boolean;<br>type<br> &nbsp;TNewStartService=function (hService: SC_HANDLE; dwNumServiceArgs: DWORD;<br> &nbsp; &nbsp;lpServiceArgVectors: PPChar): BOOL; stdcall;<br>var<br> &nbsp; &nbsp;SchSCManager:SC_HANDLE;<br> &nbsp; &nbsp;schService:SC_HANDLE;<br> &nbsp; &nbsp;ret:BOOL;<br> &nbsp; &nbsp;err:DWORD;<br>begin<br><br> &nbsp; &nbsp;{打开Service Control Manager}<br> &nbsp; &nbsp;SchSCManager := OpenSCManager (nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 机器 (nil = 本机) }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 数据库 (nil = 默认 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SC_MANAGER_ALL_ACCESS); &nbsp; &nbsp; { 访问权 }<br><br> &nbsp; &nbsp;if (SchSCManager = 0) then<br> &nbsp; &nbsp; &nbsp;if (GetLastError = ERROR_ACCESS_DENIED) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; { 没有权限打开SCM管理,必须是poor用户}<br> &nbsp; &nbsp; &nbsp; &nbsp; showmessage('PortTalk: 没有权限访问Service Control Manager,'#$D#$A+<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '不能安装和启动PortTalk,请使用超级用户来安装。');<br> &nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br> &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;repeat begin<br> &nbsp; &nbsp; &nbsp; &nbsp; {打开PortTalk服务数据库}<br> &nbsp; &nbsp; &nbsp; &nbsp; schService := OpenService(SchSCManager, &nbsp; &nbsp; &nbsp; {服务数据库句柄}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'PortTalk', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{要启动的服务名}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SERVICE_ALL_ACCESS); {存取的权限}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; if (schService = 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case (GetLastError) of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ERROR_ACCESS_DENIED:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('PortTalk: 没有权限访问PortTalk服务数据库');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ERROR_INVALID_NAME:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('PortTalk: 指定的服务名无效');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ERROR_SERVICE_DOES_NOT_EXIST:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//showmessage('PortTalk: PortTalk驱动程序不存在');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;InstallPortTalkDriver;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; end until (schService &lt;&gt; 0);<br><br> &nbsp; &nbsp;{启动PortTalk驱动程序,如果发生错误,一般是由于PortTalk.SYS不存在。}<br><br> &nbsp; &nbsp;ret := TNewStartService(@StartService) (schService, &nbsp;{服务标识}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {参数个数}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil); &nbsp; &nbsp; &nbsp; &nbsp; {参数}<br><br> &nbsp; &nbsp;if (ret) then //showmessage('PortTalk: PortTalk安装成功!')<br> &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp; &nbsp;err := GetLastError;<br> &nbsp; &nbsp; &nbsp; &nbsp;if (err = ERROR_SERVICE_ALREADY_RUNNING) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('PortTalk: PortTalk已经安装')<br> &nbsp; &nbsp; &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('PortTalk: 启动PortTalk时发生未知错误。'+#$D#$A+<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'PortTalk.SYS没有放入/System32/Drivers目录吗?');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;{关闭Service Control Manager}<br> &nbsp; &nbsp;CloseServiceHandle (schService);<br> &nbsp; &nbsp;result:=TRUE;<br>end;<br><br>procedure InstallPortTalkDriver;<br>var<br> &nbsp; &nbsp;SchSCManager:SC_HANDLE;<br> &nbsp; &nbsp;schService:SC_HANDLE;<br> &nbsp; &nbsp;err:DWORD;<br> &nbsp; &nbsp;DriverFileName:array[0..79]of CHAR;<br>begin<br> &nbsp; &nbsp;if (GetSystemDirectory(DriverFileName, 55)=0) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; showmessage('PortTalk: 取System目录出错');<br> &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;{加入驱动程序文件名}<br> &nbsp; &nbsp;lstrcat(DriverFileName,'/Drivers/PortTalk.sys');<br> &nbsp; &nbsp;//showmessagefmt('PortTalk: 拷贝驱动程序到%s',[DriverFileName]);<br><br> &nbsp; &nbsp;{拷贝驱动程序到System32/drivers目录,如果出错,一般是因为文件不存在。}<br><br> &nbsp; &nbsp;if (not CopyFile('PortTalk.sys', DriverFileName, FALSE)) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; showmessagefmt('PortTalk: 拷贝驱动程序到以下位置出错:%s'+#$D#$A+<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'请手工拷贝到system32/driver目录',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[DriverFileName]);<br> &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;{打开Service Control Manager}<br> &nbsp; &nbsp;SchSCManager := OpenSCManager (nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 机器 (nil = 本机) }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 数据库 (nil = 默认 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SC_MANAGER_ALL_ACCESS); { 访问权 }<br><br> &nbsp; &nbsp;schService := CreateService (SchSCManager, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { SCManager数据库 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'PortTalk', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 服务个数 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'PortTalk', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 显示名 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SERVICE_ALL_ACCESS, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 权限 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SERVICE_KERNEL_DRIVER, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 服务类别 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SERVICE_DEMAND_START, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 启动类别 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SERVICE_ERROR_NORMAL, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 出错控件类别 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'System32/Drivers/PortTalk.sys', &nbsp; { 服务二进制文件 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 加入的组 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 标识 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 隶属 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 本地帐户 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 密码 }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);<br><br> &nbsp; &nbsp;if (schService = 0) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; err := GetLastError;<br> &nbsp; &nbsp; &nbsp; &nbsp; if (err = ERROR_SERVICE_EXISTS) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage('PortTalk: 驱动程序不存在。')<br> &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp;showmessage('PortTalk:建立服务时发生未知的错误。');<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;//else showmessage('PortTalk: 成功安装!');<br><br> &nbsp; &nbsp;{ 关闭Service Control Manager }<br> &nbsp; &nbsp;CloseServiceHandle (schService);<br>end;<br>end.
 
这个我知道,问题是我没看到你在那里调用OpenPortTalk函数
 
好象是<br>我调试一下看看
 
不好意思,我没搞过,所以帮不上忙了
 
我吧 OpenPortTalk函数 &nbsp;<br>给楼了<br>好了 弄好了
 
后退
顶部