Solved

Save hicon to a .ico file

Posted on 2002-03-03
10
2,957 Views
Last Modified: 2013-12-03
Q: How to save an HICON to a .ico file

VC6, Needs to work on Win 95 and up. Can be MFC or Win API.

Ideally work for whatever the hicon contains, but failing that a version which works with 32X32 16 color icons would do.



BOOL MySaveIcon( hIcon, LPCSTR lpszFile )
{
// <-------what goes here
}


int main( int argc, char * argv[] )
{
HICON hIcon ;
// etc. etc. lots of stuff setting up hIcon

MySaveIcon( hIcon "c:\\example.ico" ) ;
}
0
Comment
Question by:Answers2000
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6838273
HANDLE hIcon; //handle to Icon
int nHandleSize = ::GlobalSize(hIcon);
LPSTR lpIcon = ::GlobalLock(hIcon);

//simply write nHandleSize bytes of the data in lpIcon to
// the file on the disk. Don't forget to do

::GlobalUnlock(hIcon);

0
 
LVL 6

Expert Comment

by:MichaelS
ID: 6838598
0
 
LVL 86

Expert Comment

by:jkr
ID: 6841573
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 86

Expert Comment

by:jkr
ID: 6841574
Hum, that's actually pretty much what Axter described...
0
 
LVL 30

Expert Comment

by:Axter
ID: 6842366
jkr,
It should be.
I copied it off one of your previous answers. :-) :-O
0
 
LVL 8

Author Comment

by:Answers2000
ID: 6843120
Michael, nope, it needs to be ico format

Axter/JKR - if this work'd it be great - funny that I get 0 back for nHandleSize (in nAxter's ver), I'll go back and check the rest of the stuff to figure out if it's me or this method simply doesn't work in win32...  btw I'm testing on Win98 at the moment, but it needs to work on everything from 95 and NT4 upwards.

In case you're wondering, I'm adding a mini-icon editor to my prog (MFC) as a "hobby" (i.e. not much time to work on this) in between doing the real work on it. The darn thing only needs to work on 32X32 16 color icons, and this one last thing to solve.

0
 
LVL 86

Expert Comment

by:jkr
ID: 6843198
>>It should be.
>>I copied it off one of your previous answers. :-) :-O

It'd better be one graded "A" :o)
0
 
LVL 30

Expert Comment

by:Axter
ID: 6844006
Answers2000,
Can you post the code you have in the relevant section?
0
 
LVL 8

Author Comment

by:Answers2000
ID: 6965573
Sorry for delay

I'm sure your answer is right.

The test program I have uses all this library of code (not by me!) to make the icon, and this is when I couldn't make it save.   The bug seems to be in the library, which I don't have time to debug - so I've shelved this hobby project for the time being.
0
 

Expert Comment

by:eliakaris
ID: 13339273
I am trying to use this code in c# using .NET interop and this does not seem to be working.

I am using the Handle property of the icon.
All I receive from the GlobalSize call is zero...

here are my P/Invokes...
[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);

[DllImport("kernel32.dll")]
static extern uint GlobalSize(IntPtr hMem);

[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);

Doesnt work with LocalLock either.

Elia
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apparent collision of square brackets in jQuery and Classic ASP 12 30
Build VS2015 project in bat command 3 101
Need more details 5 182
Graph API & MS Apps 1 53
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question