简体   繁体   中英

how to use piping in C#

我在 C++ 中有一个 exe,我在其中创建了 CreateIPCQueue 函数,我正在 C# 中创建另一个 exe,我想在其中使用此方法。那么我应该如何进行此操作。请帮助我

Powershell (.NET) and Piping

You might be able to make use of Windows Powershell to pipe between classic program output and cmdlets built in .NET. Because it pipes objects instead of raw text between programs, I'm unsure it's compatibility with piping classic strings, although you might find a unique solution here so I'll post it now and update later if a more specific variation is found...

Update:

In Powershell, I tested and found that classic text output can be piped into .NET cmdlets. For example, I took the standard text output from the C# compiler help screen and piped it into the ForEach-Object construct - in this case each object is a .NET String because the classic output is represented textually.

# In Powerhsell.exe #
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /? | ForEach-Object -Process {"LINE: " + $_}

It produced the following output that proves the concept - indeed it prepended the phrase "LINE: " to each line of text.

LINE:                         - ERRORS AND WARNINGS -
LINE: /warnaserror[+|-]             Report all warnings as errors
LINE: /warnaserror[+|-]:<warn list> Report specific warnings as errors
LINE: /warn:<n>                     Set warning level (0-4) (Short form: /w)
LINE: /nowarn:<warn list>           Disable specific warning messages
LINE:
LINE:                         - LANGUAGE -
LINE: /checked[+|-]                 Generate overflow checks

Any other .exe that produces text output can be used in this example.

Next Steps

Based on that proof of concept showing classic text and .NET objects interoperating through Powershell piping, the next step might be expose your .NET program as a cmdlet (to substitute in place of the For-Each construct in the example). I looked for a useful article about exposing your existing .NET code as a cmdlet and found: Creating a Windows PowerShell CmdLet using the Visual Studio Windows PowerShell Templates

Basically you can expose a piece of your existing .NET logic as a cmdlet and then rely on the natural piping ability inherent in Powershell. The bonus is you'll be compatible with Powershell and other programs can reap the benefits of using your cmdlet.

Update 2
Additional Resources

I think what you are taking about is that you want to use the UNMANGED c++ funciton from

MANAGED C# environment. That means you want to use interop.

If this is correct, then you can follow the below:

Suppose in CPP(UNMANAGED) if have the following function(A basic Calculator):

//User Program Starts from Here
extern "C"
{
  __declspec(dllexport) int MyCalculator(int _FirstNo,int _SecNo,char _op)
  {

      switch(_op)
      {
          case '+':  return(_FirstNo + _SecNo);break;
      }
  }
}

Next , I want to call this function from my MANAGED C# Environemnt.

The below will help

public partial class Form1 : Form
{
    [DllImport(@"F:\CppDll.dll")] //is the path where the CPP dll is located
    private static extern int MyCalculator(int _num1, int _num2,char _operator);
    int _Result = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSum_Click(object sender, EventArgs e)
    {
        _Result = MyCalculator(20,10,'+');
        Messagebox.Show( _Result.ToString()); // Output: 30
    }

   }

Hope this helps

NET provides the System.IO.Pipes namespace for working with pipes since 2.0. It supports named and anonymous pipes, access and auditing rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM