{"id":10491,"date":"2006-04-25T12:18:38","date_gmt":"2006-04-25T12:18:38","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/dynamically-loading-assemblies-in-mshsnapins\/"},"modified":"2019-02-18T13:24:52","modified_gmt":"2019-02-18T20:24:52","slug":"dynamically-loading-assemblies-in-mshsnapins","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/dynamically-loading-assemblies-in-mshsnapins\/","title":{"rendered":"Dynamically loading assemblies in mshsnapins."},"content":{"rendered":"<p>In Monad, mshsnapins can be installed in a different directory from the Monad executables. At runtime, the Monad engine will load the mshsnapin assembly and its referenced assemblies based on application base information&nbsp;provided during&nbsp;mshsnapin installation. <\/p>\n<p>If an mshsnapin itself dynamically loads an assembly through an assembly name or a relative path, however, CLR may not be able to locate the assembly.&nbsp; This is&nbsp;because the CLR runtime knows nothing about the mshsnapin installation directory and thus will not search there&nbsp;during dynamic assembly resolution.&nbsp;<\/p>\n<p>The following is a&nbsp;common scenario for this: &nbsp;Assume&nbsp;that an mshsnapin named &#8220;mysnapin&#8221; is installed in the c:\\mysnapin directory and the monad executable is installed in c:\\monad\\ directory. Also assume that cmdlet &#8220;my-command&#8221; in &#8220;mysnapin&#8221; will dynamically load another assembly like this, (assume mylibrary.dll sits in the same directory as &#8220;mysnapin&#8221; assembly)<\/p>\n<blockquote>\n<p>Assembly.LoadFrom(&#8220;mylibrary.dll&#8221;);<\/p>\n<\/blockquote>\n<p dir=\"ltr\">If you run &#8220;my-command&#8221; in msh.exe, you will get an error indicating that mylibrary.dll can&#8217;t be found. The reason for this is that CLR runtime only searchs through the&nbsp;GAC and the currently executable directory (which is installation directory for msh.exe) for assembly resolution. <\/p>\n<p dir=\"ltr\">If you want to dynamically load an assembly in your mshsnapin, always provide an absolute path unless the assembly itself is in GAC. <\/p>\n<p dir=\"ltr\">-George[MSFT]<\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Monad, mshsnapins can be installed in a different directory from the Monad executables. At runtime, the Monad engine will load the mshsnapin assembly and its referenced assemblies based on application base information&nbsp;provided during&nbsp;mshsnapin installation. If an mshsnapin itself dynamically loads an assembly through an assembly name or a relative path, however, CLR may not [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>In Monad, mshsnapins can be installed in a different directory from the Monad executables. At runtime, the Monad engine will load the mshsnapin assembly and its referenced assemblies based on application base information&nbsp;provided during&nbsp;mshsnapin installation. If an mshsnapin itself dynamically loads an assembly through an assembly name or a relative path, however, CLR may not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10491"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10491\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}