Versioning/Deploying Unmanaged Files


An unmanaged dll can be wrapped in a managed assembly by adding it as a file of a multi-module assembly. Then, it can be deployed and versioned in the same way as managed assemblies. (So, that assembly could contain nothing but metadata and unmanaged code – no managed code, if you prefer. It can also contain multiple unmanaged files in the same assembly.)


If your compiler does not support this directly, you can get this to work by adding that file as a linked managed resource. For example, see Visual Studio’s /linkresource option (if using it for command line compiling).


This is useful in the case where DllImport() is used to access a function in that file. That call should be updated with the new assembly’s info. For example, in the place of “unmanagedfile.dll”, change it to include the display name like this: “unmanagedfile.dll, managedassembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”.


If that unmanaged file needs to be loaded by LoadLibrary() outside of DllImport(), however, it will need to follow the rules of LoadLibrary(), like the usual unmanaged file use outside the CLR. (See Junfeng’s blog for an extra tip regarding that when using v2.)

Comments (4)

  1. Kevin Westhead says:

    Can this be applied to COM ocxs and dlls, or is it just Win32 dlls?

  2. Klaus Klausen says:

    This sounds like a really valuable feature.

    However – I have tried to deploy and load an unmanaged dll as described in the post – but I havn’t been able to make his work.

    The only way I can get my unmanaged.dll packed in an assembly is like this:

    al /v:1.0.0.0 /keyf:pkey.snk /embed:unmanaged.dll /out:managedWrap.dll

    I then linked the managedWrap.dll to my application like this:

    csc /out:console.exe /target:exe /main:console.Class1 /recurse:*.cs /linkres:ManagedWrap.dll

    In my console application code I then tried to import like this:

    [DllImport("unmanaged.dll, ManagedWrap, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5164048f62342e31")]

    public static extern int MyExternFunc();

    When I run my application I get a DllNotFoundException when MyExternFunc() is called.

    Am I doing this wrong – or is this only supported in framework versions other than .NET 1.1?

  3. CipherCraft says:

    Does not work for me either…

    I dragged the unmanaged.dll into my project and set its properties to embed it as a resource.  That much works.

    Now I’ve tried dozens of ways to refer to it using DllImport, but all fail.

    Reading up on AssemblyName, it does not tell of the unmanaged.dll,ManagedWrap way of pointing to the dll in the resources.

    Suzanne, could you tell us more, please?  We’re clearly missing some information here to make this work.

    tia

    PS I’m using .NET 1.1 too.

  4. L says:

    I am really curious, can i use my .NET dll in ordinary ASP like COM dll do?

    regards,

    L

    Beginner Programmer