CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2010
    Posts
    11

    [RESOLVED] Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Hello, my name is Justin! I'm currently a 3rd year Junior majoring in CSE, and I'm learning a bit about Windows programming on my own outside of class. The book I am currently reading from is "Tricks of the Windows Game Programming Gurus 2nd Edition" by Andre LaMothe. I'm on Chapter 3 right now, and I have run into some difficulty with resource files. I have been searching the internet for 3 hours (it is now 4:39am!) for a problem I have been having, yet am still unable to find a solution! After searching this forum and reading the available FAQs, I decided to post to see if I could have any luck here! I'm not sure if this is the appropriate forum for this thread, so I apologize if it isn't.

    This is what I want to do: I want to make a .rc file, a corresponding .h file, a .ico file, and have my compiler link and successfully create an executable that has my personal .ico file in its place, as well as in it's upper window. From what I understand, the compiler will generate some .res file, and then linking my .h, .cpp, .rc, and .ico file, it will generate an .exe file. My program is quite simple; it simply displays a simple 400 x 400 black window. No menus, nothing else. I have successfully gotten this to work (displaying the window), I just need to be able to get the icon thing to work!

    Originally, I was using Visual C++ Express Edition 2008 for this "project". After some research using google, I discovered that it doesn't support resource editing. I downloaded a tool called ResEdit, but this isn't really what I want to do. Sure, it's great for creating the .rc and respective .h files, but I want to learn to do this on my own! I looked at some other options, but for me they were a bit more complicated and time consuming that I could handle.

    My next step was using Visual C++ 6.0 Standard Edition. This supported resource files, but again it seems more for creating them then simply linking them over all. I'm sure I'm over-complicating this problem, but I sure would love some help!

    For reference, here is some information regarding my files:

    File 1: "resource.rc"

    #include "resource.h"
    id_icon1 ICON star.ico

    File 2: "resource.h"

    #define id_icon1 100

    File 3: "main.cpp"

    //this is simply part of the winclass I am registering

    winclass.cbSize = sizeof(WNDCLASSEX);
    winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc = WindowProc; //how the messages are handled
    winclass.cbClsExtra = 0;
    winclass.cbWndExtra = 0;
    winclass.hInstance = hinstance;
    winclass.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(id_icon1));
    winclass.hCursor = LoadCursor(hinstance, IDC_ARROW);
    winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    winclass.lpszMenuName = NULL;
    winclass.lpszClassName = WINDOW_CLASS_NAME;
    winclass.hIconSm = LoadIcon(hinstance, MAKEINTRESOURCE(id_icon1));

    File 4: "star.ico"

    I was quite certain to use an '#include "resource.h" in my main.cpp file. All my files, including star.ico are contained in the same directory, my "Source" folder, for either compiler. I will be quite honest; I have yet to make an executable yet (I haven't gotten that far with these new programs for me), but just simply seeing my "hIconSm" in the window would make me happy! If I can at least figure out how to get this to work, I can easily handle the rest on my own! Thank you for any time that can be offered to assist me!!!

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Once you have begun using Visual C++ 6.0 which has a resorce editor, use that resource editor and avoid manually editing of resource script files as well as using some external tools.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Feb 2010
    Posts
    11

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Without a doubt, I do indeed plan on using the built in resource editor. Reading ahead in my book, it seems like creating menus will become a pain unless I use the editor. I don't plan on manually editing these files long-term-wise (especially in the case of menus), but for the present I still want to learn how to manipulate these files without the resource editor. I feel like I could learn a lot if I could start without it, and then learn to use it later. Sorry for being so stubborn!

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Once having a resource editor, the maually editing of resource script files (and I'm not only talking about menus) is a kind of masochistic waste of time.
    In very rare cases we really have to do that.
    Instead, we can focus on another better goals like for example learning Windows API, which oho... it a huge domain that never let us bored.

    However, if you really, really want to learn the resource file statements, you can find them in the (MSDN) documentation.
    Well, last but not the least: which is the real problem?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Feb 2010
    Posts
    11

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Yes, I agree that overall, it can become a waste. However, I'm not planning on learning the entire Windows API. I just need enough "bare-bones" coding to get me into Direct X. Besides, I'm a control-freak (as some programmers tend to be), I want to make my own files and link/compiler them without all the extra tags Visual C++ puts in.

    I'm not sure what you mean when you ask "what's the real problem?". I'm doing this for fun, though I am embarrassed to say that despite being a Junior, my programming capabilities are quite novice. I have trouble understanding a lot of things, which is why I was hoping to get some help on this forum! I checked through the MSDN, but I was unable to find anything pertaining to what I actually wanted to do. Sure, I found topics on resource files and creating them, but not linking and compiling my own personal ones. Perhaps am I simply looking for the wrong thing?

    Thanks again for your time!

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Quote Originally Posted by jroeser View Post
    I'm not sure what you mean when you ask "what's the real problem?".
    I too don’t understand what the problem you are trying to solve is.
    Of course you do not “need” to use resource editor; it simply is a convenience tool.
    The code you have in your first post should work, provided that you build it properly. BTW, how do you do that? Do you use your Visual Studio? What error(s) are you getting? Did you add that resource file to your project?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  7. #7
    Join Date
    Feb 2010
    Posts
    11

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Well, now I am really getting frustrated! I can't even get my program to compile now!!! I just wanted to learn a bit about Microsoft Programming, and it seems like no matter how much time I spend trying to fix this problem, things get worse!!!

    To me, I really don't mind what compiler I use - I just want to be able to easily code my program and run it. Maybe it's my inexperience in using compilers that is leading to so such frustration, or maybe I just haven't set a simple option. Regardless, please allow me to go back a few steps and see if I can get assistance getting my simple program running on Visual C++ 6.0, which allows for resource editing. After that, I'd like to approach the real problem at hand (trying to get my resources to compile correctly)! Please note that I have no problem getting this project to work (without the resources) on Visual C++ 2008 Express Edition, just on Visual C++6.0 Standard Edition.

    Steps I do:

    1.) File > New > Projects > Win32 Console Application
    Project Name is "WindowsPractice"

    2.) Click on "File View" (not from the upper menu), right click on the main project, and left click on "Add Files to Project..."

    3.) I add the file "main.cpp" from my desktop.
    This file contains the following code (which only displays two, empty white windows):



    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h> //windows headers
    #include <windowsx.h> //macros
    #include <stdio.h>
    #include <math.h>
    
    #define WINDOW_CLASS_NAME "MYCLASS"
    
    //my super awesome message handler
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    	PAINTSTRUCT ps;
    	HDC hdc;
    	switch(msg)
    	{
    	case WM_CREATE:
    		{
    			return(0);
    		}
    		break;
    
    	case WM_PAINT:
    		{
    			hdc = BeginPaint(hwnd, &ps);
    			EndPaint(hwnd, &ps);
    			return(0);
    		}
    		break;
    
    	case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			return(0);
    		}
    		break;
    
    	default:break;
    	}
    
    	//let Windows handle the rest
    	return (DefWindowProc(hwnd, msg, wparam, lparam));
    
    }//WinProc is over
    
    //WINMAIN - consider this your main loop
    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
    {
    	WNDCLASSEX winclass; //holds the class we create
    	HWND hwnd; //generic window handle
    	MSG msg; //generic message
    
    	//first we have to define what our structure will be
    	winclass.cbSize = sizeof(WNDCLASSEX);
    	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    	winclass.lpfnWndProc = WindowProc; //how the messages are handled
    	winclass.cbClsExtra = 0;
    	winclass.cbWndExtra = 0;
    	winclass.hInstance = hinstance;
    	winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	winclass.hCursor = LoadCursor(hinstance, IDC_ARROW);
    	winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	winclass.lpszMenuName = NULL;
    	winclass.lpszClassName = WINDOW_CLASS_NAME;
    	winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
    	//register this class now
    	if (!RegisterClassEx(&winclass))
    		return(0);
    
    	//----------------------------------------------------------------------
    	//create the window time!!!
    	if (!(hwnd = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "My first window!",
    		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    		0,0, //inital x and y
    		400,400, //initial width, height
    		NULL,
    		NULL,
    		hinstance,//histance of application
    		NULL)))
    		return(0);
    
    	//----------------------------------------------------------------------
    	//let's create a second window
    	if (!(hwnd = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "Here is a second window!!!",
    		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    		0,0,
    		600,800,
    		NULL,
    		NULL,
    		hinstance,
    		NULL)))
    		return(0);
    
    
    	//main event loop time
    	while(GetMessage(&msg, NULL, 0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	//return to basic Windows
    	return(msg.wParam);
    }
    I hit "Control + F7" to compile it (0 errors and 0 warnings).
    Then, I hit "Control + F5" to run it, and I get this error:


    --------------------Configuration: WindowsPractice - Win32 Debug--------------------
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/WindowsPractice.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Windows7.exe - 2 error(s), 0 warning(s)


    Why am I able to compile this program so easily on the express edition, yet not at all on the 6.0 edition? Perhaps if I can resolve this issue, it may take care of my resource problem as well!

    [ Added by ovidiucucu ]
    Please place source code between [CODE] tags!
    See BB Code List.
    Last edited by ovidiucucu; February 4th, 2010 at 02:07 AM. Reason: Added [CODE] tags for source code

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Quote Originally Posted by jroeser View Post
    Regardless, please allow me to go back a few steps and see if I can get assistance getting my simple program running on Visual C++ 6.0,
    First, please use code tags when posting code, as the code you posted becomes unformatted and almost unreadable.

    A console program must have a main() entry point, and your program does not have one. It has a WinMain(), but not a main() entry point. In other words, when you choose "Windows Console Program", your program has to be a "normal" C++ program. This means it has to have a main() function, just like the C++ examples you find in any book. Inside of this "normal" program, you can make use of the Windows API as much as you want. The WinMain() is the entry point for a Windows GUI program.

    Secondly, Visual C++ 6.0 is very old (over 10 years old), obsolete (pre-standard ANSI C++ compiler), and no longer updated. It should only be used for upkeeping legacy apps that have not (for some reason) migrated themselves to the newer versions of Visual C++. Version 6.0 not only compiles broken C++ code as if nothing's wrong, it will also fail to compile legitimate ANSI C++ code.

    For example, if you get code that you like a lot, but it uses anything advanced in terms of C++, you will more than likely find it very hard, if not impossible for such an old compiler like 6.0 to compile it. Even library vendors drop support for 6.0 because the compiler cannot handle ANSI C++ properly.

    There was a thread in the other forum a few weeks ago, and the poster had a memory overwrite bug in their app. The app was written using Visual 6.0's broken use of the for() loop initialization variable. None of the people here as far as I remember could help the person, since we were getting hundreds of compiler errors when trying to compile the broken for-loops that 6.0 accepted.

    So you are better off using the latest compilers, and not one that is "broken" in terms of it being non-ANSI compliant.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Quote Originally Posted by jroeser View Post
    Why am I able to compile this program so easily on the express edition, yet not at all on the 6.0 edition? Perhaps if I can resolve this issue, it may take care of my resource problem as well!
    You are compiling this OK in VC 6.0 as well.
    I bet you couldn't link your project (the same one) in VC 2008. The chosen "console" type implies entry point main(). You have WinMain() that is typical for WINDOWS subsystem.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Quote Originally Posted by jroeser View Post
    I'm not sure what you mean when you ask "what's the real problem?". I'm doing this for fun...
    I looked through the whole thread... You know what? What occured to me first was exactly the same: what's the real problem? There are lots of lines about resources, and some misty things about desperate switching among compilers. Finally it seems you just able to compile, but unable to link your executable. So, what was the point of mentioning about resource editing?

    As long as you intend to become an engineer, first thing you need to learn is the protocol for solving problems:
    • dissect your problem for simpler parts,
    • ask simple questions separately,
    • try the solutions separately,
    • and only after having a firm grip on all of them, integrate all the parts together.


    You've missed the first step, that's why you were unable to ask simple questions, that was the reason of having no specific answers you would be able to try out.

    You see? You just follow the protocol and you're saved.
    Last edited by Igor Vartanov; February 5th, 2010 at 06:33 AM.
    Best regards,
    Igor

  11. #11
    Join Date
    Feb 2010
    Posts
    11

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    Thank you so much for the replies, guys and girls!

    Quote Originally Posted by Paul McKenzie
    First, please use code tags when posting code, as the code you posted becomes unformatted and almost unreadable.
    Sorry! I couldn't find the tags needed for it and was in a hurry at the time. I'm familiar with them now, and I will be sure to use them in my post from now on!

    A console program must have a main() entry point, and your program does not have one. It has a WinMain(), but not a main() entry point. In other words, when you choose "Windows Console Program", your program has to be a "normal" C++ program. This means it has to have a main() function, just like the C++ examples you find in any book. Inside of this "normal" program, you can make use of the Windows API as much as you want. The WinMain() is the entry point for a Windows GUI program.
    After reading this quote, I had one of those life-changing "DUH!!!" moments. I changed from a Win32 Console Application to just a Win32 Application. It worked immediately after this, so that relaxed me a lot. Thank you.

    Secondly, Visual C++ 6.0 is very old (over 10 years old), obsolete (pre-standard ANSI C++ compiler), and no longer updated. It should only be used for upkeeping legacy apps that have not (for some reason) migrated themselves to the newer versions of Vis ual C++. Version 6.0 not only compiles broken C++ code as if nothing's wrong, it will also fail to compile legitimate ANSI C++ code.
    To be quite frank, I don't really like Visual C++ 6.0 much at all. The only reason I ever switched compilers to begin with was due to the fact that Visual C++ 6.0 supports resource editing, whereas the express edition does not. The book also uses Visual C++ 6.0 as well, as well as the one I intend to use afterwords. For this reason, it may be prudent of me to familiarize myself with this particular compiler, and then when I become more confident and fluent in my programming skills, make the transfer to a more suitable long-term compiler (if "long-term" is indeed the appropriate word of choice here).

    Quote Originally Posted by Igor Vartanov
    I looked through the whole thread... You know what? What occured to me first was exactly the same: what's the real problem?
    Thank you for taking the time to give me some advice! I will certainly keep it in mind when I ask questions in the future!

    I feel my problem has already been broken down into its simplest form. It just so happened that as I was troubleshooting this problem, another problem came along! I think the real issue here is my lack of clarity, which comes from my overall lack and experience in computing. Please allow me one more chance to see if I can succinctly explain the problem:


    Problem
    I can't load a custom icon into my window, using my own self-created resource files (as opposed to ones that can be used from the tools a given compiler supplies.)

    That's it! Nothing more! Now, when I analyze this problem, the first thing I ask myself is: Is the code correct? I know the code is correct, because I'm copying it straight from the book, with only a few of my own personal adjustments. So I know the code cannot be incorrect. Next I ask myself is if the file extensions are correct. They are indeed correct as a .cpp, .rc, and .h file. I've also remembered put an #include "resource.h" tag in the main code.

    This leaves only one option left: somehow, I'm not compiling and/or linking the code correctly. Researching google on resources and compilers show that I need an external tool or a compiler that supports resource editing. Well, now I have one! So perhaps I should ask my question as follows:

    Given the following files all contained in the "Source Files" directory of Visual C++ 6.0 Standard Edition:

    star.ico
    resource.h
    resource.rc
    main.cpp

    How do I correctly compile and run this program to create two windows with my custom icon? Also, take note that the previous code I supplied was just to create two windows. The newer code I want to use has an include tag for the header file, and winclass.hIconSm becomes:

    Code:
    winclass.hIconSm = LoadIcon((hinstance, MAKEINTRESOURCE(id_icon1));
    I'd like to thank everyone once again for taking the time to help me and offer their advice. It has not fallen on daft ears!

    Edit:

    Sorry, I almost forgot. Currently, when I try to run my program, this is the error message I receive:

    --------------------Configuration: MenuProgram - Win32 Debug--------------------
    Compiling resources...
    C:\Documents and Settings\ROESER\Desktop\WindowsProgramming\MenuProgram\resource.h(1) : fatal error RC1004: unexpected end of file found
    Error executing rc.exe.

    MenuProgram.exe - 1 error(s), 0 warning(s)
    Last edited by jroeser; February 6th, 2010 at 06:14 AM.

  12. #12
    Join Date
    Feb 2010
    Posts
    11

    Re: Difficulty Compiling Resource files (.rc, .h, and .res) in Visual C++

    I have found the problem. While I was googling this issue and experimenting with Visual C++, I tried googling the error I most recently posted with a a few different keywords I hadn't tried before. As it turns out, there is a thread on here from about five years ago:

    http://codeguru.earthweb.com/forum/s...d.php?t=362550

    And it was on the second page that I found my answer:

    My code was technically correct, though for some reason I needed to have a new line at the end of a file. I simply went to my resource.h file, hit enter, and saved the file. Then.....the program still didn't work! But after googling the next error, I realized you can't save a file in paint as .ico. So I downloaded a tool, remade the icon, crossed my fingers, and ran it again. It was a complete success! With my program running without any problems, I can happily (albeit somewhat masochistically) continue my adventure into the world of the Windows API.

    Thanks to everyone for their help and suggestions! I'll be looking forward to harassing you guys again sometime soon!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured