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:
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.