A
A
Alex_LC2015-07-02 22:51:32
.NET
Alex_LC, 2015-07-02 22:51:32

Why doesn't FindWindow return the window id on first run?

There is a WPF application that receives command line arguments as input at startup, and passes them through SendMessage to the first running instance of the application, here is the code:

class Program : Application
    {
        [System.STAThreadAttribute()]
        public static void Main() {
            bool createdNew = true;
            using (Mutex mtx = new Mutex(true, "AudioPlayer", out createdNew)) {
                if (createdNew)
                {
                    Program app = new Program();
                    app.InitializeComponent();
                    app.Run();
                }
                else {
                    MessageHelper msg = new MessageHelper();
                    int result = 0;
                    int hWnd = msg.getWindowId(null, "AudioPlayer");
                    result = msg.sendWindowsStringMessage(hWnd, 0, Environment.GetCommandLineArgs()[1]);
                }
            }
        }

        public void InitializeComponent() {
            this.StartupUri = new Uri("MainWindow.xaml", System.UriKind.Relative);
        }
    }

MessageHelper class: https://gist.github.com/BoyCook/5075907
So, if the application is already running, then when you restart it, the arguments are calmly passed and stored in a List in the MainWindow.xaml.cs class. id match and SendMessage works,
but in the case of the first run with multiple arguments, FindWindow (in our case, getWindowId) returns zero and only the first argument is processed.
What is missing for everything to work as it should?
UPD:
as planned, the program should open several selected files and save the paths of these files in the first instance of the program and use them further for their intended purpose

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Okhotnikov, 2015-07-03
@Alex_LC

I'm afraid to seem unconstructive, but: Can your "running" application have elementary checks on the result that the functions it uses return? Or maybe a debug log? If it were present, the issue would not arise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question