.net - Dllimport 可在控制台应用程序中运行,但不能在 ASP.NET 网站中运行

标签 .net visual-studio dll .net-4.5 ghostscriptsharp

我从未使用过 DLL 导入,所以我遇到了一个有趣的问题。

我正在尝试在一个业余爱好项目中实现 Ghostscript.NET。所以我做了一个名为 GhostscriptSharp 的项目。该项目有一个名为 Ghostscript32.cs 的文件,它像这样导入 DLL:

        #region Hooks into Ghostscript DLL
        [DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
        private static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle);

        [DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
        private static extern int InitAPI(IntPtr instance, int argc, string[] argv);

        [DllImport("gsdll32.dll", EntryPoint = "gsapi_exit")]
        private static extern int ExitAPI(IntPtr instance);

        [DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
        private static extern void DeleteAPIInstance(IntPtr instance);

当我添加新的控制台测试项目时,引用 GhostscriptSharp 项目并将 gsdll32.dll 放在我的根目录中,然后运行如下程序,该程序有效:

GhostscriptWrapper.GeneratePageThumb(TEST_FILE_LOCATION, SINGLE_FILE_LOCATION, 1, 100, 100);

但是,我需要它在网络项目中工作。因此,我在根目录中添加了 gsdll32.dll,并添加了对 GhostscriptSharp 项目的引用。但是,然后我在这里收到错误:

An exception of type 'System.DllNotFoundException' occurred in GhostscriptSharp.dll but was not handled in user code

Additional information: Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

有什么想法可以解决这个问题吗?

这是我的解决方案文件夹的图像:

enter image description here

最佳答案

我只会回答它,即使答案非常简单。

DLL 文件必须位于 bin 文件夹中的原因。如果不是默认情况下。

因此,我制作了以下脚本,使其正确地将其复制到 bin 文件夹中:

if exist "$(TargetDir)gsdll32.dll" goto :exit
   copy "$(ProjectDir)\..\Packages\GhostscriptSharp\gsdll32.dll" "$(TargetDir)"
:exit

关于.net - Dllimport 可在控制台应用程序中运行,但不能在 ASP.NET 网站中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22568651/

相关文章:

.net - EF Core 添加迁移构建失败

c# - 项目的 Sitecore 增量发布

c# - 列表的 XML 序列化

c# - 在 Visual Studio C# 中开发 WebService,并从单独的 VS 解决方案中调用它

.net - 在 Visual Studio 中添加 Azure Key Vault 连接服务将继续显示 "Verifying that your application will have access to the key vault"

.net - 在 "controls in a panel"中设置 "flowlayoutpanel"的 anchor

visual-studio - 验证依赖于 SSIS 包中创建的表的数据流

c - Matlab 的 C 包装器有问题

c# - 模拟 Microsoft DLL

c# - 如何从javascript调用dll中的方法?