locked
Calling unmanaged dll (installed in WinSxS) from C# RRS feed

  • Question


  • Hi all,

    I call an unmanaged dll from my C# assembly using DllImport. So far this works just fine if both dlls are located in the same directory. Now I want to install the unmanaged dll into the WinSxS folder. But in order to use the unmanaged dll once it is installed there I need a manifest for my C# assembly that specifies the version of the unmanaged dll that shall be used.

    I have seen how this works for C++ assemblies. All C++ assemblies that are compiled with Visual Studio 2005 / 2008 have an embedded manifest that specifies the C++ runtime (installed in the WinSxS folder) that shall be used. Such a manifest looks like this:

    <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
    <assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel level='asInvoker' uiAccess='false' />
          </requestedPrivileges>
        </security>
      </trustInfo>
      <dependency>
        <dependentAssembly>
          <assemblyIdentity type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
        </dependentAssembly>
      </dependency>
    </assembly>

    Now it is not clear to me if I can do the same for C# assemblies. Visual Studio C++ projects offer a manifest by default in the project options. However, C# projects don't (as far as I know). I've tried mt.exe to embed the manifest into the C# assembly (have to embed it because the assembly shall be installed into the Global Assembly Cache) but somehow the unmanaged dll is not loaded from the WinSxS folder (system.dllnotfoundexception). I checked my managed dll using dependey walker and it does not show a reference to my unmanaged dll. This is different for C++ assemblies where I have seen references to unmanaged dlls.

    Is it possible, can this approach work or do I have to write a managed C++ wrapper for my unmanaged dll?


    Thanks!

    Monday, October 13, 2008 2:49 PM

Answers

  •  

    Hello,

           I found this connect record on Microsoft Connect Center.         https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97418&wa=wsignin1.   We will have some issue here when using P/Invoke to call your unmanaged DLL with manifest information in WinSxS folder.  

           I could recommend you tow methods here.  

           One is still using DllImport here, however, if you want to install the unmanaged C++ DLL in WinSxS folder.   I think you could just define the function like the following codes does: 


    -------------------------------------------------------------------
    [DllImport(@"C:\WINDOWS\WinSxS\unmanaged.dll", SetLastError = true)]

    static extern string Method(string param1, string param2);
    -------------------------------------------------------------------      

          Here we define the absolute path of the DLL file, then DllImport will find the file correctly as long the file exists in the folder.   Or you could set the system path environment, add “C:\WINDOWS\WinSxS” into it.   (Right Click “My Computer”
    à “Advanced” à “Environment Variables” à “System variables” à “Path”).   For detail, please check Dynamic-Link Library Search Order.

         

           Another way, I think you could use P/Invoke to call LoadLibrary Windows API to directly load the unmanaged C++ DLL in C# application.   For additional information, you could have a check on this popular thread: DLLImport – using dynamic file path.  

     

            Thanks,

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Thursday, October 16, 2008 12:00 PM
    Moderator

All replies

  •  

    Hello,

           I found this connect record on Microsoft Connect Center.         https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97418&wa=wsignin1.   We will have some issue here when using P/Invoke to call your unmanaged DLL with manifest information in WinSxS folder.  

           I could recommend you tow methods here.  

           One is still using DllImport here, however, if you want to install the unmanaged C++ DLL in WinSxS folder.   I think you could just define the function like the following codes does: 


    -------------------------------------------------------------------
    [DllImport(@"C:\WINDOWS\WinSxS\unmanaged.dll", SetLastError = true)]

    static extern string Method(string param1, string param2);
    -------------------------------------------------------------------      

          Here we define the absolute path of the DLL file, then DllImport will find the file correctly as long the file exists in the folder.   Or you could set the system path environment, add “C:\WINDOWS\WinSxS” into it.   (Right Click “My Computer”
    à “Advanced” à “Environment Variables” à “System variables” à “Path”).   For detail, please check Dynamic-Link Library Search Order.

         

           Another way, I think you could use P/Invoke to call LoadLibrary Windows API to directly load the unmanaged C++ DLL in C# application.   For additional information, you could have a check on this popular thread: DLLImport – using dynamic file path.  

     

            Thanks,

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Thursday, October 16, 2008 12:00 PM
    Moderator
  • Following Error occured while adding dll...

    ..

     

     

    A reference to the "....dll" could not be added.Please make sure that the file is accessible and that it is a valid assembly or COM component.

    .

    .

    Plz help me

    Wednesday, September 22, 2010 10:49 AM