Query.dll throws Access Violation during ExitProcess

If you have linked against query.dll (e.g. to use the LoadIFilter API), your app may throw an access violation during ExitProcess when your process is finishing.
 
A workaround for this (rather nasty) access violation is to call the undocumented CIShutdown method in the shutdown code of your app, e.g. right before CoUnitialize.
 

typedef void (__stdcall CIShutdownFn)();

void

CIShutdown()

{

  HMODULE hModule = LoadLibrary(_T("query.dll"));

  if (NULL != hModule)

  {

    CIShutdownFn *fn = (CIShutdownFn*)GetProcAddress(hModule, _T("?CIShutdown@@YGXXZ"));

    if (NULL != fn)

      fn();

    FreeLibrary(hModule);

  }

}

 
This rather indirect call is necessary because CIShutdown is not defined in ntqery.h/ntquery.lib.
 
The IFilter test utility filtdump.exe seems to do the very same thing, at least from looking at the output of Dependency Walker (Depends.exe).
 
Please note: Code provided AS IS without warranties of any kind. Use at your own risk. Your mileage may vary. May break with any version of Windows, Index Server or MS Search based application.
 
As a side note, LoadIFilter already calls IPersistFile::Load on the selected filter, so you don’t need to.
Advertisements
This entry was posted in Coding Horror. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s