>This leads me to a conclusion that if driver calls ZwUnloadDriver() on
itself in context of a driver-created thread, its >DrvUnload() will get
invoked in context of another thread.
Unassemble the function, in a quick scan of my Vista VM I don’t see this to
be the case.
It also appears to be a synchronous operation, so if you wait for the thread
you posted in your unload routine I think you’d deadlock (if the I/O manager
would even allow it, that is. What happens if you try to trigger an unload
while you’re already unloading? Don’t know, don’t particularly care.).
It’s complex because it’s not a clearly defined scenario and thus subject to
lots of, "I suspect"s, "I think"s, and "it should"s. Luckily Tim has already
moved on to bigger and better things, so no one needs to spend time
modelling the behavior of the scenario on crufty old versions of the O/S
(though I think I still have a dusty copy of XP SP2 lying around if someone
wanted to check).
-scott
Scott Noone
Software Engineer
OSR Open Systems Resources, Inc.
http://www.osronline.com
wrote in message news:xxxxx@ntdev…
>> I’m not aware of any way for a non-PnP driver to ask for itself to be
>> unloaded.
>
> ZwUnloadDriver()…
>
> This function can be called by anyone, but DrvUnload() gets invoked in
> context of a system thread. This leads me to a conclusion that if driver
> calls ZwUnloadDriver() on itself in context of a driver-created thread,
> its DrvUnload() will get invoked in context of another thread.
>
> Therefore, the solution seems to be pretty easy. Create a thread in
> DriverEntry() that works like:
>
> KeWait( &event,…);
> ZwUnloadDriver(RegPath);
> PsTerminateSystemThread(0);
>
> When your driver wants to unload itself, it can signal the event, so that
> the above lines get executed. In DrvUnload() wait until the “unloader”
> thread gets signaled before you return - by doing so you will ensure
> that driver image cannot get unloaded from RAM until “unloader” thread
> terminates. What is so complex here???
>
> Anton Bassov
>
>