2011-05-27 28 views
1

我使用Visual Studio 2010/.NET 4.0在C#中創建應用程序。對於部分應用程序,我需要壓縮文件並使用J#庫。我遇到導入kernal32.dll的問題。我已經嘗試將導入值設置爲「kernal32」,「kernal32.dll」和system32文件夾中的絕對路徑。我得到的錯誤在下面,相關的源代碼在下面。該錯誤發生在我調用「LoadLibrary(fullPath);」的行任何幫助將被大大挖掘!試圖從C#(.NET 4.0)加載kernal32和J#庫並獲取kernal32加載錯誤

我在http://blogs.windwardreports.com/davidt/2011/02/calling-j-code-from-net-40.html

System.DllNotFoundException拉出一些這個代碼從一個例子是未處理 消息=無法加載DLL「kernal32」:指定的模塊找不到。 (從HRESULT異常:0x8007007E) 源= ZipLibrary 類型名= 「」 堆棧跟蹤: 在ZipLibrary.Zipper.LoadLibrary(字符串ipFileName) 在ZipLibrary.Zipper..ctor(字符串directoryToZip,字符串zipToPath)在C:\用戶\ cchamberlain \ documents \ visual studio 2010 \ Projects \ AppDeploy \ ZipLibrary \ Zipper.cs:line 37 at AppDeployLibrary.AppDeployer.Deploy()in c:\ users \ cchamberlain \ documents \ visual studio 2010 \ Projects \ AppDeploy \ AppDeployLibrary \ AppDeployer.cs:line 37 at c:\ users \ cchamberlain \ documents \ visual studio 2010 \ Projects \ AppDeploy \ AppDeploy \ Program.cs中的AppDeploy.Program.Main(String [] args):line 21 at System.AppDomain .nExecuteAssembly(RuntimeAssembly程序集,String []參數) 位於System.AppDomain。 ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext的ExecutionContext,ContextCallback回調,對象的狀態,布爾ignoreSyncCtx) 在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回調,對象狀態) 在System.Threading.ThreadHelper.ThreadStart() 的InnerException:

public class Zipper 
{ 
    private const string DotNetFrameworkRelativePath = @"..\Microsoft.NET\Framework\v2.0.50727"; 
    private const string VjsDllName = "vjsnativ.dll"; 

    [DllImport("kernal32", SetLastError=true)] 
    static extern IntPtr LoadLibrary(string ipFileName); 

    private readonly string _directoryToZip; 
    private readonly string _zipToPath; 

    public Zipper(string directoryToZip, string zipToPath) 
    { 
     // If the current framework is .NET 4, include the VJS dll.))) 
     if(Environment.Version.Major >= 4) 
     { 
      string folder = SystemIO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), DotNetFrameworkRelativePath); 
      folder = SystemIO.Path.GetFullPath(folder); 
      string fullPath = SystemIO.Path.Combine(folder, VjsDllName); 

      // Check to see if file exists, if not the J# redistributable is not installed. 
      if(!SystemIO.File.Exists(fullPath)) 
      { 
       throw new ApplicationException("ERROR: Zipper requires that the Microsoft Visual J#® 2.0 Redistributable Package be installed on the environment."); 
      } 

      LoadLibrary(fullPath); 
     } 

     _directoryToZip = directoryToZip; 
     _zipToPath = _zipToPath; 
    } 
} 

回答

3

嘗試kernel32。下面的示例通過P/Invoke LoadLibrary頁面。

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