Now we talk about how UG/NX load a .net plugin dll.
As far as we know, NX run without .net framework, i.e., no need setup .net framework on the PC to use NX.
But, why NX can also load .net dll. (Of cource, who use .net plugin should setup .net framework by selves).
As I known, a native C/C++ program to load a .net program could use CLI, COM, and API methods. Others are between processes.
Obviously, NX didn't use CLI nor COM, because of both methods should prepare .net framework on PC.
API is the short of CLR Hodting APIs, some COM components to load .net program.
API is different with .net versions, especially .net4 having in process side by side which let it be reprogram. (The usage of .net API is not the businee of this post and I would post another article latter).
Conclusion, NX load the customer .net dll with ManagedLoader.dll by .net hosing API.
ManagedLoader.dll is a key component.
Let us have a look for it.
From the Fig., we see a interface and a class which is a implementation of the former.
The interface realiezed how to load a .net dll, and run their methods.
We pay attension to Load(string), for it is take charge for loading the customer dll. So, let's look:
1: public string Load(string assemblyName)
2: {
3: string str = null;
4: this.myAssemblyName = assemblyName;
5: Trace.WriteLine(string.Concat(new object[] { "ManagedLoader.Load: ", assemblyName, " ", AppDomain.CurrentDomain }));
6: Trace.WriteLine("AppBase: " + AppDomain.CurrentDomain.BaseDirectory);
7: try
8: {
9: AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(this.AssemblyResolveHandler);
10: AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(this.AssemblyLoadHandler);
11: this.myAssembly = Assembly.LoadFrom(assemblyName);
12: }
13: catch (Exception exception)
14: {
15: Trace.WriteLine(exception.ToString());
16: str = exception.ToString();
17: }
18: return str;
19: }
20:
21:
Line 9, mainly correspond to loading dll successfully event. There is nothing, only Trace information for debugging.
Line 10, deal with loading the dependent dlls, very importantly, details latter.
Line 11, load customer .net dll. Note, it use LoadForm, not LoadFile, there is differen between them.
In addition, reflection have some variation in .net4, you could look for MSDN.
What are dependent dlls?
Different from native dlls which could be found form the Path directory. .net dll only could put donw in exe path or GAC.
That's the problem. The main process of UG/NX is ugraf.exe, at program files/UGS… . It could not ask put all customer dlls in there, so there must be some place for users.
Except the path of the customer dll, NX allows the path speciafied by the system variable UGII_MANAGED_DLL_PATH to put down.
The Event AssemblyResolve deal with searching these dependent dlls, you can look:http://msdn.microsoft.com/zh-cn/library/system.appdomain.assemblyresolve(v=vs.80).aspx
Now, let's see the implementation of above line 10 function.
1: private Assembly AssemblyResolveHandler(object sender, ResolveEventArgs args)
2: {
3: Trace.WriteLine("Resolve failed: " + args.Name);
4: int index = args.Name.IndexOf(',');
5: string str = args.Name.Substring(0, index);
6: if ((!(str == "NXOpen") && !(str == "NXOpen.Utilities")) && (!(str == "NXOpenUI") && !(str == "NXOpen.UF")))
7: {
8: return null;
9: }
10: str = str + ".dll";
11: string str2 = TranslateVariable("UGII_MANAGED_DLL_PATH");
12: string path = null;
13: if (str2 != null)
14: {
15: foreach (string str4 in str2.Split(new char[] { Path.PathSeparator }))
16: {
17: path = Path.Combine(str4, str);
18: if (File.Exists(path))
19: {
20: break;
21: }
22: path = null;
23: }
24: }
25: if (path == null)
26: {
27: str2 = TranslateVariable("UGII_ROOT_DIR");
28: if (str2 != null)
29: {
30: path = Path.Combine(Path.Combine(str2, "managed"), str);
31: }
32: }
33: Assembly assembly = Assembly.LoadFrom(path);
34: if (assembly == null)
35: {
36: Trace.WriteLine("Failed to load: " + str);
37: return assembly;
38: }
39: Trace.WriteLine("Loaded: : " + assembly.GetName(false).ToString());
40: return assembly;
41: }
4-5, acquire the name of a dependent dll.
6-9, exclude 4 dlls laoding automatically with ManagedLoader.dll.
10-24, search them in the path speciafied by UGII_MANAGED_DLL_PATH.
25-32, search them in the path specialfied by UGII_ROOR_DIR which is the root of UG/NX.
Line 33, load the dependent dll.
34-38, deal with errors.
Ohters are searched form the path of the customer plugin, which is automatically realized by reflection of .net.
OK, that's all.
About .net API usage, we talk about latter.
All rights 白途思(Beck)
2011-03-01
No comments:
Post a Comment