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
perthgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wim ten BrinkSelf-employed developerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.