Returning a string from a console application
Asked Answered
C

2

20

What I really want to do is this

static string Main(string[] args)

but that doesn't work, your only options are void and int. So, What are some different ways to return the string that I need to return to the calling application?

Background

I need to write a console app that is specifically designed to be called from another application

Process.Start("MyCode.exe -Option 12aaa1234");

How can this calling program receive a string returned from that executable?

Research

From what I can tell, at this point in time my only option is to have the calling application attach a listening stream to the Standard Output stream of the process before starting it, and send the "return" using Console.Out.Write from inside my executable. Is this in fact the ONLY way to do this, or is there something different/better I can use?

Crim answered 4/11, 2013 at 18:5 Comment(7)
Are you sure you should be calling it as a separate application, and not just as a library?Cannae
social.msdn.microsoft.com/Forums/en-US/…Cinda
You can't return a string. You can output a string and catch that output in your other application.Gravitate
You have tons of options. You could write to a file, you could write to an environment variable, you could create a named pipe, you could use a registry value, you could use a network socket, or any number of other options. None are likely to be easier than using standard output though.Beamon
Are you writing/influencing both executables here?Competitor
@Beamon What about the clipboard?!! You forgot the clipboard. msdn.microsoft.com/en-us/library/windows/desktop/…Emboss
@ebyrob Very true. It wasn't an exhaustive list.Beamon
C
17

Is this in fact the ONLY way to do this, or is there something different/better I can use?

This isn't the only way to do this, but it is the most common.

The other options would involve some form of interprocess communication, which is likely going to be significantly more development effort for a single string.

Note that, if the calling application is a .NET application, and you have control over both applications, it might make more sense to just write a class library instead of a console application. This would allow you to keep the code completely separate, but have the executable "call into" your library to get the string data.

Competitor answered 4/11, 2013 at 18:9 Comment(1)
This is, in fact, the answer I was looking for. Yes there are other ways to do it, but they all are rather more involved. I only have control of the executable, not the application that will call it. I am trying to create a "dependency free" interface to certain portions of my libraries useable by external sources, without them having to include and reference all 18 dll's involved directly in their own project. This seemed the easiest, but I needed a way to return the data.Crim
F
3

Idea 1:

Using MyCode.exe, create an encrypted text file, which is saved in a specified path, which can then be decrypted in the current app and read.

In the app: "MyCode.exe", add this code:

public void ReturnToOther()
{
    string ToReturn = "MyString";
    System.IO.File.WriteAllText("Path", Encrypt(ToReturn));
}

public String Encrypt(string ToEncrypt)
{
    string Encrypted = null
    char[] Array = ToEncrypt.ToCharArray();
    for (int i = 0; i < Array.Length; i++)
    {
        Encrypted += Convert.ToString(Convert.ToChar(Convert.ToInt32(Array[i]) + 15));
    }
    return Encrypted;
}

In the app you are making now:

public void GetString()
{
    string STR = Decrypt(System.IO.File.ReadAllText("Path"));
    Console.WriteLine("The string is: {0}", STR);
}

// If you want to keep this running before the file exists, use this:

/*
public void GetString()
{
    for(int i = 0; i > -1; ++i)
    {
        if(System.IO.File.Exists("Path"))
        {
            string STR = Decrypt(System.IO.File.ReadAllText("Path"));
            Console.WriteLine("The string is: {0}", STR);
            break;
        }
        else
        {
            //Do something if you want
        }
    }
} */


public String Decrypt(string ToDecrypt)
{
    string Decrypted = null
    char[] Array = ToDecrypt.ToCharArray();
    for (int i = 0; i < Array.Length; i++)
    {
        Decrypted += Convert.ToString(Convert.ToChar(Convert.ToInt32(Array[i]) - 15));
    }
    return Decrypted;
}

Idea 2:

Use TCP to upload the string to a port, e.g. LocalHost (127.0.0.1), and then receive the string on the app you are developing, using a TCP Listener

An article on TCP - http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

Hope this helps :)

EDIT:

Have a look at Sockets too: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

Feud answered 4/11, 2013 at 18:21 Comment(3)
I like the tcp to localhost idea. Not what I was looking for, but I do like it :-)Crim
@Crim BTW - TCP is one form of interprocess communication. Pipes are likely better if this is the same system, though.Competitor
one computer, two different programs, and the only one I get to design is the executable interface that is being called remotely. I do have a free hand on the design though, so I can use whatever return method I want and tell them how to access it...I just dont have control over what runs it, or when it runs, or what system its running on, etc etc and so onCrim

© 2022 - 2024 — McMap. All rights reserved.