我在win2k下create一个service ,然后打开服务,并启动,可以正常动行,
可是在winxp下却不行~~各位大帮忙~源码如下
SC_HANDLE scmHandle = NULL;
SC_HANDLE svcHandle = NULL;
 scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
 *svcHandle = CreateService(scmHandle, 
ServiceName,
ServiceDesc,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
DriverPath,
NULL, NULL, NULL, NULL, NULL);
srvHandle = OpenService(scmHandle,ServiceName,SERVICE_START);
if (srvHandle != NULL)
{
  if (StartService(srvHandle, 0, NULL)==0)

// 这个地方有错,  错误代码为,系统无法打开文件


ServiceName ,ServiceDesc DriverPath , 都为unicode
以上代码DriverPath 放在c:\winnt\system32\drivers\(win2k)
c:\windows\system32\drivers\ (winxp)
在win2k下可以正常运行,可在winxp下却出错, 我郁闷了?
win2k 和 winxp有什么区别呢?

解决方案 »

  1.   

    SC_MANAGER_ALL_ACCESS 
     Includes STANDARD_RIGHTS_REQUIRED, in addition to all of the access types listed in this table. 
    你换成SC_MANAGER_CREATE_SERVICE试试.
      

  2.   

    SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS); 
    if (schSCManager==0) 
    {
    long nError = GetLastError();
    FILE* pLog = fopen(pLogFile,"a");
    fprintf(pLog, "OpenSCManager failed, error code = %d\n", nError);
    fclose(pLog);
    }
    else
    {
    SC_HANDLE schService = OpenService( schSCManager, pName, SERVICE_ALL_ACCESS);
    if (schService==0) 
    {
    long nError = GetLastError();
    FILE* pLog = fopen(pLogFile,"a");
    fprintf(pLog, "OpenService failed, error code = %d\n", nError); 
    fclose(pLog);
    }
    else
    {
    SERVICE_STATUS status;
    if(nIndex>=0&&nIndex<128)
    {
    if(ControlService(schService,(nIndex|0x80),&status))
    {
    CloseServiceHandle(schService); 
    CloseServiceHandle(schSCManager); 
    return TRUE;
    }
    long nError = GetLastError();
    FILE* pLog = fopen(pLogFile,"a");
    fprintf(pLog, "ControlService failed, error code = %d\n", nError); 
    fclose(pLog);
    }
    else
    {
    FILE* pLog = fopen(pLogFile,"a");
    fprintf(pLog, "Invalid argument to BounceProcess: %d\n", nIndex); 
    fclose(pLog);
    }
    CloseServiceHandle(schService); 
    }
    CloseServiceHandle(schSCManager); 
    }
      

  3.   

    fengzi_zhu大侠, 为什么要用ControlService,
    我第一次创建服务,打开,启动,那么服务的初始状态应为
    SERVICE_STOPPED,我在win2k下可以, 在winxp 下就不行,
    getlasterror()返回4,系统无法打开文件,
    是不是xp下创建时,就已经加载的driver,就不允许访问这个文件了。
      

  4.   

    按fengzi_zhu的代码,
    controlservice
    还是不行, 拒绝访问 ̄ ̄
    不知为何, 大侠,救命呀 ̄
      

  5.   

    服务程序所在目录的安全设置可能不足。GetLastError()返回错误码5。你可以为该目录的安全权限添加SYSTEM或者EVERYONE具有读取运行权限再试一下。
      

  6.   

    执行StartService()后,服务程序将转到ServiceMain()函数运行,你在ServiceMain()里面打开文件吗?或者你当前登陆的用户是否具有安装服务的权限?
      

  7.   

    ServiceMain,我代码里没有对这个函数处理,
    登陆的用户是管理员权限,