CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Jan 2004
    Location
    Near Portland, OR
    Posts
    222

    OpenService returns Null but GetLastError also returns Null

    I'm trying to debug some driver install code. We had a problem with code initialization on the first run on a new machine and I've been trying to debug the issue. After getting it to install once I tried to manually uninstall and it's been behaving oddly since. (This is a legacy, non-PnP driver/hardware, it even runs on the ISA bus!)

    I'm having to debug this the old fashioned way with messages because I shouldn't install VS on the target machine.

    The first time one of the applications runs after boot, the following function gets called:

    Code:
    unsigned char StartPortTalkDriver(void)
    {
        SC_HANDLE  SchSCManager;
        SC_HANDLE  schService;
        BOOL       ret;
        DWORD      err;
        char str[100];
    
        /* Open Handle to Service Control Manager */
        SchSCManager = OpenSCManager (NULL,   
                                      NULL,              
                                      SC_MANAGER_ALL_ACCESS);  
                             
        if (SchSCManager == NULL)
        {
            if (GetLastError() == ERROR_ACCESS_DENIED) 
            {
                MessageBox(NULL, "Can't Open Service Control Manager", "DeviceIoControl", MB_OK);
                return(0);
            }
        }
    
        /* Open a Handle to the PortTalk Service Database */
        schService = OpenService(SchSCManager,    
                                 "PortTalk",      
                                 SERVICE_ALL_ACCESS);
    
        if(schService == NULL) 
        {
            MessageBox(NULL, "DEBUG - schService==NULL","DEBUG",MB_OK);
            err=GetLastError();
            switch (err) 
            {
                case ERROR_ACCESS_DENIED:
                    MessageBox(NULL, "Error - Rights error to PortTalk service", "DeviceIoControl", MB_OK);
                    return(0);
                case ERROR_INVALID_NAME:
                    MessageBox(NULL, "Error - Invalid service name", "DeviceIoControl", MB_OK);
                    return(0);
                case ERROR_SERVICE_DOES_NOT_EXIST:
                    InstallPortTalkDriver();
                     break;
                default:
                    sprintf(str,"DEBUG - Error Installing PortTalk Driver, code=0x%x",err);
                    MessageBox(NULL, str, "DeviceIoControl", MB_OK);
                     break;
            }
            return 0;
        }
    
       
        ret = StartService (schService,    /* service identifier */
                            0,             /* number of arguments */
                            NULL);         /* pointer to arguments */
                        
        if (!ret) 
        {
            err = GetLastError();
            if (err != ERROR_SERVICE_ALREADY_RUNNING) 
            {
              MessageBox(NULL, "Error starting PortTalk service", "DeviceIoControl", MB_OK);
              return(0);
            }
        }
    
        /* Close handle to Service Control Manager */
        CloseServiceHandle (schService);
        return(TRUE);
    }
    From the messages I'm getting, OpenService is returning NULL, but when GetLastError() is called, it too returns 0. According to the documentation on OpenService, this should be impossible.

    I've searched the registry for all instances of "PortTalk" and all have been removed. I also deleted the driver in Device Manager. The physical .sys file is still there, but there was never any .inf file or any other driver files. I searched the system folder and didn't find anything related to porttalk except the single .sys file.

    If OpenService would return 0 and then GetLastError would return ERROR_SERVICE_DOES_NOT_EXIST, I could reinstall the driver programatically. It did it once, but had an error message after that. I removed the driver to try and recreate the later error message.

    Anybody have any idea why this is happening?

    Bill
    Last edited by wdolson; October 30th, 2010 at 07:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured