[英]How to import and use/run a C++ dll into another C# project?
I've created a C# application. 我创建了一个C#应用程序。 Within this application I would like to use/run a C++ API from another project(the API is written in macro coding). 在此应用程序中,我想使用/运行另一个项目中的C ++ API(该API用宏编码编写)。 I tried import the dll of that C++ project and tried to call a functions which belongs to that API. 我尝试导入该C ++项目的dll,并尝试调用属于该API的函数。 The problem is that it throws "unable to find method" error. 问题是它引发“无法找到方法”错误。
How can I run a C++ project in a C# project? 如何在C#项目中运行C ++项目?
You can't add a native DLL as a reference to a managed project. 您不能将本机DLL添加为对托管项目的引用。 You have 3 main options: 您有3个主要选项:
For any serious amount of code, option 3 is the most productive and effective approach. 对于大量代码,选项3是最有效和有效的方法。
If by "running", you mean a separate process: 如果“运行”是指一个单独的过程:
Use the class System.Diagnostics.Process
available in .NET: 使用.NET中可用的类System.Diagnostics.Process
:
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.StartInfo.CreateNoWindow = false;
myProcess.Start();
Else, if you mean using a dll developed in C++, you can use Platform Invoke Services
: 否则,如果您要使用C ++开发的dll,则可以使用Platform Invoke Services
:
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest
{
//First param is of course either in your PATH, or an absolute path:
[DllImport("msvcrt.dll", EntryPoint="puts", CallingConvention=CallingConvention.Cdecl)]
public static extern int PutString(string c);
[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
internal static extern int _flushall();
public static void Main()
{
PutString("Test");
_flushall();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.