{"id":2467,"date":"2022-08-30T15:25:06","date_gmt":"2022-08-30T15:25:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/17\/loading-referenced-dlls-from-memory-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:06","modified_gmt":"2022-08-30T15:25:06","slug":"loading-referenced-dlls-from-memory-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/loading-referenced-dlls-from-memory-collection-of-common-programming-errors\/","title":{"rendered":"Loading Referenced DLLs from Memory-Collection of common programming errors"},"content":{"rendered":"<p>Hey guys, \u00a0<\/p>\n<p>I have an application (let&#8217;s call it <i>LoaderApp<\/i>) that reads a byte[] into memory and loads it as an assembly (provided that the byte[] is an executable. This works fine. But how can I set the <i>LoaderApp<\/i> to also load the referenced DLLs of the byte[] into its Assembly as well?<\/p>\n<p>\u00a0 The code so far is as follows: \u00a0<\/p>\n<pre>\nbyte[] bytes = ; Assembly assembly = Assembly.Load(bytes);\nMethodInfo method = assembly.EntryPoint;\n \nif (method != null)\n{ object o = assembly.CreateInstance(method.Name); try { method.Invoke(o, null); } catch (TargetInvocationException e)\n    {\n            }\n}<\/pre>\n<p>\u00a0<br \/>\nI have read that the loaded assembly can search for its referenced DLLS by itself, as described here: Load Assembly at Runtime Method #3. \u00a0<\/p>\n<p>But since the <i>LoaderApp<\/i> will be loading more than one possible executable, I&#8217;d rather have that logic placed in it, rather than for every executable&#8230;<\/p>\n<p>\u00a0<\/p>\n<p>Thanks a lot <img decoding=\"async\" src=\"http:\/\/unknownerror.org\/script\/Forums\/Images\/smiley_smile.gif\" \/><\/p>\n<ol>\n<li>Not the best way to do it, but since no one else provided any suggestions, here&#8217;s what I did: \u00a0\n<p>1. The <i>LoaderApp<\/i> loads all the referenced assemblies in memory (as shown in the code snippet of the original question<\/p>\n<p>2. The loaded Assemblies are passed to a private method implemented in the dynamically loaded assemblies which simply accepts (and stores) an Assembly[] collection. 3. I&#8217;ve added to the dynamically loaded assmeblies:<\/p>\n<pre>AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);<\/pre>\n<p>4. For each unresolved assembly, the dynamic assembly checks its Assembly[] collection and returns the matching one as the correct referenced assembly.<\/li>\n<li>Hi, \u00a0 To get referenced assemblies frorm a specific type, you can try using the code below.\n<pre>(typeof(Item).Assembly).GetReferencedAssemblies()<\/pre>\n<p>\u00a0<\/p>\n<p>Thank you,<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-17 07:09:20. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Hey guys, \u00a0 I have an application (let&#8217;s call it LoaderApp) that reads a byte[] into memory and loads it as an assembly (provided that the byte[] is an executable. This works fine. But how can I set the LoaderApp to also load the referenced DLLs of the byte[] into its Assembly as well? \u00a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2467","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2467"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2467\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}