0

Is it possible to open a c# exe file (console app) from another c # project (windows form) and to write or read different text values from the exe file? i'm using user32dll to handle the exe file.

Thx I did use this method to add a text in an exe file:

        Clipboard.SetText("Message!");
        foreach (IntPtr pPost in pControls)
        {
            PostMessage(pPost, (uint)WindowMessage.WM_PASTE, 0, 0);
        }

but is not working. I don't see the "Message" post added in c# exe (which is a console app) . Thoug using the notepad.exe everyting is working ok.

12
  • can you elaborate on these dates? What dates are they? Are they variables? Also, I don't really think you need user32.ddl to write to the exe? Commented Apr 2, 2011 at 12:42
  • string values. I would like to read and write text values in the exe file Commented Apr 2, 2011 at 12:44
  • so you want to just read/write dates into the exe file? You want to append these to the exe? Commented Apr 2, 2011 at 12:45
  • Are you trying to run the exe and get data from the output? Or do you want to change the exe itself? Commented Apr 2, 2011 at 12:48
  • i want to open it and to read and write in it (of course from another c# application). Commented Apr 2, 2011 at 12:51

2 Answers 2

6

You can use the Process class to execute the command line app and redirect the input/output using the StandardInput and StandardOutput properties.

From MSDN:

 Process p = new Process();
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

There are more examples on the StandardOutput page linked above.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 did not know you could do this! =) But this would cover sending inputs TO the calling exe FROM the executed exe? right?
@giddy - You can also redirect StandardInput and StandardError.
OK checked MSDN, seems you can do just the opposite for standard input! Nice, learn't something new today!
@elisa - Your question doesn't make sense. An exe file contains binary code. Why would you want to read that?
2

There are a large number of possible failure modes here.

  • First off, WM_PASTE is sent, not posted, you should use SendMessage().
  • Only a limited number of Windows controls actually implement behavior for this message, it has to be a edit control, rich edit control or combo box.
  • There's UIPI, the user interface part of UAC, it stops an unelevated process from hijacking the privileges of an elevated one.
  • There's the usefulness of doing something like this, pasting text in every child window doesn't make much sense.
  • The pinvoke declaration you used for PostMessage() isn't correct, the wparam and lparam arguments are IntPtr, not int.

Visit pinvoke.net for (usually) correct pinvoke declarations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.