2015-11-01 63 views
0

我想在C++中創建一個Windows IoT應用程序,我也可以使用C#代碼。C++和C#IoT - C++項目訪問另一個項目中的C#DLL

我首先使用Visual C++空白Windows IoT Core控制檯應用程序創建了一個解決方案。這裏是入口點代碼:

// EnterPoint.cpp : Defines the entry point for the console application. 
// 

#include <iostream> 

#include "pch.h" 

#using <MainLogic.dll> 

int main(int argc, char **argv) 
{ 
    int result = MainLogic::StartupTask::Start(); 
    std::cout << result << "\n"; 
} 

然後我在名爲MainLogic,此時相同的解決方案創造了另一個項目的一個Visual C#類庫(便攜式)使用.NET框架6和默認目標以及Windows Phone 8.1。這是在C#文件中的代碼我提出:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace MainLogic 
{ 
    public class StartupTask 
    { 
     public static int Start() 
     { 
      return 0; 
     } 
    } 
} 

我建立了C#項目,並添加..\MainLogic\bin\Debug的參考目錄和C/C++ - C++的項目>其他使用#using目錄。

當我與x86架構跑這在本地Windows調試,它給了一個錯誤:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'MainLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. 
    at main(Int32 argc, SByte** argv) 
    at _mainCRTStartup() 
在出現的命令提示符窗口

。它促使我突破,我做到了。該窗口對此表示:

Unhandled exception at 0x75613E28 (KernelBase.dll) in PythonInCSharpInCPP.exe: 0xE0434352 (parameters: 0x80070002, 0x00000000, 0x00000000, 0x00000000, 0x72D60000). 

我去了最後一個地址(0x72D60000),和周圍的地方,這是「此程序無法在DOS模式下運行」的文本。

我在做什麼錯?

+0

我在這裏找到答案:http://stackoverflow.com/questions/33489924/can-you-use-c-dlls-in-c-sharp-code-in-a-uwp/33490707?noredirect=1 – M3579

回答

0

您需要在C++項目中添加C#項目作爲依賴項。那麼當你構建C#時,DLL將被複制到C++輸出目錄。

0x72D60000地址是DLL加載的基址。每個Windows程序都以一個存根表示,它不能在DOS模式下運行,以便在MS-DOS仍然與Windows一起運行的時候使用。在Windows下運行時,該部分被跳過。

+0

我在Project - > Project Dependencies中檢查了MainLogic,但錯誤窗口出現了。 – M3579