• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

check if an application is console or GUI

Hi,

I need a function to check if an application is console or GUI. I will just pass the exe name (with full path) as parameter.

regards,
Partha
0
perthg
Asked:
perthg
  • 2
1 Solution
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

And here's how to do it in Delphi,

procedure TForm1.Button2Click(Sender: TObject);
var
  fs: TFilestream;
  signature: DWORD;
  dos_header: IMAGE_DOS_HEADER;
  pe_header : IMAGE_FILE_HEADER;
  opt_header: IMAGE_OPTIONAL_HEADER;
begin
  OpenDialog1.Filter := 'Exe|*.exe';
  if OpenDialog1.Execute then
  begin
  try
    Fs:= TFilestream.Create(OpenDialog1.FileName, fmOpenread or fmShareDenyNone );
    fs.read(dos_header, sizeof( dos_header ));
    fs.seek(dos_header._lfanew, soFromBeginning );
    fs.read(signature, sizeof(signature));
    fs.read(pe_header, sizeof( pe_header ));
    if pe_header.SizeOfOptionalHeader > 0 then
    begin
      fs.read( opt_header, sizeof( opt_header));
      case Opt_Header.Subsystem of
        IMAGE_SUBSYSTEM_WINDOWS_GUI: ShowMessage( '  GUI Application ');
        IMAGE_SUBSYSTEM_WINDOWS_CUI: ShowMessage( '  Console Application ');
      end;
    end;
  finally
    Fs.Free;
  end;
  end;
end;

This code is subset of the complete code which available in this link
http://www.swissdelphicenter.ch/en/showcode.php?id=1302

HTH

Regards,
Mohammed
0
 
LRHGuyCommented:
function GetSubSystem(aFile:String):integer;
var
  H1:TImageDOSHeader;
  H2:TImageFileHeader;
  H3:TImageOptionalHeader;
  Sig:ULong;
  F:file;
begin
  result:=-1; //unknown
  assignfile(F,aFile);
  Try
    reset(F,1);
    blockread(F,H1,sizeof(H1));
    if H1.e_Magic=Image_Dos_Signature then begin
      Seek(F,H1._lfanew);
      BlockRead(F,Sig,sizeof(Sig));
      if (IMAGE_NT_SIGNATURE=Sig) then begin
        BlockRead(F,H2,Sizeof(H2));
        blockread(F,H3,sizeof(H3));
        Result:=H3.SubSystem;
      end;
    end;
    closefile(F);
  except
  end;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  SS:integer;
begin
  SS:=GetSubSystem('C:\WINDOWS\COMMAND\CHKDSK.EXE');      
  case SS of
       IMAGE_SUBSYSTEM_WINDOWS_GUI: ShowMessage( 'GUI');
       IMAGE_SUBSYSTEM_WINDOWS_CUI: ShowMessage( 'Console');
       else ShowMessage('unknown');
  end;

  SS:=GetSubSystem('C:\WINDOWS\CALC.EXE');
  case SS of
       IMAGE_SUBSYSTEM_WINDOWS_GUI: ShowMessage( 'GUI');
       IMAGE_SUBSYSTEM_WINDOWS_CUI: ShowMessage( 'Console');
       else ShowMessage('unknown');
  end;
end;
0
 
Wim ten BrinkCommented:
It could be worse. An application could be BOTH GUI and console. If I create a GUI application and call AllocConsole in it, then it becomes a console application too. So what's the use of it anyway?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now