0

I'm using a CreateProcess call within a C++ program to execute a JAR file that runs a Java Swing GUI application. All works fine with the exception that the Java app starts off minimized and I want it to start with the window displayed. Here's the relevant code snippet:

// Construct the command string to be used for the CreateProcess call,
//including a parameter string
sprintf(cmdStr, "javaw -jar \"AppDir\\App.jar\" %s", parmStr);

// Create and initialized startup-info structure for use with CreateProcess call
STARTUPINFO startInfo;
ZeroMemory(&startInfo, sizeof(startInfo));

startInfo.wShowWindow = SW_NORMAL;
startInfo.dwFlags = STARTF_USESHOWWINDOW;
startInfo.cb = sizeof(startInfo);
PROCESS_INFORMATION procInfo;
ZeroMemory(&procInfo, sizeof(procInfo));

if (!CreateProcess(NULL, cmdStr, NULL, NULL, FALSE, 0, NULL, NULL, &startInfo,  &procInfo))
{
   MessageBox( dialogOwner, "Create Process Error", "Application not instantiated", MB_OK);
}

According to the MSDN literature, setting the wShowWindow flag to SW_NORMAL and dwFlags to STARTF_USESHOWWINDOW ought to do the trick but some of the comments I've read in this and other forums imply that sometimes those flags are ignored (e.g. for console apps) so I was wondering if that was the case here. For the record, I've had this problem before then it went away on its own and now it's back after I made some code changes. But I wasn't setting any flags in the startupinfo structure before, so I was hoping to achieve some consistency in behaviour by doing so. Any tips or pointers would be appreciated...

Sheldon R.

Sheldon R.
  • 452
  • 2
  • 7
  • 26
  • Are you using AWT or Swing to display the window? If so, `JFrame#setState()` is what you're looking for. – Jim Garrison Feb 13 '12 at 17:48
  • I haven't tried your suggestion yet, Jim, because I've still been playing around on the C++ side. What I've seen is that if I add a MessageBoxA call to display some debug info prior to doing the CreateProcess call, the window comes up just fine as opposed to being minimized. Is there a way in the C++ world to exorcise this ghost in the machine? – Sheldon R. Feb 13 '12 at 20:17
  • UPDATE: I edited my question to include code that shows the CreateProcess call, and also the statement where I set startupinfo.cb to be the size of the structure, since I read in a comment to another question that that was important. Also, I discovered that using the command "java" instead of "javaw" (which displays a command window for the console output) also causes the application window to be displayed instead of being minimized. Any thoughts on what I might be missing in the C++ part of my code? – Sheldon R. Feb 13 '12 at 21:39
  • UPDATE #2: I added code to my Java app to get the state from the JFrame and display a message in the status bar. There are two ways to get to the CreateProcess statement that launches the Java app, and currently, one way displays the app window, the other has it minimized. In both cases, the frame state is reported as NORMAL... – Sheldon R. Feb 14 '12 at 20:04

2 Answers2

0

This is an update to my previous answer: The reason I had to call my java applet two different ways (i.e. "java" or "javaw") depending on the context, had to do with a bug in the C++ application from which I was calling my applet. The reason I know this is because a few months after fixing my issue, a newer version of this application was released, and this version didn't have the underlying bug, which essentially caused a new bug in my applet due to the "java" command doing what you'd expect i.e. instantiating a console window in addition to the applet window, much to the surprise of my business users :). So for the new bug-free version of the C++ application, I call my applet using the "javaw" command regardless of whether or not a dialog box is instantiated first to enable the user to enter login credentials...

Sheldon

Sheldon R.
  • 452
  • 2
  • 7
  • 26
0

Okay, I've been working on a solution to my problem and I'm finally ready to talk about it, since it appears to be working :) I call my Java app one of two ways: either by popping up a dialog box first to collect login credentials, or by calling the app directly using saved credentials. For the login-dialog case, I call CreateProcess with the parameter "javaw...", whereas the saved-credential case appears to need "java..." to avoid the app starting up minimized. Aside from the difference in the parameter string, everything else about the CreateProcess call is the same. I don't know why I would use "java" in one case and "javaw" in the other, but since it's working, I won't question it :) But, of course, if someone wants to enlighten on the subject, I'd be happy to learn more. Thanks to @Jim Garrison for the suggestion, even if it wasn't ultimately the solution to my issue...

Sheldon R.

Sheldon R.
  • 452
  • 2
  • 7
  • 26