Unloading a Client Module

To unload a client module, the operating system calls the client module's Unload function. See Initializing and Registering a Client Module for more information about how to specify a client module's Unload function during initialization.

A client module's Unload function ensures that the client module is deregistered from the Network Module Registrar (NMR) before the client module is unloaded from system memory. A client module initiates deregistration from the NMR by calling the NmrDeregisterClient function, which it typically calls from its Unload function. A client module must not return from its Unload function until after it has been completely deregistered from the NMR. If the call to NmrDeregisterClient returns STATUS_PENDING, the client module must call the NmrWaitForClientDeregisterComplete function to wait for the deregistration to complete before it returns from its Unload function.

For example:

// Variable containing the handle for the registration
HANDLE ClientHandle;

// Unload function
VOID
  Unload(
    IN PDRIVER_OBJECT DriverObject
    )
{
  NTSTATUS Status;

  // Deregister the client module from the NMR
  Status =
    NmrDeregisterClient(
      ClientHandle
      );

  // Check if pending
  if (Status == STATUS_PENDING)
  {
    // Wait for the deregistration to be completed
    NmrWaitForClientDeregisterComplete(
      ClientHandle
      );
  }

  // An error occurred
  else
  {
    // Handle error
    ...
  }
}

If a client module is registered as a client of multiple Network Programming Interfaces (NPIs), it must call NmrDeregisterClient for each NPI that it supports. If a network module is registered as both a client module and a provider module (that is, it is a client of one NPI and a provider of another NPI), it must call both NmrDeregisterClient and NmrDeregisterProvider.

A network module must wait until all of the deregistrations are complete before returning from its Unload function.

A client module is not required to call NmrDeregisterClient from within its Unload function. For example, in the situation where a client module is a subcomponent of a complex driver, the deregistration of the client module might occur when the client module subcomponent is deactivated. However, in such a situation the driver must still ensure that the client module has been completely deregistered from the NMR before returning from its Unload function.