如何使函数的DLL(C ++)的主要功能在C#中使用 [英] How to make function for dll (c++) from main function to use in c#

查看:206
本文介绍了如何使函数的DLL(C ++)的主要功能在C#中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用我的C ++代码在C#应用程序。我想过PInvoke的,但我很困惑与我应该采取的从我的C ++应用程序创建DLL的方法。
基本上,我的C ++应用程序是一个功能,它运行数据处理的无限循环,我不知道我怎么能出口这样的事情对DLL并调用它从C#。

I need to use my c++ code in c# application. I thought about pinvoke, but I'm confused with the approach I should take for creating dll from my c++ app. Basically, my c++ app is a main function, which runs an endless loop of data processing and I dont know how I can export such thing to dll and call it from c#.

我试图重写我的在独立功能的init 函数的DLL,并从C#调用它,而是因为它是一个无限循环它永远不会来到了这一点与从函数返回。我不知道如何获得无限循环我的数据缓冲在某些时候我的C#应用​​程序。

I tried to rewrite my main function in standalone init function for dll and call it from c#, but because its a endless loop it never came to the point with return from function. And I dont know how to get my data buffer from endless loop at some point to my c# app.

我已经做了目前:

// myDLL.cpp
int __stdcall OpenRTSP(char url, char progname)
{
    TaskScheduler* scheduler = BasicTaskScheduler::createNew();
    UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);

    if (openURL(*env, &progname, &url) < 0)
    {
        return -1;
    }

    // All subsequent activity takes place within the event loop:
    // This function call does not return, unless programm closes.
    env->taskScheduler().doEventLoop(&eventLoopWatchVariable);

   return 0;
}

// myDLL.h
__declspec(dllexport)  int __stdcall OpenRTSP(char url, char progname);



我不知道如何从C#调用OpenRTSP,因为它永远不会从这个FUNC并返回如何写一个函数,该函数将返回我从无限循环数据缓冲区?

I dont know how to call OpenRTSP from c#, because it will never return from this func and how to write a function, which will return me a data buffer from endless loop?

推荐答案

由于你的C ++程序运行一个持续的过程,你倒是可能更好让它独立的过程,使用的 过程。从你的C ++程序的数​​据可以通过某种IPC方式进行传递。简单STDOUT似乎适合你的任务相当不错。 这篇文章介绍它如何。为实现

Since your C++ program runs a continuous process, you'd probably better leave it a standalone process and run it from your C# code using Process class. The data from you C++ process can be passed by virtue of some IPC methods. The simple STDOUT seems to fit your task quite well. This post describes how it can be achieved.

下面是一个简单的代码示例:

Here's a brief code sample:

var cppProcess = new Process();
cppProcess.StartInfo = new ProcessStartInfo
{
    FileName = "YourCppApp.exe",
    UseShellExecute = false,
    RedirectStandardOutput = true,
};
cppProcess.OutputDataReceived += (sender, arg) =>
{
    Console.WriteLine("Received: {0}", arg.Data);
};
cppProcess.Start();
cppProcess.BeginOutputReadLine();

如果您从C ++应用程序发回的缓冲区而应是二进制,使用 BinaryReader 从C ++应用程序的标准输出为:

If the buffer you send back from the C++ app should rather be binary, use BinaryReader to read from the C++ app's STDOUT:

var cppProcess = new Process();
cppProcess.StartInfo = new ProcessStartInfo
{
    FileName = "YourCppApp.exe",
    UseShellExecute = false,
    RedirectStandardOutput = true,
};
cppProcess.Start();
using (var cppOutput = new BinaryReader(cppProcess.StandardOutput.BaseStream))
    while (!cppProcess.StandardOutput.EndOfStream)
    {
        var buffer = new byte[4096];
        int bytesRead= cppOutput.Read(buffer, 0, buffer.Length);
        // Process the buffer...
    }






另外,你可以改变你的函数库函数,你已经尝试做的,通过它从C#一个回调函数作为参数。但是,这会比较繁琐,我个人不会在你的具体情况去为这一点。


Alternatively, you may change your main function to a library function as you have already tried to do and pass it a callback function from C# as a parameters. But this would be more tedious and I personally would not go for this in your particular case.

这篇关于如何使函数的DLL(C ++)的主要功能在C#中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆