STARTUPINFO.wShowWindow is 0 when running from Visual Studio

Igal Tabachnik

I stumbled upon an issue while debugging a feature in an open-source Notepad replacement called Notepad2 (more specifically, a more recent fork called Notepad2-mod).

It has a flag /u that causes the app to restart itself under Administrative privileges (using runas verb with ShellExecute). The code looks like this (snipped for brevity):

STARTUPINFO si;
SHELLEXECUTEINFO sei;

si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);

ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
...
sei.lpVerb = L"runas";
sei.lpFile = lpArg1;
sei.lpParameters = lpArg2;
sei.nShow = si.wShowWindow;

ShellExecuteEx(&sei);

For some reason, if I launched this from Visual Studio (with or without a debugger attached), the elevated child process' main window would just not show! It would appear in Process Explorer, but had no visible windows.

Upon investigation, I realized that the nCmdShow passed to the child process' WinMain was 0 (which corresponds to SW_HIDE) when started from Visual Studio! This value was subsequently passed to ShowWindow, and that's why it didn't show.

When trying to launch this from a cmd shell, everything worked fine.

Upon further investigation, it turned out that the value of si.wShowWindow, obtained by a call to GetStartupInfo was 0 when running in VS, but was 1 when started from a cmd:

Launched from a cmd

According to STARTUPINFO MSDN entry, the value for wShowWindow should match the value of nCmdShow if dwFlags has STARTF_USESHOWWINDOW in it. However, in both cases (launching from VS and cmd), the value for dwFlags was 0.

So, is this an issue with VS or am I just holding it wrong?

Hans Passant

I'll write this one up, it is a pretty awesome bug. It is specific to the VS2015 debugging engine, it is quite notorious for having rather a lot of bugs. Something you can see for yourself by disabling it. Tools > Options > Debugging > General > tick the "Use Native Compatibility Mode" option. That forces an older debugging engine to be used, you now consistently get STARTUPINFO.nCmdShow == SW_SHOWNORMAL.

There is a wee corner-case to reason that this was intentional, blindly following the nCmdShow advice is not advisable. It is a malware attack vector, allowing it to start a program without the user noticing. Many programs intentionally ignore SW_HIDE, not a very intuitive thing to do and very easy to overlook. You need a glass that's well over half-full to make that interpretation however, the nCmdShow argument to WinMain() is what is normally used and it is correct.

Which is also the workaround that you can use. Of course in this specific case you should never rely on the startup value and pass SW_SHOWNORMAL or SW_SHOWMAXIMIZED depending on the current state of Notepad++'s main window.

So I vote bug, use connect.microsoft.com to report it. Put a link to the feedback article in a comment and we'll vote for it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"No extensions found" when running Visual Studio Code from source

From Dev

Visual Studio not deploying when running

From Dev

Program slow down when running on Visual Studio

From Dev

Visual Studio - Connect dialog when running project

From Dev

Running a test category from the Visual Studio UI

From Dev

Running Visual Studio Code from the CLI in Linux

From Dev

Avoiding "Press any key to continue" when running console application from Visual Studio

From Dev

Invalid Syntax error when running python from inside Visual Studio Code

From Dev

Avoiding "Press any key to continue" when running console application from Visual Studio

From Dev

Visual Studio 2015 - Running Post Build Events only when built from the GUI

From Dev

Undefined remote host when running devenv on Visual Studio for Linux project from jenkins

From Dev

Ignore cache in IE when running through Visual Studio

From Dev

'Access violation' when running application through Visual Studio

From Dev

Visual Studio becomes unresponsive when running a program using a SoundEffect

From Dev

Visual Studio recompiles "moc" files every time when running qmake

From Dev

White Screen when running Graphics Diagnostics in Visual Studio 2013

From Dev

Run other project when running unit tests in Visual Studio 2013

From Dev

Command window popping up on visual studio when running a program

From Dev

Visual Studio recompiles "moc" files every time when running qmake

From Dev

Visual Studio deleting source code when running Web Project

From Dev

Error when running Docker Compose in Visual Studio project for RabbitMQ container

From Dev

Error 500.21 when running site on IISExpress using visual studio

From Dev

Visual Studio 2013 Not Running

From Java

How to solve ERR_CONNECTION_REFUSED when trying to connect to localhost running IISExpress - Error 502 (Cannot debug from Visual Studio)?

From Dev

How get the current running Visual Studio installation path from VSPackage

From Dev

Inconsistent Results with Running Gulp from Visual Studio on Post Build

From Dev

Running orchard CMS version (1.10) and (1.10.1) from visual studio 2015

From Dev

Find and Replace works when running with visual studio but when running off of IIS i get null exception error

From Dev

WPF APP won't be running after publishing from Visual Studio but would be running with administrator

Related Related

  1. 1

    "No extensions found" when running Visual Studio Code from source

  2. 2

    Visual Studio not deploying when running

  3. 3

    Program slow down when running on Visual Studio

  4. 4

    Visual Studio - Connect dialog when running project

  5. 5

    Running a test category from the Visual Studio UI

  6. 6

    Running Visual Studio Code from the CLI in Linux

  7. 7

    Avoiding "Press any key to continue" when running console application from Visual Studio

  8. 8

    Invalid Syntax error when running python from inside Visual Studio Code

  9. 9

    Avoiding "Press any key to continue" when running console application from Visual Studio

  10. 10

    Visual Studio 2015 - Running Post Build Events only when built from the GUI

  11. 11

    Undefined remote host when running devenv on Visual Studio for Linux project from jenkins

  12. 12

    Ignore cache in IE when running through Visual Studio

  13. 13

    'Access violation' when running application through Visual Studio

  14. 14

    Visual Studio becomes unresponsive when running a program using a SoundEffect

  15. 15

    Visual Studio recompiles "moc" files every time when running qmake

  16. 16

    White Screen when running Graphics Diagnostics in Visual Studio 2013

  17. 17

    Run other project when running unit tests in Visual Studio 2013

  18. 18

    Command window popping up on visual studio when running a program

  19. 19

    Visual Studio recompiles "moc" files every time when running qmake

  20. 20

    Visual Studio deleting source code when running Web Project

  21. 21

    Error when running Docker Compose in Visual Studio project for RabbitMQ container

  22. 22

    Error 500.21 when running site on IISExpress using visual studio

  23. 23

    Visual Studio 2013 Not Running

  24. 24

    How to solve ERR_CONNECTION_REFUSED when trying to connect to localhost running IISExpress - Error 502 (Cannot debug from Visual Studio)?

  25. 25

    How get the current running Visual Studio installation path from VSPackage

  26. 26

    Inconsistent Results with Running Gulp from Visual Studio on Post Build

  27. 27

    Running orchard CMS version (1.10) and (1.10.1) from visual studio 2015

  28. 28

    Find and Replace works when running with visual studio but when running off of IIS i get null exception error

  29. 29

    WPF APP won't be running after publishing from Visual Studio but would be running with administrator

HotTag

Archive