unit server13;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,ComObj,Printers;type
  TService13 = class(TService)
    procedure ServiceExecute(Sender: TService);
  private
    { Private }
  public
    function GetServiceController: TServiceController; override;
    { Public }
  end;var
  Service13: TService13;
implementation{$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  Service13.Controller(CtrlCode);
end;function TService13.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;procedure TService13.ServiceExecute(Sender: TService);
var
  i   : integer;
  tmp : Integer;
begin
  ShowMessage('Execute');
  for i := 0 to Printer.Printers.Count - 1 do
  begin
      ShowMessage(Printer.Printers.Strings[i]);
  end;
  try
  tmp := Printer.PrinterIndex;
  //启动服务的时候,上面的代码抛出异常,而通过一般访问该代码一切正常,不知道问题出在什么地方,难道是服务程序的执行权限受到限制?服务程序应该是运行在系统级别的呀。
  ShowMessage('Printer.PrinterIndex ='+IntToStr(tmp));
  except
    ShowMessage('Read PrinterIndex Error ='+IntToStr(GetLastError()));
  end;while not Terminated do begin
ServiceThread.ProcessRequests(FALSE);
end;
end;end.//-------------------------------------------------------------
//server13.dfmobject Service13: TService13
  OldCreateOrder = False
  DisplayName = 'Servic13'
  Interactive = True
  StartType = stManual
  OnExecute = ServiceExecute
  Left = 192
  Top = 107
  Height = 480
  Width = 696
end

解决方案 »

  1.   

    try
      tmp := Printer.PrinterIndex;
      //启动服务的时候,上面的代码抛出异常,而通过一般访问该代码一切正常,不知道问题出在什么地方,难道是服务程序的执行权限受到限制?服务程序应该是运行在系统级别的呀。
      ShowMessage('Printer.PrinterIndex ='+IntToStr(tmp));
      except
        on e:Exception do
        begin
          ShowMessage('Error Desc:'+E.Message);//看一看你的到底报的是什么错,你就知道怎么办了
        end;    
      end;
      

  2.   

    Drate(小虫)  
         Help!!
      

  3.   

    在SERVICE中怎么能SHOWMESSAGE呢?当然错误了。
      

  4.   

    一般通过文本文件来记录信息的,服务程序是不能用SHOWMESSAGE,MESSAGEBOX等等函数。
      

  5.   

    服务程序也可以进行交互操作的,在TService的属性设置中加上如下语句就可以了
      Interactive = True
    现在的问题是服务程序似乎有某些方面的限制,导致TPrinter对象出现异常。
      

  6.   

    经过试验,发现你这代码(excute中)的代码在普通程序中运行没问题,在服务程序中的确存在楼主所说的错误,我正怀疑是否在服务程序中不能用printer!!!!
    我正在进一步试验(由于我以前也没做过,哈哈……),有结果马上相告!!
      

  7.   

    procedure ServiceController(CtrlCode: DWord); stdcall;
    begin
      Service13.Controller(CtrlCode);
    end;function TService13.GetServiceController: TServiceController;
    begin
      Result := ServiceController;
    end;
    调用SERVICECONTROLLER时怎么没有参数?返回值又是哪里来的?
      

  8.   

    procedure ServiceController(CtrlCode: DWord); stdcall;
    begin
      Service13.Controller(CtrlCode);
    end;function TService13.GetServiceController: TServiceController;
    begin
      Result := ServiceController;
    end;
    调用SERVICECONTROLLER时怎么没有参数?返回值又是哪里来的?
      

  9.   

    十分感谢 mrtxc(阿春) ,我都郁闷很多日子了。如果只是TPrinter对象出现问题,还可以想办法绕过去,可是现在还有其他很重要的对象比如OLE对象等也出现这样的问题,在服务状态下就表现不正常了。甚至在通过外壳函数调用普通应用程序的时候,如果普通应用程序中包含这样的对象,也会报错的。
      

  10.   

    没做过服务程序
    Printer 是个全局对象,在服务中使用会不会有问题呢?
      

  11.   

    我想把一个普通应用程序改写成服务程序的形式,使它能在系统还没登陆的时候就能运行,
    但是这个普通应用程序中使用了很多TPrinter对象,OLE对象及DDE对象,在改写过程中对这些对象的使用出现了问题,无法绕过去。
      

  12.   

    to :hiflower(花) 
       
       即使不使用全局的TPrinter对象,而采用自己Create的方式,问题仍然存在。