为什么从C#控制台应用程序看到我的C ++程序集而不是ASP.NET程序?

时间:2018-01-18 20:20:19

标签: c# c++ asp.net .net dllimport

我写了一个C ++ DLL。它有一个函数,它返回双1.0。

我试图在C#控制台应用程序中使用它:

namespace MyNameSpace
{

    class Program
        {
        [DllImport(@"C:\path\to\my\dll\myDll.dll")]
        static extern double SendOne();

        static void Main(string[] args)
        {
            double i = SendOne();
            Console.WriteLine(i);
        }
    }
}

这很有效。

然后我尝试在ASP.NET应用程序中使用它,使用空的MVC应用程序创建,然后添加一个控制器和一个视图:

控制器/ HomeController.cs:

{
    public class HomeController : Controller
    {
        [DllImport(@"C:\path\to\my\dll\myDll.dll")]
        static extern double SendOne();

        // GET: Home
        public ActionResult Index()
        {
            double i = SendOne();
            ViewBag["result"] = i;
            return View();
        }
    }
}

查看/主页/ Index.cshtml:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        This is some text and the result : @{ ViewBag["result"]}
    </div>
</body>
</html>

这不起作用,并返回BadImageFormatException。

在所有三个项目中,我手动将平台配置为 Debug / x64 (来自默认的Debug / AnyCPU)。

这是堆栈跟踪的底部:(第一行从法语手动翻译)

[BadImageFormatException: Impossible charge the file or assembly '[Name of the ASP.NET Project]' or one of its dependencies. Attempt to load a program of incorrect format.]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +36
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +21
System.Reflection.Assembly.Load(String assemblyString) +28    System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38

堆栈跟踪的顶部:HttpException 0x80004005。

ASP.NET Project&amp; C#控制台应用程序是同一个Visual Studio 2017解决方案中的两个项目,它们没有相互引用,而是我单独启动。

我没有理解ASP.NET项目和C#控制台应用程序之间存在的差异,这些差异可能导致Native C ++ Dll加载在C#Console应用程序中运行并在ASP.NET项目中失败。 / p>

有人会知道这个问题或对我可以尝试的建议有什么建议吗?感谢。

2 个答案:

答案 0 :(得分:0)

当程序试图加载具有不匹配位的模块时,会引发

BadImageFormatException。 Hat是64,但是进程尝试加载32位模块,反之亦然。因此,似乎ASP进程是来自控制台应用程序的缓冲位。

答案 1 :(得分:0)

感谢@ DavidHeffernan的回答,我的研究让我想到了这个问题:Compile ASP.NET to 64 BIT

在替代答案中哪些可以解决我的问题:无论ASP.NET是编译为32位还是64位,调试器,IIS Express都以32位运行,这与冲突有关使用64位C ++ DLL。

有必要更改调试器的版本,这可以在Visual Studio -> Options -> Projects and Solutions -> Web Projects -> Use the 64 bits version of IIS Express for web sites and projects完成。