Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically change the location of an unmanaged DLL to import
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/10044511/how-to-separate-managed-and-unmanaged-dlls-in-another-directory">How to separate managed and unmanaged DLLs in another directory</a> </p> </blockquote> <p>I am using unmanaged code with my C# managed code. I have the unmanaged DLL embedded in the executable of the application. In order to make this work I extract the resource/DLL to a file before I make any calls to the DLLs methods.</p> <pre><code>public static class ResourceExtractor { public static void ExtractResourceToFile(string resourceName, string filename) { if (!System.IO.File.Exists(filename)) using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create)) { byte[] b = new byte[s.Length]; s.Read(b, 0, b.Length); fs.Write(b, 0, b.Length); } } } </code></pre> <p>So that before I make calls to the DLL I make sure to do the following:</p> <pre><code>ResourceExtractor.ExtractResourceToFile("Namespace.dll_name.dll", "dll_name.dll"); </code></pre> <p>And to wrap the methods of the unmanaged DLL I have...</p> <pre><code>public class DLLWrapper { public const string dllPath = "dll_name.dll"; [DllImport(dllPath)] public static extern uint functionA(); [DllImport(dllPath)] public static extern uint functionB(); [DllImport(dllPath)] public static extern uint functionC(); } </code></pre> <p><strong>Now for the question...</strong></p> <p>This all works. What I <strong><em>DO NOT</em></strong> like is that it creates the DLL sitting next to the executable. I would much rather create the DLL in a temporary directory and load it from there. Something like...</p> <pre><code>string tempDLLname = Path.GetTempFileName(); ResourceExtractor.ExtractResourceToFile("Namespace.dll_name.dll", tempDLLname ); DLLWrapper.dllPath = tempDLLname; ... public class DLLWrapper { public static string dllPath; [DllImport(dllPath)] public static extern uint functionA(); [DllImport(dllPath)] public static extern uint functionB(); [DllImport(dllPath)] public static extern uint functionC(); } </code></pre> <p>However this does not work because <code>[DllImport(path)]</code> requires the path to be a <code>const</code>. So how do you change the location that you want to load the DLL from? I won't know until runtime.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload