0

I have two different versions of an unmanaged dll and a manifest for each. In my C# code, I use activation contexts to control which one is used. I'm then using P Invoke to do the actual call. It appears that the creation and activation of the activation context is successful. However, the context seems to be ignored and whichever dll gets called first is used by both.

Can P Invoke be used with SxS? Or have I set this up incorrectly?

My definition for the C function in the C# code is:

[DllImport("MyMath.dll")]
private static extern double Add(double a, double b);

My manifest just has the assembly identity that I added, and the trustInfo that Visual Studio 2010 generated.

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
    <assemblyIdentity name="MyMath.dll"
                   version="1.1.0.0"
                   type="win32"
                   processorArchitecture="x86"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
      <security>
        <requestedPrivileges>
          <requestedExecutionLevel level='asInvoker' uiAccess='false' />
        </requestedPrivileges>
      </security>
    </trustInfo>
</assembly>

Thank you for your help.

-Nick

Nikhil
  • 1,121
  • 2
  • 11
  • 27
  • Have you tried pinvoking LoadLibraryEx("MyMath.dll") after calling ActivateActCtx instead of using [DllImport]? Just a guess but I think [DllImport] doesn't release loaded libraries (and thus the activation context would seem to be ignored on 2nd call). – NtscCobalt May 30 '12 at 20:37
  • Once a DLL gets loaded, Windows stops looking for another one with the same name. Simply renaming the DLL solves the problem. – Hans Passant May 31 '12 at 01:13
  • @NtscCobalt it seems that LoadLibrary if a lot like System.Reflection, in that I need to create instances in odd ways, instead of using it as if it's linked in. Is that correct? I think I must be doing something wrong with my manifests or initialization, since I'm getting the same behavior if i use a managed C++ binding. – Nikhil Jun 05 '12 at 15:56
  • 1
    @HansPassant That's the problem that SxS is supposed to fix. Unfortunately, renaming the dlls is not an option. – Nikhil Jun 05 '12 at 15:57
  • @Nikhil If you are using [DllImport] in C++/CLI then you will probably have the same issue. Again the problem is likely the implementation of [DllImport] which probably calls `LoadLibraryEx()` but never calls `FreeLibrary()`. Dlls are reference counted by the system and extra calls to a dll that is already loaded just return the currently loaded dll. I highly doubt that [DllImport] only loads the dll for the lifetime of the call. It is far more likely that it loads it on first use and then keeps the module handle until the application exits. Nothing you can do except write your own DLL loading – NtscCobalt Jun 05 '12 at 16:49
  • @Nikhil You shouldn't have to do any strange initilization. If it is a C/C++ object that is being returned from the call into the native dll you can use http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.ptrtostructure to create a .Net struct (marked with appropriate [StructLayoutAttribute]) from the returned C/C++ pointer. – NtscCobalt Jun 05 '12 at 16:53

0 Answers0