LoadLibrary sometimes fails with error 14.

  • Thread starter Thread starter Robert Fridén
  • Start date Start date
R

Robert Fridén

I found the following post on google groups and I have the exact same
problem as him. His workaround also works for me, but It's not really an
option in my case, since I need to be able to write plugins for my
application (loaded using Assembly.Load) which in turn need to use DllImport
to access functions in native libraries.

Does anyone know the real cause for this odd behaviour? Does anyone know how
it can be fixed properly?

// Robert Fridén

From:Shaun Wilde ([email protected])
Subject:MissingMethodException and PInvoke/DllImport - bug?
News groups:microsoft.public.dotnet.framework.compactframework
Date:2003-08-12 11:42:25 PST
 
This is a known issue in .NET CF v1.0. We believe that it is due to the
fact that CF does not unload P/Invoke'd dlls once they are loaded, but have
not been able to prove this conclusively. This would account for the dll
loading when the app is first launched, but failing to load later.

If this error occurs, it might be possible to unload unneeded modules by
P/Invoking to GetModuleHandle and FreeLibrary, then try the P/Invoke again.

try
{
MyPInvoke();
}
catch (Exception x)
}
string moduleName = "ModuleToUnload.dll";
IntPtr hModule = GetModuleHandle(moduleName);
if (IntPtr.Zero != hModule)
{
FreeLibrary(hModule);
}
}

HTH,
Dan

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top