-2

I am trying to build a .NET wrapper around the 1Password CLI tool in order to manage our vaults from within our internal tools, however I can't seem to be able to send the input. It appears that it's not using the usual stdin for inputing the password. Is there a way to launch a process and send it keystrokes instead of using stdin?

The following does NOT work, the executable does NOT receive the input:

ProcessStartInfo processStartinfo = new ProcessStartInfo(_opPath, arguments)
{
    CreateNoWindow = true,
    UseShellExecute = false,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
Process process = Process.Start(processStartinfo);
process.StandardInput.WriteLine("password");
process.StandardInput.Flush();
jscarle
  • 1,045
  • 9
  • 17
  • What you're asking for [sounds very unlikely](https://stackoverflow.com/questions/6880624/). If you're redirecting StandardOutput you need to read it [or your process will deadlock](https://stackoverflow.com/questions/1040706/). – Dour High Arch Feb 05 '21 at 23:30
  • I can read StandardOutput and StandardError just fine, the problem is that even if I do read it, no matter what I send to StandardInput, the op.exe executable never receives it. – jscarle Feb 05 '21 at 23:33

2 Answers2

0

Perhaps try to set the StandardInputEncoding to System.Text.ASCIIEncoding (or other Encodings) and see if it works then?

PostThreadMessage

If all else fails maybe this will work for you. Consider investigating using the PostThreadMessage function. The password application will definitely have a message queue you can send the message to.

Once you have the threadId (Process.Threads) then send WM_Char messages to the password process and see if it processes them.

public const Int32 WM_CHAR = 0x0102;
public const Int32 WM_KEYDOWN = 0x0100;
public const Int32 WM_KEYUP = 0x0101;

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostThreadMessage(uint threadId, uint msg, IntPtr wParam, IntPtr lParam);
Jon P
  • 117
  • 4
0

Turns out that it was in line with what Dour mentioned, the caveit is that the order is very specific (at least in my case). Write to StandardInput, then read StandardOutput, then read StandardError. Any other order would cause a deadlock.

jscarle
  • 1,045
  • 9
  • 17