Subsystem software check

I have an .exe created with Windows Subsystem. I will copy this .exe to another .exe and I run:

editbin.exe /SUBSYSTEM:CONSOLE my.exe

      

So, I intend to have an .exe that works with a GUI and another .exe that is designed for command line operations (no GUI).

How can I check which subsystem is currently active in my C ++ code?

+2


source to share


3 answers


The type of subsystem (GUI, console, etc.) is stored in the PE header, which you can access through ImageHlp functions. You can get it with the following code:

// Retrieve the header for the exe.  GetModuleHandle(NULL) returns base address
// of exe.
PIMAGE_NT_HEADERS header = ImageNtHeader((PVOID)GetModuleHandle(NULL));
if (header->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
{
    // Console application.
}

      



Relevant MSDN entries:

ImageNtHeader IMAGE_NT_HEADERS IMAGE_OPTIONAL_HEADER

+8


source


Take a look at the ImageLoad function in the Imagehlp library. This returns a LOADED_IMAGE structure that has an IMAGE_NT_HEADERS structure in the FileHeader field. The OptionalHeader field in this structure is IMAGE_OPTIONAL_HEADER , which has a Subsytem that contains the information you want.



+3


source


Much easier than spelunking in your own headers: check if you have any console descriptors. For the application, the GUI subsystem GetStdHandle()

will return handles NULL

.

+1


source







All Articles