0

I have a C header file that contains something like this:

void init(FILE* in, FILE* out, FILE* err);

I'm guessing this equates to the following PInvoke signature:

[DllImportAttribute("mylib", EntryPoint = "init")]
public static extern void init(IntPtr @in, IntPtr @out, IntPtr err);

Incidentally, I want to invoke this method. Can I get the IntPtr from Console.In, Console.Out, Console.Error somehow?

It should work on Linux and Windows, and not Windows exclusively.

HelloWorld
  • 3,381
  • 5
  • 32
  • 58

1 Answers1

1

You can use GetStdHandle to obtain handles to the standart input(STD_INPUT_HANDLE), output(STD_OUTPUT_HANDLE) and error(STD_ERROR_HANDLE) file.

You can then write and read from those files using WriteFile, ReadFile functions.

After that, if you still need a FILE structure, follow these steps to create one.

How make FILE* from HANDLE in WinApi?

Gurhan Polat
  • 696
  • 5
  • 12
  • Should have mentioned it, but I'm working on Linux. Nevertheless, I'll mark it as correct :) – HelloWorld Jun 24 '19 at 08:01
  • Thanks for your response :), So actually in linux, you can use fdopen to obtain a FILE structure for a file descriptor. `FILE *f = fdopen(STDOUT_FILENO, "w");` – Gurhan Polat Jun 24 '19 at 09:53