The MASM Forum

General => The Campus => Topic started by: pcMike on April 19, 2016, 01:44:46 PM

Title: Creating an Icon for a Console application
Post by: pcMike on April 19, 2016, 01:44:46 PM
I have a Console mode Win32 application that I want to have a custom icon for, but after following instructions I found in the old forum, I'm unable to make it appear in the upper-left corner of my application.

I placed a MyIcon.ico in my project folder, along with an rsrc.rc file that contains the following line:
100 icon discardable "MyIcon.ico"

Then I ran the resource compiler like so:
\masm32\bin\rc /v rsrc.rc
\masm32\bin\cvtres /machine:ix86 rsrc.res

This successfully created the rsrc.res and rsrc.obj files.

Next I load my ASM file into QE, and select "Console Assemble & Link". and I see the .EXE file is about 1K larger then before, so it seems to have the new resource included. But when I run it, the Icon is still the default Command Prompt icon.

Can anyone see what I did wrong?
Title: Re: Creating an Icon for a Console application
Post by: pcMike on April 19, 2016, 04:08:00 PM
I discovered the Icon only fails to load when I open the application from a Command Prompt. If I open it from Windows Explorer, then the icon loads as it should.

It might be because I'm running under Windows 10 (Using the Legacy Command Prompt), but I see other Win32 Console programs are able to set it correctly even when run from the same Command Prompt.
Title: Re: Creating an Icon for a Console application
Post by: hutch-- on April 19, 2016, 04:26:41 PM
Mike,

This is just a guess but when you run the console, the icon would be for CMD.EXE, not your app.
Title: Re: Creating an Icon for a Console application
Post by: pcMike on April 19, 2016, 04:33:16 PM
Right Hutch, it is the CMD.EXE icon that continues to appear after I run my program from the Command Prompt. The same happens under Windows 7.

I did some more research and I see that I will need to use the LoadIcon() and SetConsoleIcon() functions.
I found a nice example from Vortex here in written in C which correctly sets the icon from the Command Prompt:

http://www.masmforum.com/board/index.php?topic=14795.0

Title: Re: Creating an Icon for a Console application
Post by: pcMike on April 19, 2016, 04:47:35 PM
I found another thread in which Vortex had converted the above C sample to MASM32, and this was exactly what I needed.

http://www.masmforum.com/board/index.php?topic=11772.0
Title: Re: Creating an Icon for a Console application
Post by: nidud on April 19, 2016, 07:48:17 PM
deleted
Title: Re: Creating an Icon for a Console application
Post by: TWell on April 19, 2016, 09:54:13 PM
An another way:.386
.model flat,stdcall

ExitProcess PROTO :DWORD
GetModuleHandleA PROTO :DWORD
GetConsoleWindow PROTO
INCLUDELIB kernel32.lib
LoadIconA PROTO :DWORD,:DWORD
MessageBoxA PROTO :DWORD,:DWORD,:DWORD,:DWORD
SendMessageA PROTO :DWORD,:DWORD,:DWORD,:DWORD
INCLUDELIB user32.lib
;WM_GETICON = 007Fh
;WM_SETICON = 0080h

.data?
hWnd   dd ?
hIcon0 dd ?
hIcon  dd ?

.code
start:
    invoke  GetModuleHandleA,0
    invoke  LoadIconA,eax,100
    mov     hIcon,eax
    invoke  GetConsoleWindow
    mov     hWnd,eax
    invoke  SendMessageA,eax,7Fh,0,0 ; original icon
    mov     hIcon0,eax
    invoke  SendMessageA,hWnd,80h,0,hIcon
    invoke  MessageBoxA,0,0,0,0
    invoke  SendMessageA,hWnd,80h,0,hIcon0 ; original icon back
    invoke  ExitProcess,0
END start
Title: Re: Creating an Icon for a Console application
Post by: jj2007 on April 19, 2016, 10:54:33 PM
Totally sufficient, provided you link it with the *.res file produced by \masm32\bin\rc.exe:
include \masm32\include\masm32rt.inc
.code
start:
  inkey "ok?"
  exit
end start


You won't see the icon from a DOS prompt. However, start ConsoleWithIconMasm32 works.
Title: Re: Creating an Icon for a Console application
Post by: pcMike on April 20, 2016, 03:05:48 PM
Thanks for all the feedback everyone!  :t

Nidud: I tryed using Open Watcom resource compiler using the command line you suggested, and it made my exe larger but when I try to run it, Windows 10 x86 says:
"This App can't run on your PC. To find a version for your PC, check with your software publisher."
It seems to have done something that Win10 does not approve of.

Tim: Your suggestion works perfectly, and I like that I don't need to use an undocumented function. It's also nice that I can restore the original icon for CMD.EXE when I exit.

JJ: So if I understand correctly, you are saying that cvtres.exe is not needed after running rc.exe? In any case, my goal is to have the icon appear even when running it from the Command Prompt.

Title: Re: Creating an Icon for a Console application
Post by: jj2007 on April 20, 2016, 04:35:02 PM
Quote from: pcMike on April 20, 2016, 03:05:48 PMJJ: So if I understand correctly, you are saying that cvtres.exe is not needed after running rc.exe? In any case, my goal is to have the icon appear even when running it from the Command Prompt.

In case you already have \Masm32\MasmBasic\RichMasm.exe on your disk, attached an example. Just replace Eye.ico with, for example, globe.ico and hit F6.

The point is that Windows knows what to do with an icon in the resource section. But check also the OPT_Run parameter.

Note you need MasmBasic's editor (http://masm32.com/board/index.php?topic=94.0) but the code generated is plain Masm32.
Title: Re: Creating an Icon for a Console application
Post by: nidud on April 20, 2016, 09:51:58 PM
deleted
Title: Re: Creating an Icon for a Console application
Post by: Siekmanski on April 20, 2016, 10:49:23 PM
Works on Win8.1 64
Title: Re: Creating an Icon for a Console application
Post by: Vortex on April 21, 2016, 05:08:57 AM
A resource linker can be used to add an icon to an executable :

QuoteThis small command line tool links a binary Win32 resource file (*.res) into an executable (exe or dll).
Binary resource files you can create using the Borland Resource Workshop, with the Microsoft Resource Compiler and many other resource compilers.
First build your application (exe/dll) using your favorite compiler and after You can link the resources into your application using RLINK32.EXE

http://www.rowalt.de/pc/programming/toolse.htm
Title: Re: Creating an Icon for a Console application
Post by: pcMike on April 21, 2016, 07:52:17 AM
nidud: Where can I download asmc?

Vortex: The ConsoleIcon.exe shows the icon when I run it from explorer or QuickEditor, but not from the Command Prompt.  Interestingly I can run your build.bat and run ConsoleIcon.exe, but if I use that batch file to build my console App, then when I run it I get the same Windows 10 error "This app can't run on your PC. To find a version for your PC, check with the software publisher" which I also got when using WRC.EXE.

I narrowed down the problem to my program defining an 8k buffer in .data?
If I change this to a 1k buffer and run your build.bat again, then Windows 10 runs it without the error.

.386
.model flat,stdcall

option casemap:none
   include \masm32\include\windows.inc
   include \masm32\include\user32.inc
   include \masm32\include\kernel32.inc
   include \masm32\include\masm32.inc
   include \masm32\macros\macros.asm
   includelib \masm32\lib\user32.lib
   includelib \masm32\lib\kernel32.lib
   includelib \masm32\lib\masm32.lib

.data?
;----------------------------------------------------
   wcCHAR_INFO     db 8000 dup (?) ; CHAR_INFO Buffer
;----------------------------------------------------
.code

start:
   print "hello world",13,10
   invoke ExitProcess,0
end start

Title: Re: Creating an Icon for a Console application
Post by: nidud on April 21, 2016, 08:11:29 AM
deleted
Title: Re: Creating an Icon for a Console application
Post by: pcMike on April 21, 2016, 10:12:50 AM
nidud: Yes the exe file worked.

As I mentioned when I used either RLink32 or WRC to add the icon resource to my .exe file, the result was the same with Windows 10 (x86) telling me "This app can't run on your PC".

It took me a moment to find that green button to my left, but it took me to asmc and also DOSZIP Commander which looks even better then Norton Commander. I haven't looked at asmc yet but I will check it out. Thanks!