Cancelled

.NET GUI application + Native dll: stdout redirection

Hi, everyone.

I'm developing .NET GUI application (WinForms, .NET). It uses native dll with a single exported function. This function writes some output in stdout, here's the native code sample:

#define SAMPLELIBRARY_API __declspec(dllexport)

SAMPLELIBRARY_API void LetterList(void)

{

cout << "This function was called from LetterList() " << endl;

}

I want to redirect stdout, produced by native DLL, for the purpose of further usage.

So, I've made a class, which uses anonymous pipes for redirecting. Here's code:

public static class ConsoleOutRedirector

{

#region Constants

private const Int32 STD_OUTPUT_HANDLE = -11;

#endregion

#region Externals

[DllImport("[url removed, login to view]")]

extern static Boolean SetStdHandle(Int32 nStdHandle, SafeHandleZeroOrMinusOneIsInvalid handle);

[DllImport("[url removed, login to view]")]

extern static SafeFileHandle GetStdHandle(Int32 nStdHandle);

#endregion

#region Methods

public static String GetOutput(Action action)

{

[url removed, login to view](action != null);

using (var server = new AnonymousPipeServerStream([url removed, login to view]))

{

var defaultHandle = GetStdHandle(STD_OUTPUT_HANDLE);

[url removed, login to view](![url removed, login to view]);

[url removed, login to view](SetStdHandle(STD_OUTPUT_HANDLE, [url removed, login to view]));

try

{

action();

}

finally

{

[url removed, login to view](SetStdHandle(STD_OUTPUT_HANDLE, defaultHandle));

}

using (var client = new AnonymousPipeClientStream([url removed, login to view], [url removed, login to view]))

{

using (var reader = new StreamReader(client))

{

using (var writer = new StringWriter())

{

while ([url removed, login to view]() != -1)

{

[url removed, login to view]([url removed, login to view]([url removed, login to view]()));

}

return [url removed, login to view]();

}

}

}

}

}

#endregion

}

and usage sample:

[DllImport("[url removed, login to view]")]

extern static void LetterList();

private void button1_Click(object sender, EventArgs e)

{

[url removed, login to view] = [url removed, login to view](() => LetterList());

}

This all works just fine. But... only once.

The second call of GetOutput method hangs up on the reader's Peek() method call.

P.S. In the real project I've third-party native DLL, using stdout, and I cannot modify it. (

Task:

Fix or write a new wrapper (in the language c# or [url removed, login to view] ) to get stdout output from native dll in .net application

Skills: .NET, C# Programming, C++ Programming

See more: dll stdout, stdout dll, redirect cout, stdout native dll, safefilehandle stdout, redirect stdout dll, setstdhandle, net native dll redirect, setstdhandle safefilehandle, stdout, net redirect cout, gui setstdhandle, gui write stdout, wrapper programming, wrapper application, net winforms, static programming, public programming, programming methods, programming language usage, m string, define programming language, sharp programming language, sharp object, programming extern

About the Employer:
( 24 reviews ) Amman, Jordan

Project ID: #1149827

3 freelancers are bidding on average $67 for this job

crypted

please check your pmb.

$50 USD in 1 day
(26 Reviews)
5.0
richb38

Hi, I'm pretty sure I can figure this out reasonably quickly.

$100 USD in 3 days
(2 Reviews)
3.0
ShubhabrataKarma

I am confident of doing it.

$50 USD in 3 days
(0 Reviews)
0.0