Welcome to MSDN Blogs Sign in | Join | Help

张羿的MSDN Blog

All about CLR/Interop, COM, Win32, C++

Syndication

How to deploy unmanaged dll into GAC

Let's say you have a managed wrapper assembly Managed.dll that calls unmanaged code Unmanaged.dll using P/Invoke. If you want to register Managed.dll to GAC, you'll need to deploy Unmanaged.dll into GAC as well, otherwise it would require extra work to be done so that Managed.dll can find Unmanaged.dll. To deploy both Manage.dll & Unmanaged.dll into GAC, you can create a multi-file assembly consisting both Managed.dll & Unmanaged.dll. In order to create such a assembly, you can:

1. Use /LinkResource option in C# compiler (CSC.EXE) if you are using C#
2. Use /AssemblyLinkResource
option in C++ compiler (CL.EXE) if you are using managed C++
3. Use /Link option in Assembly Linker (AL.EXE)

Let's take C# as example. You can type the following to create a multi-file assembly:


CSC /platform:<target_platform> Managed.cs /LinkResource:Unmanaged.dll /keyfile:<your_key_file>

Please note that you’ll have to specify the same platform as Unmanaged.dll. This makes sense because they are considered the same assembly and will run under same architecture.

The output assembly will consist of both Managed.dll & Unmanaged.dll.

Later if you try to register Managed.dll using gacutil:


gacutil /i managed.dll

It will deploy both managed.dll & unmanaged.dll into GAC. Since managed.dll & unmanaged.dll are in the same directory, P/Invoke works as usual.

 

Published Sunday, June 17, 2007 10:07 AM by yzha

Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Cómo desplegar DLL no manejadas en la GAC @ Monday, June 18, 2007 4:09 AM

Interesante artículo o entrada escrita en inglés en un Blog de MSDN en la que nos indican cómo podríamos

Jorge Serrano - MVP Visual Developer - Visual Basic

# re: How to deploy unmanaged dll into GAC @ Saturday, June 30, 2007 10:17 AM

Getting unmanaged dll GACed is good for performance?

buddhist

# re: How to deploy unmanaged dll into GAC @ Saturday, June 30, 2007 10:41 AM

Putting unmanaged DLL in GAC is for simpler deployment in some scenarios. Imagine if you have a managed A.dll call unmanaged B.dll, and you want to put managed A.dll into GAC as a shared component. If you don't put the unmanaged B.dll into GAC as well, A.dll will have trouble finding B.dll, and A.dll will be broken.  

yzha

# re: How to deploy unmanaged dll into GAC @ Saturday, June 30, 2007 11:30 AM

Thanks.

In other words, you meant that if a managed dll is in GAC, all its dependencies should be in GAC too, right? And that is not for purpose of performance.

buddhist

# re: How to deploy unmanaged dll into GAC @ Saturday, June 30, 2007 11:55 AM

That depends on what kind of dependencies you are referring to. If you register A.dll, all the other DLLs (modules) within the same assembly as A.dll will be put into GAC in the same place. Other assemblies won't be affected, even though A might be calling them at runtime.

yzha

# re: How to deploy unmanaged dll into GAC @ Saturday, June 30, 2007 10:12 PM

I'm confused with you last words - "even though A might be calling them at runtime."

But you mentioned above that if a managed A.dll call unmamaged B.dll, then both should be put into GAC.

Did I miss sth?

buddhist

# re: How to deploy unmanaged dll into GAC @ Sunday, July 01, 2007 5:28 AM

The difference is that A.dll & B.dll are in the *same* mult-file assembly. Suppose A.dll also calls *another* assembly C at runtime (we can say A.dll have a dependency on C). Registering A.dll will put both A.dll & B.dll into GAC because they are within the same assembly, but not C, since C is a different assembly.

yzha

# re: How to deploy unmanaged dll into GAC @ Sunday, July 01, 2007 10:38 AM

Got you!

multi-file assembly, that's what I'm missed.

buddhist

# Where to put those DLLs - deployment best practices @ Sunday, November 09, 2008 12:16 PM

A friend asked me last week what is the optimal solution to deal with application version upgrades. He

Shahar Ron

# How to deploy using WIX @ Friday, January 16, 2009 4:06 AM

In case anybody wants to know. I spent some time trying to find out how to deploy such a multi-file assembly using WIX (or Windows Installer) in the GAC. Apparently, you should create a single component and put both your .NET assembly and your native assembly in this component. And that's apparently all you need to do.

E.g.

<Directory Id="GAC" SourceName="Global Assembly Cache">

       <Component Id="Assembly.dll_GAC" Guid="{6DA8FA85-0D2B-46ee-AD7A-19284D6BDF4A}">

         <File Id="Assembly.dll_GAC" Name="Assembly.dll" KeyPath="yes" Checksum="yes" Assembly=".net" AssemblyManifest="Assembly.dll_GAC" DiskId="1" Source="Assembly.dll" />

         <File Id="Unmanaged_GAC" Name="Unmanaged.dll" DiskId="1" Source="Unmanaged.dll" />

       </Component>

</Directory>

Michael Vanhoutte

# re: How to deploy unmanaged dll into GAC @ Tuesday, July 14, 2009 7:07 PM

Thanks a lot, this is exactly what I was looking for!

Chadwick Tuteaux

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
Page view tracker