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
mccozAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.