CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  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.

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: OpenService returns Null but GetLastError also returns Null

    You probably missed some detail, like how you didn't notice that the code tags didn't work.

    Code:
    schService = OpenService(SchSCManager,
    "PortTalk",
    SERVICE_ALL_ACCESS);
    
    if(schService == NULL)
    {
            MessageBox(NULL, "DEBUG - schService==NULL","DEBUG",MB_OK);
            err=GetLastError();
    Here you're getting the last error for the MessageBox call, not the OpenService call. Put the GetLastError before the MessageBox call.

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

    Re: OpenService returns Null but GetLastError also returns Null

    I guess I earned the dunce cap today

    Note to self: don't post when you've been staring at code 9 hours straight.

    Thanks for pointing it out.

    Bill

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

    Another Issue Starting the Driver...

    EDIT: I found it. Need to do OpenService again after installing the driver.

    Now that I fixed my stupid mistake I'm back to the original problem. When the driver is installed, everything works correctly, but if the driver is not installed when the program starts and the program programatically installs the driver, an error pops up that says "Can't start PortTalk Driver", but shutting down the program and restarting clears the problem and there are no further errors.

    Here is the code that calls the code I posted earlier:

    Code:
            
    PortTalk_Handle = CreateFile("\\\\.\\PortTalk", 
                               GENERIC_READ, 
                               0, 
                               NULL,
                               OPEN_EXISTING, 
                               FILE_ATTRIBUTE_NORMAL, 
                               NULL);
    
    if(PortTalk_Handle == INVALID_HANDLE_VALUE) 
    {
               /* Start or Install PortTalk Driver */
               StartPortTalkDriver();
    
               /* Then try to open once more, before failing */
    PortTalk_Handle = CreateFile("\\\\.\\PortTalk",
           GENERIC_READ,0,NULL,
           OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
                   
           if(PortTalk_Handle == INVALID_HANDLE_VALUE) 
          {
                   MessageBox(NULL, "Can't access PortTalk Driver", 
                        "DeviceIoControl", MB_OK);
                    return (char)-1; //WDO 6/23/10 Add cast
          }
    }
    When the driver is installed, the second call to CreateFile fails and I get the error message. I doublechecked StartPortTalkDriver and InstallPortTalkDriver and the last call in both before returning is CloseServiceHandle, so there should be no open handle when CreateFile is called.

    It can be worked around by shutting down the program and restarting, but that isn't a very clean way to do it. Why won't the second call to CreateFile work only when the driver is installed and never any other time?

    I did verify that the service is not running on boot and the first run of the program does call StartPortTalkDriver. The problem only occurs when InstallPortTalkDriver is called.

    Here is the relevant portion of InstallPortTalkDriver:

    Code:
    /* Open Handle to Service Control Manager */
    SchSCManager = OpenSCManager (NULL, 
                                NULL,         
                                SC_MANAGER_ALL_ACCESS); 
    
    /* Create Service/Driver - This adds the appropriate registry keys in */
    /* HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services - It doesn't  */
    /* care if the driver exists, or if the path is correct.              */
    
    schService = CreateService (SchSCManager,
                         "PortTalk",
                         "PortTalk",
                         SERVICE_ALL_ACCESS, 
                         SERVICE_KERNEL_DRIVER,
                         SERVICE_DEMAND_START, 
                         SERVICE_ERROR_NORMAL,
                         "System32\\Drivers\\PortTalk.sys",
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL);
    
    if (schService == NULL) 
    {
          err = GetLastError();
          if (err == ERROR_SERVICE_EXISTS)
          {
     MessageBox(NULL, "PortTalk driver install: Attempt to install driver failed", "DeviceIoControl", MB_OK);
          }
          else  
    MessageBox(NULL, "PortTalk driver install: Unknown error installing the driver", "DeviceIoControl", MB_OK);
    }
    
    /* Close Handle to Service Control Manager */
    CloseServiceHandle (schService);
    Last edited by wdolson; October 31st, 2010 at 01:13 AM.

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