Another program icon via command line

New to FreeBASIC? Post your questions here.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Another program icon via command line

Post by srvaldez »

@newbieforever
I find your small font posts annoying
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: Another program icon via command line

Post by newbieforever »

@srvaldez: Tiny font means:
All these weird and wonderful nerds here make life difficult for the newbieforever with their childish quirks.
The nerd newbieforever has to take revenge somehow.

Sorry, it's all a joke, do not take it so seriously.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Another program icon via command line

Post by srvaldez »

@newbieforever
yes of course, but the older I get, the less my sense of humor.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: Another program icon via command line

Post by newbieforever »

@srvaldez: In my case, it's the other way round.
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Another program icon via command line

Post by St_W »

Actually you have posted the solution yourself, with your AutoHotkey code, which just seems to call some regular Win32 API methods. I haven't used any of those yet (and tbh didn't even know of their existence), but it should be pretty straightforward to translate the AHK code to FB.

There even seems to be an example in MSDN on updating resources:
https://docs.microsoft.com/en-gb/window ... -resources

PS: I find your tiny fonts annoying too.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Another program icon via command line

Post by srvaldez »

there are quite a large number of web articles related to changing the icon at runtime https://www.codeguru.com/cpp/w-p/win32/ ... ources.htm
but I simply don't have the expertise.
using fbfrog you get a good start at translating the code to FB

Code: Select all

#pragma once

#include once "crt/long.bi"
#include once "io.bi"
#include once "crt/stdio.bi"
#include once "crt/stdlib.bi"
#include once "fcntl.bi"
#include once "windows.bi"

extern "C"

type GROUPICON field = 2
	Reserved1 as WORD
	ResourceType as WORD
	ImageCount as WORD
	Width as BYTE
	Height as BYTE
	Colors as BYTE
	Reserved2 as BYTE
	Planes as WORD
	BitsPerPixel as WORD
	ImageSize as DWORD
	ResourceID as WORD
end type

private sub InjectMainIcon(byval Where as zstring ptr, byval What as zstring ptr)
	'' TODO: HANDLE hWhere = BeginUpdateResource(Where, FALSE);
	dim buffer as zstring ptr
	dim buffersize as clong
	dim hFile as long
	hFile = open(What, O_RDONLY or O_BINARY)
	if hFile = (-1) then
		return
	end if
	buffersize = filelength(hFile)
	buffer = cptr(zstring ptr, malloc(buffersize))
	read(hFile, buffer, buffersize)
	close(hFile)
	UpdateResource(hWhere, RT_ICON, MAKEINTRESOURCE(1), MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), buffer + 22, buffersize - 22)
	dim grData as GROUPICON
	grData.Reserved1 = 0
	grData.ResourceType = 1
	grData.ImageCount = 1
	grData.Width = 32
	grData.Height = 32
	grData.Colors = 0
	grData.Reserved2 = 0
	grData.Planes = 2
	grData.BitsPerPixel = 32
	grData.ImageSize = buffersize - 22
	grData.ResourceID = 1
	UpdateResource(hWhere, RT_GROUP_ICON, "MAINICON", MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), @grData, sizeof(GROUPICON))
	'' TODO: delete buffer;
	EndUpdateResource(hWhere, FALSE)
end sub

private sub main(byval argc as long, byval argv as zstring ptr ptr)
	if argc <> 3 then
		printf("Usage: chicon [executable filename] [icon filename]")
		return
	end if
	InjectMainIcon(argv[1], argv[2])
end sub

end extern
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Another program icon via command line

Post by fxm »

@newbieforever
You play constantly with this tiny police and it's annoying.
Therefore I lost interest in all your posts (and I'm certainly not the only one).
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Another program icon via command line

Post by dodicat »

srvaldez
you frog translation works.
chicon.bas
compile to exe

Code: Select all



#include once "crt/long.bi"
#include once "crt/io.bi"
#include once "crt/stdio.bi"
#include once "crt/stdlib.bi"
#include once "windows.bi"
#include "file.bi"

type GROUPICON field = 2
   Reserved1 as WORD
   ResourceType as WORD
   ImageCount as WORD
   Width as BYTE
   Height as BYTE
   Colors as BYTE
   Reserved2 as BYTE
   Planes as WORD
   BitsPerPixel as WORD
   ImageSize as DWORD
   ResourceID as WORD
end type

 sub InjectMainIcon(byval Where as string , byval What as string)
  
   var hwhere= beginupdateresourceA(where,0)
   dim buffer as zstring ptr
   dim buffersize as clong
   dim hFile as any ptr
   hFile = fopen(What,"rt+")', O_RDONLY or O_BINARY) 'two Irishmen ??
   if hFile = (-1) then
      return
   end if
  
   
   fseek(hfile, 0, SEEK_END)' // seek to end of file
   buffersize = ftell(hfile)' // get current file pointer
   fseek(hfile, 0, SEEK_SET)'re set
   buffer = cptr(zstring ptr, malloc(buffersize))
    
   fread( buffer,buffersize,1,hfile)
   
   fclose(hFile)
   UpdateResource(hWhere, RT_ICON, MAKEINTRESOURCE(1), MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), buffer + 22, buffersize - 22)
   dim grData as GROUPICON
   grData.Reserved1 = 0
   grData.ResourceType = 1
   grData.ImageCount = 1
   grData.Width = 32
   grData.Height = 32
   grData.Colors = 0
   grData.Reserved2 = 0
   grData.Planes = 2
   grData.BitsPerPixel = 32
   grData.ImageSize = buffersize - 22
   grData.ResourceID = 1
   UpdateResource(hWhere, RT_GROUP_ICON, "MAINICON", MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), @grData, sizeof(GROUPICON))
    free (buffer)
   EndUpdateResource(hWhere, FALSE)
end sub


      printf("Usage: chicon [executable filename] [icon filename]")
    if fileexists(command(1))=0 then print "   " +command(1) + " not found":end
    if fileexists(command(2))=0 then print "   " + command(2) + " not found":end
   InjectMainIcon(command(1), command(2))


'InjectMainIcon("pool.exe","icon.ico")  'test OK
'sleep

  
in a folder
From a shell;
chicon file.exe file.ico
You will see the icon changing.

Here is a shell
shell.bas
compile to exe

Code: Select all

shell "cmd"  
Last edited by dodicat on Jul 10, 2018 10:03, edited 1 time in total.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Another program icon via command line

Post by grindstone »

newbieforever wrote:Tiny font means:
All these weird and wonderful nerds here make life difficult for the newbieforever with their childish quirks.
The nerd newbieforever has to take revenge somehow.
It's not a good idea to bully people you want to get help from.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Another program icon via command line

Post by jj2007 »

St_W wrote:There even seems to be an example in MSDN on updating resources:
https://docs.microsoft.com/en-gb/window ... -resources
Actually, a simple example showing how to change an icon in an existing executable. But it can't read directly from a *.ico file, so you would need a bunch of executables containing the desired icons.
Besides, it won't help OP because he needs to embed also his mysterious other executables to be launched.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: Another program icon via command line

Post by newbieforever »

Many thanks to all who helped me here! You are all great!

I know, in your posts here everything is already included, it only has to be written the code. But because I do not know enough about this at the moment, I will use my AHK solution for the time being because I have more knowledge there.

I'm also making good progress with my project, but there are always new insidious problems...
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Another program icon via command line

Post by grindstone »

@newbieforever:
Only for me to know if I got you right: Your intention is that your launcher inherits the icon of the exe it launches. Correct?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Another program icon via command line

Post by dodicat »

I have adjusted the above code slightly.
Please note: With the code as it stands,the .ico file must be 128 by 128 in size.
Also it should be a proper .ico format, not a re-named bitmap file.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: Another program icon via command line

Post by newbieforever »

@grindstone:
Inherit? Not in an automatic way, if that's what you mean.
I am giving the Launcher the icon. Actually, I give it to both––to the Launcher and to the exe which is to be started.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: Another program icon via command line

Post by newbieforever »

@dodicat:
I have no possibility to create a 128 x 128 icon at the moment (IcoFX doesn't support this size), so I can't test it. As you said, it doesn't work with other sizes.

But, as I already said, I am using now the AHK solution, which will be good enogh for the moment (for a Proof-of-the-Concept of my project), so this has not a high priority at the moment.

Especially because I have other problems to deal with...
Post Reply