• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

What can be wrong with Paths in a DLL?

Hi,

I am trying to use giveio.sys to access ports under win2k. But when I try to open the driver with CreateFile() I already get an error.
That's the code:
h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(h == INVALID_HANDLE_VALUE) {
MessageBox(0,"Couldn't access giveio device","Error",MB_ICONHAND);
}
CloseHandle(h);

If I use GetLastError() I get an 2 or 123 error, i.e. something with the filename ist wrong.
Really stange is, that this code works fine in a Dos Box application but when I plug it into my DLL, it doesn't!

This is probably a very simple problem but I really have no clue what's wrong with the path.

Thanks,
  mccoz
0
mccoz
Asked:
mccoz
  • 5
  • 2
  • 2
  • +1
1 Solution
 
jkrCommented:
You need to start and load the driver before using 'CreateFile()', otherwise the name is not present in the kernel namespace:

/****************************************************************************
*
*    FUNCTION: StartDriver( IN SC_HANDLE, IN LPCTSTR)
*
*    PURPOSE: Starts the driver service.
*
****************************************************************************/
BOOL StartDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName )
{
    SC_HANDLE  schService;
    BOOL       ret;

    schService = OpenService( SchSCManager,
                              DriverName,
                              SERVICE_ALL_ACCESS
                              );
    if ( schService == NULL )
        return FALSE;

    ret = StartService( schService, 0, NULL )
       || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING
         || GetLastError() == ERROR_SERVICE_DISABLED;

    CloseServiceHandle( schService );

    return ret;
}



/****************************************************************************
*
*    FUNCTION: OpenDevice( IN LPCTSTR, HANDLE *)
*
*    PURPOSE: Opens the device and returns a handle if desired.
*
****************************************************************************/
BOOL OpenDevice( IN LPCTSTR DriverName, HANDLE * lphDevice )
{
    TCHAR    completeDeviceName[64];
    HANDLE   hDevice;

    //
    // Create a \\.\XXX device name that CreateFile can use
    //
    // NOTE: We're making an assumption here that the driver
    //       has created a symbolic link using it's own name
    //       (i.e. if the driver has the name "XXX" we assume
    //       that it used IoCreateSymbolicLink to create a
    //       symbolic link "\DosDevices\XXX". Usually, there
    //       is this understanding between related apps/drivers.
    //
    //       An application might also peruse the DEVICEMAP
    //       section of the registry, or use the QueryDosDevice
    //       API to enumerate the existing symbolic links in the
    //       system.
    //

      if( GetVersion() & 0xFF >= 5 ) {

            //
            // We reference the global name so that the application can
            // be executed in Terminal Services sessions on Win2K
            //
            wsprintf( completeDeviceName, TEXT("\\\\.\\Global\\%s"), DriverName );

      } else {

            wsprintf( completeDeviceName, TEXT("\\\\.\\%s"), DriverName );
      }
    hDevice = CreateFile( completeDeviceName,
                          GENERIC_READ | GENERIC_WRITE,
                          0,
                          NULL,
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL
                          );
    if ( hDevice == ((HANDLE)-1) )
        return FALSE;

      // If user wants handle, give it to them.  Otherwise, just close it.
      if ( lphDevice )
            *lphDevice = hDevice;
      else
          CloseHandle( hDevice );

    return TRUE;
}

The SCM handle can be obtained using
      SC_HANDLE      schSCManager;

      schSCManager = OpenSCManager(      NULL,                 // machine (NULL == local)
                                          NULL,                 // database (NULL == default)
                                                      SC_MANAGER_ALL_ACCESS // access required
                                                );
0
 
mccozAuthor Commented:
Thanks for the answer, I'll try that!

But why does it work in case I use it in the DOS box application?
0
 
moduloCommented:
PAQed with points refunded (100)

modulo
Community Support Moderator
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
Err, what? Why a refund? The answer given was exactly what it takes to make that work...
0
 
jkrCommented:
Actually, three. Sorry, but this time there was a backlog of about 50 notif mails, so I probably missed to check this one, thinking that my initial comment would be considered 'OK' anyway.
0
 
tinchosCommented:
Hi

Sorry about the delay......

Just for the record, I recommended a PAQ/Refund, because as modulo said, I consider the question abandoned by the expert, as the asker continue to have doubts

Cheers

Tincho
0
 
jkrCommented:
Well, I was waiting for the result of " I'll try that!" :o)
0
 
tinchosCommented:
Sorry jkr

I meant because of the

"But why does it work in case I use it in the DOS box application?"

:)

PS: I'll also join your queue
0
 
jkrCommented:
Nah, my fault in the 1st place. I should have added some response when tinchos posted the suggestion announcement (failed to notice that one, though).

But, if you also are thinking that this is one great chance to grab a thread in the C++ area to just chat and gossip until we get an answer, I'm with you *g*
0
 
mccozAuthor Commented:
Hi guys,

sorry, I didn't notice there were new post for me any earlier.

At that time, I spent a good amount of time trying to get my code work by using the suggestion from jkr but I gave up on it later. I thought there must be an easier way when it works in case I start the exact same code from within a DOS box application. So I hoped for an answer for that question. Since my time I had for that task was used up I hoped for a quick and dirty answer why it works in the DOS case.

I don't know what the correct behaviour is in this case: Should I accept the answer and give the credits? It is a kind of my fault since I couldn't get it to work and I couldn't afford spending more time on it and discussing it further.

Thus, if you think the correct action for me is to give you the credits I definitely would be willing to do that. (Then we have to find a way how to transfer the credits...)

Sorry again for any inconvenience!
  mccoz
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now