试图从C#(.NET 4.0)加载kernal32和J#库并获取kernal32加载错误

时间:2011-05-27 01:37:13

标签: c# .net dllimport

我正在使用Visual Studio 2010 / .NET 4.0在C#中创建一个应用程序。对于部分应用程序,我需要压缩文件并使用J#库。我在导入kernal32.dll时遇到问题。我已经尝试将导入值设置为“kernal32”,“kernal32.dll”和system32文件夹中的绝对路径。我得到的错误如下,相关的源代码低于该值。错误发生在我调用“LoadLibrary(fullPath);”的行。任何帮助都会被大大挖掘出来!

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

的示例中提取了一些代码
  

System.DllNotFoundException未处理     消息=无法加载DLL“kernal32”:找不到指定的模块。 (来自HRESULT的异常:0x8007007E)     来源= ZipLibrary     类型名=“”     堆栈跟踪:          at ZipLibrary.Zipper.LoadLibrary(String ipFileName)          at ZipLibrary.Zipper..ctor(String directoryToZip,String zipToPath)位于c:\ users \ cchamberlain \ documents \ visual studio 2010 \ Projects \ AppDeploy \ ZipLibrary \ Zipper.cs:第37行          at AppDeployLibrary.AppDeployer.Deploy()位于c:\ users \ cchamberlain \ documents \ visual studio 2010 \ Projects \ AppDeploy \ AppDeployLibrary \ AppDeployer.cs:第37行          at AppDeploy.Program.Main(String [] args)位于c:\ users \ cchamberlain \ documents \ visual studio 2010 \ Projects \ AppDeploy \ AppDeploy \ Program.cs:第21行          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean 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;
    }
}

1 个答案:

答案 0 :(得分:3)

尝试kernel32。以下示例通过P / Invoke LoadLibrary页面。

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