2

I have data from input fields on a simple Windows Form, which I'd like to pass back to a batch file or a caller.

  • The data being returned is complex in nature. Something that could be represented as an XML or JSON payload. So the numeric ExitCode is not suitable.

  • If I change the main method's return type to string instead of void, the program won't invoke.

  • The Enviromnent args is immutable, so you can't do something like:


string args[] = Environment.GetCommandLineArgs();
args[1] = 'blah';

(args 0 is program being invoked).

  • Environment variables become a pain, because they don't persist after the call returns.

Both these solutions might work, but it seems like extreme overkill, for something that should be simple to do in memory:

  • sockets
  • write the data to a xml file for the caller to pick up.

You'd think it should be possible to return a string. Is there something I'm missing?

Footnote:

(Unfortunately for me - I had to resort to writing the data out to a file. Writing the data to the Console and redirecting the console output to a file yielded a "file in use by another process" message back in Automation Anywhere, the RPA app I was trying to pass data back to). C'est la vie! Using Automation Anywhere

JGFMK
  • 8,425
  • 4
  • 58
  • 92
  • Possible duplicate of [Exit code from Windows Forms app](https://stackoverflow.com/questions/3201770/exit-code-from-windows-forms-app) – Sinatr Jun 27 '19 at 14:10
  • 2
    You could "dump" your output data into the standard output channel/stream. Then it is up to the batch file (or calling process) to capture or redirect this data in a way that allows it to process this data in some way... –  Jun 27 '19 at 14:10
  • 3
    You can only return an int value indicating success or otherwise. You either need to write the output to the console and then consume that or you need to use a file. – Ian Mercer Jun 27 '19 at 14:10
  • 1
    @Sinatr - It's not a duplicate of that. Since ExitCodes are a numeric return. I want to return an XML or JSON payload of complex data. – JGFMK Jun 27 '19 at 14:12
  • Possible duplicate of https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output – David Hope Jun 27 '19 at 14:12
  • If you want to process returned complex data structures (such as XML/Json, as you mentioned), batch files are probably not powerful enough on their own (unless your batch file is using some other 3rd party utility for that). Perhaps a PowerShell script instead of a batch file might be a more suitable approach (i am just guessing here about your application scenario, of course) –  Jun 27 '19 at 14:14
  • The answer at the top is BS. It's an entirely different scenario. Comments have already explained why. – JGFMK Jun 27 '19 at 14:14
  • This along with the comment from @elgonzo works for me. Thanks. https://stackoverflow.com/questions/5706723/windows-form-console-writeline-where-is-console – JGFMK Jun 27 '19 at 14:16
  • @elgonzo - If you want to convert your comment to an answer and add the link I gave on the prior answer I'll accept it - and question can be marked as answered. (it involves changing Windows app to a Console app so you can use Console.Write - the extra bit to make things work). – JGFMK Jun 27 '19 at 14:35
  • @mywillis I changed it back, because that will enable people to Google the question. Converting a Windows Project to a Console app is the solution. But, that was the missing bit of the puzzle, so I reverted it back to how I originally posted it. That is how my mind was thinking of the problem and how I'd perceive others would find answer more easily in the future. You could have said outbound parameter(s) instead of arguments if that helped. – JGFMK Jun 27 '19 at 14:50
  • @JGFMK, you don't need to change the type of the app. You can keep it a WinForms project. Just use Console.WriteLine. The app will still have no console window to write to, but Console.WriteLine will still write to the standard output channel. If the program/batch file calling your winforms application captures/redirects the standard output channel, it will receive the data written by Console.WriteLine –  Jun 27 '19 at 14:51

2 Answers2

1

The WinForms program could output the necessary data on the standard ouput stream/channel. Something like:

Console.WriteLine(outputDataString);

(Console.WriteLine being a shorthand for Console.Out.WriteLine)

The application project wouldn't necessarily need to be changed to "Console application" (as suggested by questions/answers like this: "windows form .. console.writeline() where is console?"), either, although a caveat applies: The written data won't show in the console of the caller (like, if the WinForms application has been started from within a console window), but it will still be written to the standard output stream/channel. (*)

If for example a batch file should capture the program output, the console for command can be (ab)used for this purpose. A simplified example:

for /f "usebackq" %%i in (`pathToYourWinFormsExecutable.exe`) do echo %%i

(For further help in tailoring this to your needs, open a console window and type for /?. This will provide a detailed explanation of what you can do with the for command.)



(*) Opinionated side note: That said, it is perhaps uncommon and weird behavior to let a program write data to the standard output stream/channel, with this data never being seen in a console window unless explicitly captured or redirected. Depending on the actual use scenario as well as the life-cycle of the program doing that, realizing such a "weird" counter-intuitive behavior might not be a recommended thing to do. If the common, normal behavior of having data written to the standard output stream/channel appear in the console window of the caller desired, changing the project to "Console application" would be the preferable approach.

-7

The best way to this is via args here is my simple example that returns a string value back via applicationname.exe

 class Program
 {
     static string Main(string[] args)
     {
        return "John";
    }
 }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Waheed Rafiq
  • 460
  • 1
  • 6
  • 17
  • 2
    How would this even work? `Main` can be `void` or `int` return type, not `string`. – Uwe Keim Jun 27 '19 at 14:19
  • 1
    Did you not see my comment in the original post: "If I change the main method's return type to string instead of void, the program won't invoke.". BTW: I didn't downvote you. – JGFMK Jun 27 '19 at 14:26