Link to home
Start Free TrialLog in
Avatar of faster
faster

asked on

DeviceIoControl

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.
Avatar of chensu
chensu
Flag of Canada image

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.

Avatar of faster
faster

ASKER

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?

Avatar of faster

ASKER

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:
\HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\InstalledFiles.
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!

Avatar of faster

ASKER

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.

   FARPROC p;
   _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;
notfound:
   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)
Avatar of faster

ASKER


ASKER CERTIFIED SOLUTION
Avatar of brien
brien

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of faster

ASKER

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.