2009-03-20 14 views
63

में अप्रबंधित डीएल एम्बेड करना मेरे पास एक प्रबंधित सी # डीएल है जो DLLImport का उपयोग कर एक अप्रबंधित C++ dll का उपयोग करता है। सब महान काम कर रहा है। हालांकि, मैं अपने प्रबंधित DLL अंदर कि अप्रबंधित DLL एम्बेड करने के लिए वहाँ के रूप में माइक्रोसॉफ्ट द्वारा समझाने हैं:प्रबंधित प्रबंधित सी # डीएल

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.dllimportattribute.aspx

तो मैं, अपने प्रबंधित dll परियोजना के लिए अप्रबंधित dll फ़ाइल जोड़ी 'एंबेडेड संसाधन' के लिए गुण सेट और की तरह कुछ करने के लिए DllImport संशोधित:

[DllImport("Unmanaged Driver.dll, Wrapper Engine, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null", 
CallingConvention = CallingConvention.Winapi)] 

जहां 'आवरण इंजन' अपने प्रबंधित DLL 'अप्रबंधित Driver.dll' की विधानसभा नाम है अप्रबंधित DLL

है 0

जब मैं दौड़ता हूं, मुझे मिलता है:

एक्सेस अस्वीकार कर दिया गया है। (HRESULT से अपवाद: 0x80070005 (E_ACCESSDENIED))

मैं MSDN से और http://blogs.msdn.com/suzcook/ कि संभव हो सकता है चाहिए था से देखा ...

+0

संभावित डुप्लिकेट [कैसे एक सी ++ खिड़कियां एक सी # आवेदन exe में मर्ज करने DLL कर सकते हैं?] (Http://stackoverflow.com/questions/72264/how-can-ac-windows-dll-be- विलय-इन-एसी-तीक्ष्ण-अनुप्रयोग-exe) – Noah

उत्तर

6

मैं यह संभव है पता नहीं था - मुझे लगता है कि चाहते हैं कि सीएलआर को कहीं भी एम्बेडेड देशी डीएलएल निकालने की आवश्यकता है (विंडोज़ को इसे लोड करने के लिए डीएलएल के लिए फाइल की आवश्यकता है - यह कच्ची मेमोरी से एक छवि लोड नहीं कर सकता है), और जहां भी यह करने की कोशिश कर रहा है कि प्रक्रिया में अनुमति नहीं है।

Process Monitor from SysInternals तरह आप एक सुराग दे सकता है अगर pronblem कि DLL फ़ाइल बनाने में नाकाम रहने के है ...

अद्यतन:


आह ... अब मैं कर लिया है कि सुजैन कुक के लेख को पढ़ने के लिए (पृष्ठ मेरे लिए पहले नहीं आया था), ध्यान दें कि वह प्रबंधित डीएलएल के अंदर एक संसाधन के रूप में देशी डीएलएल को एम्बेड करने के बारे में बात नहीं कर रही है, बल्कि लिंक संसाधन - मूल डीएलएल अभी भी फाइल सिस्टम में अपनी फाइल होने की जरूरत है।

, http://msdn.microsoft.com/en-us/library/xawyf94k.aspx देखें जहां यह कहते हैं:

संसाधन फ़ाइल आउटपुट फ़ाइल के लिए नहीं जोड़ा गया है। यह/संसाधन विकल्प से अलग है जो आउटपुट फ़ाइल में संसाधन फ़ाइल एम्बेड करता है।

ऐसा लगता है कि ऐसा लगता है कि देशी डीएलएल असल में असेंबली का हिस्सा बनने का कारण बनता है (भले ही यह भौतिक रूप से एक अलग फ़ाइल है)। इसलिए जीएसी में प्रबंधित असेंबली डालने जैसी चीजें स्वचालित रूप से मूल डीएलएल, आदि शामिल होंगी

55

यदि आप प्रारंभिक समय के दौरान इसे अस्थायी निर्देशिका में निकालते हैं तो आप अप्रबंधित DLL को संसाधन के रूप में एम्बेड कर सकते हैं, और इसे लोड लाइब्रेरी के साथ स्पष्ट रूप से लोड कर सकते हैं पी/Invoke का उपयोग करने से पहले। मैंने इस तकनीक का उपयोग किया है और यह अच्छी तरह से काम करता है। माइकल ने नोट किया है कि आप इसे एक अलग फ़ाइल के रूप में असेंबली से लिंक करना पसंद कर सकते हैं, लेकिन इसे एक फ़ाइल में रखने के अपने फायदे हैं। यहां उपयोग किया गया दृष्टिकोण यहां दिया गया है:

// Get a temporary directory in which we can store the unmanaged DLL, with 
// this assembly's version number in the path in order to avoid version 
// conflicts in case two applications are running at once with different versions 
string dirName = Path.Combine(Path.GetTempPath(), "MyAssembly." + 
    Assembly.GetExecutingAssembly().GetName().Version.ToString()); 
if (!Directory.Exists(dirName)) 
    Directory.CreateDirectory(dirName); 
string dllPath = Path.Combine(dirName, "MyAssembly.Unmanaged.dll"); 

// Get the embedded resource stream that holds the Internal DLL in this assembly. 
// The name looks funny because it must be the default namespace of this project 
// (MyAssembly.) plus the name of the Properties subdirectory where the 
// embedded resource resides (Properties.) plus the name of the file. 
using (Stream stm = Assembly.GetExecutingAssembly().GetManifestResourceStream(
    "MyAssembly.Properties.MyAssembly.Unmanaged.dll")) 
{ 
    // Copy the assembly to the temporary file 
    try 
    { 
    using (Stream outFile = File.Create(dllPath)) 
    { 
     const int sz = 4096; 
     byte[] buf = new byte[sz]; 
     while (true) 
     { 
     int nRead = stm.Read(buf, 0, sz); 
     if (nRead < 1) 
      break; 
     outFile.Write(buf, 0, nRead); 
     } 
    } 
    } 
    catch 
    { 
    // This may happen if another process has already created and loaded the file. 
    // Since the directory includes the version number of this assembly we can 
    // assume that it's the same bits, so we just ignore the excecption here and 
    // load the DLL. 
    } 
} 

// We must explicitly load the DLL here because the temporary directory 
// is not in the PATH. 
// Once it is loaded, the DllImport directives that use the DLL will use 
// the one that is already loaded into the process. 
IntPtr h = LoadLibrary(dllPath); 
Debug.Assert(h != IntPtr.Zero, "Unable to load library " + dllPath); 
+0

क्या केनल 32 से DLLImport का उपयोग कर लोड लाइब्रेरी है? डीबग.एएसएसर्ट डब्ल्यूसीएफ सेवा के भीतर एक ही कोड का उपयोग कर मेरे लिए असफल रहा है। –

+0

यह एक अच्छा समाधान है, लेकिन उन मामलों के लिए विश्वसनीय समाधान ढूंढना बेहतर होगा जब दो अनुप्रयोग एक ही स्थान पर एक ही स्थान पर लिखने का प्रयास करते हैं। अपवाद हैंडलर अन्य अनुप्रयोग डीएलएल को अनपॅक करने से पहले पूरा करता है। –

8

यहां मेरा समाधान है, जो जेएमसीक्लेलन के उत्तर का एक संशोधित संस्करण है। नीचे फ़ाइल को class.cs फ़ाइल में सहेजें।

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.IO; 
using System.Reflection; 
using System.Diagnostics; 
using System.ComponentModel; 

namespace Qromodyn 
{ 
    /// <summary> 
    /// A class used by managed classes to managed unmanaged DLLs. 
    /// This will extract and load DLLs from embedded binary resources. 
    /// 
    /// This can be used with pinvoke, as well as manually loading DLLs your own way. If you use pinvoke, you don't need to load the DLLs, just 
    /// extract them. When the DLLs are extracted, the %PATH% environment variable is updated to point to the temporary folder. 
    /// 
    /// To Use 
    /// <list type=""> 
    /// <item>Add all of the DLLs as binary file resources to the project Propeties. Double click Properties/Resources.resx, 
    /// Add Resource, Add Existing File. The resource name will be similar but not exactly the same as the DLL file name.</item> 
    /// <item>In a static constructor of your application, call EmbeddedDllClass.ExtractEmbeddedDlls() for each DLL that is needed</item> 
    /// <example> 
    ///    EmbeddedDllClass.ExtractEmbeddedDlls("libFrontPanel-pinv.dll", Properties.Resources.libFrontPanel_pinv); 
    /// </example> 
    /// <item>Optional: In a static constructor of your application, call EmbeddedDllClass.LoadDll() to load the DLLs you have extracted. This is not necessary for pinvoke</item> 
    /// <example> 
    ///    EmbeddedDllClass.LoadDll("myscrewball.dll"); 
    /// </example> 
    /// <item>Continue using standard Pinvoke methods for the desired functions in the DLL</item> 
    /// </list> 
    /// </summary> 
    public class EmbeddedDllClass 
    { 
     private static string tempFolder = ""; 

     /// <summary> 
     /// Extract DLLs from resources to temporary folder 
     /// </summary> 
     /// <param name="dllName">name of DLL file to create (including dll suffix)</param> 
     /// <param name="resourceBytes">The resource name (fully qualified)</param> 
     public static void ExtractEmbeddedDlls(string dllName, byte[] resourceBytes) 
     { 
      Assembly assem = Assembly.GetExecutingAssembly(); 
      string[] names = assem.GetManifestResourceNames(); 
      AssemblyName an = assem.GetName(); 

      // The temporary folder holds one or more of the temporary DLLs 
      // It is made "unique" to avoid different versions of the DLL or architectures. 
      tempFolder = String.Format("{0}.{1}.{2}", an.Name, an.ProcessorArchitecture, an.Version); 

      string dirName = Path.Combine(Path.GetTempPath(), tempFolder); 
      if (!Directory.Exists(dirName)) 
      { 
       Directory.CreateDirectory(dirName); 
      } 

      // Add the temporary dirName to the PATH environment variable (at the head!) 
      string path = Environment.GetEnvironmentVariable("PATH"); 
      string[] pathPieces = path.Split(';'); 
      bool found = false; 
      foreach (string pathPiece in pathPieces) 
      { 
       if (pathPiece == dirName) 
       { 
        found = true; 
        break; 
       } 
      } 
      if (!found) 
      { 
       Environment.SetEnvironmentVariable("PATH", dirName + ";" + path); 
      } 

      // See if the file exists, avoid rewriting it if not necessary 
      string dllPath = Path.Combine(dirName, dllName); 
      bool rewrite = true; 
      if (File.Exists(dllPath)) { 
       byte[] existing = File.ReadAllBytes(dllPath); 
       if (resourceBytes.SequenceEqual(existing)) 
       { 
        rewrite = false; 
       } 
      } 
      if (rewrite) 
      { 
       File.WriteAllBytes(dllPath, resourceBytes); 
      } 
     } 

     [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] 
     static extern IntPtr LoadLibrary(string lpFileName); 

     /// <summary> 
     /// managed wrapper around LoadLibrary 
     /// </summary> 
     /// <param name="dllName"></param> 
     static public void LoadDll(string dllName) 
     { 
      if (tempFolder == "") 
      { 
       throw new Exception("Please call ExtractEmbeddedDlls before LoadDll"); 
      } 
      IntPtr h = LoadLibrary(dllName); 
      if (h == IntPtr.Zero) 
      { 
       Exception e = new Win32Exception(); 
       throw new DllNotFoundException("Unable to load library: " + dllName + " from " + tempFolder, e); 
      } 
     } 

    } 
} 
+1

मार्क, यह वास्तव में अच्छा है। मेरे उपयोग के लिए, मैंने पाया कि मैं LoadDll() विधि को हटा सकता हूं, और ExtractEmbeddedDlls() के अंत में LoadLibrary() को कॉल कर सकता हूं। इसने मुझे पैथ संशोधित कोड को हटाने की भी अनुमति दी। – Cameron

5

आप Costura.Fody आज़मा सकते हैं। दस्तावेज़ीकरण कहता है, कि यह अप्रबंधित फ़ाइलों को संभालने में सक्षम है। मैंने केवल इसे प्रबंधित फ़ाइलों के लिए उपयोग किया है, और यह एक आकर्षण की तरह काम करता है :)

2

कोई भी डीएलएल को किसी फ़ोल्डर में कॉपी कर सकता है, और फिर उस फ़ोल्डर में SetDllDirectory पर कॉल कर सकता है। तब लोड लाइब्रेरी को कोई कॉल की आवश्यकता नहीं है।

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool SetDllDirectory(string lpPathName); 
की
संबंधित मुद्दे