'Can C#(.NET 6) call managed/unmanaged C++ dll?
I'm attempting to call a c++ DLL(WinSDK: 10.0.18362.0, Platform Toolset: VS 2017 v141) from C#(the target framework is .NET 6.0) on ARM, and I compiled them for the ARM64 platform but got an exception when it was running:
System.DllNotFoundException: Unable to load DLL "Calculate.dll" or one of its dependencies: The specified module could not be found.(0x8007007E) at ConsoleApp1.Program.Sum(int a, int b)
I'm sure I copied the DLL to the application directory and the program which was compiled for x64 platform worked.
C# Console Application Code:
internal class Program
{
[DllImport(@"Calculate.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int Sum(int a, int b);
public static void Main(string[] args)
{
try
{
Console.WriteLine("Input NumberA:");
int numberA = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Input NumberB:");
int numberB = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"The numberA is:{numberA}; the numberB is:{numberB}, the Sum is:{Sum(numberA, numberB)}");
}
catch (Exception ex)
{
Console.WriteLine($"ex:{ex}");
}
Console.ReadLine();
}
}
Calculate.h code:
#pragma once
extern "C" __declspec(dllexport) int Sum(int a, int b);
class Calculate
{
public:
};
Calculate.cpp code:
#include "Calculate.h"
#include "iostream"
using namespace std;
int Sum(int a, int b)
{
if (a - (int)a != 0 || b - (int)b != 0)
{
cout << "Please input a integrate" << endl;
return -1;
}
return a + b;
}
I want to know if C#(.NET 6.0) can call managed/unmanaged C++ DLL? Are there any suggestions? Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
