I have a mouse driver, and an application that needs to send some information to the driver.  I use DeviceIoControl to do this. It works fine when the driver was installed.  However, when the driver is removed and the application calls CreateFile (in order to get the handle used by DeviceIoControl), it crashes.  I think it is due to the initialisation code of the driver which conflicts with the current mouse driver.  How shall I do with this problem?  Is there a way to determine whether a driver is being loaded by device manager of by the "CreateFile" API from the application?  Or how shall the application detects that the correct driver has not been loaded?

Thanks in advance.
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.

How shall the application detects that the correct driver has not been loaded?

Call GetModuleHandle()

e.g., GetModuleHandle("mouse")

You can run HeapWalk to check the module name.

fasterAuthor Commented:
I am sorry, maybe I have not put my question clear enough.  My problem is that my application does not know whether the VXD is loaded or not, therefore, upon calling CreateFile, it will triger the initialisation code of the vxd, and therefore conflicts with the current mouse driver.  Calling "GetModuleHandle" does not seem to let the app know what mouse driver is currently running.
Actually, I do not understand your question very clearly. Why do you need two mouse drivers? If the initialization code of the driver conflicts with the current mouse driver, how was it installed?

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

fasterAuthor Commented:
Thank for your kind help.   Let me explain it more clearly.

I have a mouse minidriver, say "mymouse.vxd", which needs to communicate with my exe "myexe.exe".  This is needed because the vxd alone can not get all the job done and therefore it will send a message to myexe.exe so that the exe can take care of the rest.  In order to do this, I need to inform the vxd the handle of the window (belongs to the exe).  I use DeviceIoControl to do this.

When my driver (including the vxd and exe, the exe will load automatically) is installed, it works fine.  However, if the driver is removed, my vxd is still in the hard disk, and the exe is still running (the exe does not know that user has changed the driver).  The exe will call CreateFile, however, the PC crashes at this stage.  I guess this is because it cause my vxd intiialising and running again, then there are two mouse vxds and the system crashed.

So what I need is: let the exe detect that my vxd is not loaded, or: my driver has not been installed, or the vxd can know it is loaded by the exe, not by win95 itself.

Any suggestions?

Thanks again!

To check whether a VXD is installed, you can call Win32 Registry functions to access the Registry of Windows 95:
For example, if mymouse.vxd is installed, there should be a key 'mymouse.vxd'. (You may run Registry Editor to see it.) It should work although I have never tried. Good Luck!

fasterAuthor Commented:
I am afraid this does not help.  All the drivers that have been installed will appear there, no matter whether it is currently being used.

 There are a few methods I can think to try:

  First, pass a Window handle to the VxD of the application that is interested in the VxD.  When the VxD exits, it can call service SHELL_PostMessage to that Window and inform it that it is gone.  

  If you want all applications to get the message, then the VxD can invoke PostMessage and send a message that your application can trap on.  This requires the VxD locate the address of the PostMessage entry point in the system VM, and may require a helper driver.

  If your VxD has a protected mode entry point that allows Ring 3 applications access, you can use the following snippet of code to retrieve the entry point.  If the entry is not found, it will return a 0 and you can assume the VxD is not loaded.  You may even want to include a protected mode entry point if only for this purpose.

   _asm {
          push ds
          mov  ax,168Ah
          lds  si,pIDString
          xor  di,di
          mov  es,di
          int  2Fh
          pop  ds
          cmp  al,0
          jne  short notFound
          mov  word ptr [p],di
          mov  word ptr [p+2],es
   return p;
   return 0;

 in the code example,  pIDString is a char far* that is the ID string of the VxD.

  A combination of the above methods might be a good idea.  They would cover every concievable situation ie
  1) VxD removed in the middle of app execution (postmessage method, or ShellPostMessage)
  2) VxD not loaded when app starts (retrieving PM entry point method)
fasterAuthor Commented:

Gees, lots of rejections...  here's the right answer:

CreateFile("\\\\.\\mymouse", ...) will fail if the driver is not already in memory.  Use this if you want to get a handle to the device ONLY if it is already loaded.

CreateFile("\\\\.\\mymouse.vxd", ...) will load the driver if it is not already loaded into memory.  With this form you may use an absolute path such as CreateFile("\\\\.\\c:\\myapp\\mymouse.vxd", ...).

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
fasterAuthor Commented:
I am sorry to say that your answer is too late and I actually don't need it anymore since I already have my own solution.  Besides, now I don't have the environment to test your answer.  However, if your statement is true, it should solve my problem.  Thanks anyway.

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
Microsoft Development

From novice to tech pro — start learning today.