简体   繁体   中英

Referencing a .net dll + unmanaged dll

I have a project (call it platform) that references a .net dll (call it instrument-proxy), that reference a C++ dll using DllImport (call it instrument). The instrument and it's proxy are always the same version and are always deployed as one.

I need to be able to deploy several instruments (with their proxies) on the one machine, and only one platform that uses specific version of the instrument.

It seems like I cannot place an insturent-proxy to the GAC, as it uses a C++ DLL. I can install an instrument (and it's proxy) to a specific folder, but how can I ensure that platform will find that dll?

Upd.

The idea that theoretically I would like to see, is to reference this dll while development as normal, but to be able to make my application search for this dll in the custom folder.

Keeping them in separate folders would be fine. You would load the right instrument-proxy assembly yourself using Assembly.LoadFrom . Then use reflection to create instance of a class from that assembly and make a call:

Assembly assm = Assembly.LoadFrom("c:\\Versions\\Version01\\instrument-proxy.dll");
Type yourClassType = assm.GetType("YourClass");
object yourClassObj = Activator.CreateInstance(yourClassType);
object Result = yourClassType.InvokeMember("DoSomething",
                                            BindingFlags.Default | BindingFlags.InvokeMethod,
                                            null,
                                            yourClassObj,
                                            args);

To avoid reflection call InvokeMember you could try with interface:

Assembly assm = Assembly.LoadFrom("c:\\Versions\\Version01\\instrument-proxy.dll");
Type yourClassType = assm.GetType("YourClass");
YourInterface interf = (YourInterface)Activator.CreateInstance(yourClassType);
interf.DoSomething();

Interface would need to be in a separate assembly which you could reference from you platform . All instrument-proxies would have to be compiled against the same version of the interface assembly.

If your instrument-proxy can't find unmanaged Instrument.dll you can load it explicitly before first use:

[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);

IntPtr pDll = LoadLibrary(@"PathTo_Instrument.dll");

Free/unload it after you're done:

[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);

You can copy the unmanaged dll files to a folder and then add that folder to System Path Variable or you can copy these files to Windows\\System32 , as this folder is always in the System Path.

To modify system path, Right click on my computer -> Properties -> Advance System settings -> Under Advance tab -> Environment Variables, there you need to find the path variable and then modify it to include your file.

Copying file to Windows\\System32 or %systemroot% (C:\\Windows usually) is easier though but I would recommend using the approach with Path variable modification

对于.Net程序集,您可以订阅AppDomain.AssemblyResolve并在需要时使用Assembly.LoadFrom加载程序集(请参阅https://stackoverflow.com/a/10120664/143503 )。

Like Maceij suggests above, deploy your instruments in separate folders. But instead of using reflection to load/instantiate the instruments, have a look at MEF.

Your exact needs may require another way to do this, but I would let the instrument-proxy [Export] an IInstrument factory interface with the following method:

IInstrument GetInstrument( string name, int major, int minor );

Then tell MEF where to find your instruments and let it create the different factories from which you then can request the instrument you need.

MSDN on MEF: http://msdn.microsoft.com/en-us/library/dd460648.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM