Solved

Win32 API Noobie - Custom resource not found, error 1812

Posted on 2004-08-01
9
1,510 Views
Last Modified: 2013-12-03
I'm a beginning Win32 API programmer trying to get to grips with some of Petzold's examples.

The example here is the PoePoem program that illustrates use of Custom Resources.  The custom resource is an external text file, PoePoem.txt, in ASCII.

The .RC Script has the following entry...

    ////////////////////////
    //
    // TEXT
    //
    ANNABELLEE TEXT DISCARDABLE "PoePoem.txt"

The RESOURCE.H header file has this entry...

    #define ANNABELLEE 102

When I load the compiled exe into a hex editor I can see the text is included in the file, so I know the resource is 'on board'.

However, when I try to load the resource...

    hResource = LoadResource (hInst, FindResource (hInst, TEXT("ANNABELLEE"),TEXT ("TEXT"))) ;

...I get Error 1812 "The Specified Image File did not contain a resource section".  Having isolated the FindResource statement I am sure that it is returning a null pointer, so it either doesn't like the ANNABELLEE reference, or it doesn't like the custom TEXT reference.

If I try to get cute with it and force it to use 102...

    hResource = LoadResource (hInst, FindResource (hInst, TEXT("#102"),TEXT ("TEXT"))) ;

... I get Error 183, "Cannot create a file when that file already exists", which is odd if I'm trying to get a handle to a resource.

I'm using Visual Studio 6, on Windows 2000.

Your help would be appreciated.
0
Comment
Question by:Hiblet
  • 5
  • 4
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11688763
I feel your hInst is not correct. Please specify how have you obtained.
0
 

Author Comment

by:Hiblet
ID: 11688899
The hInst variable is declared at Global scope, and it *seems* to come back filled from a child scroll bar creation...

         hScroll = CreateWindow (TEXT ("scrollbar"), NULL,
                                  WS_CHILD | WS_VISIBLE | SBS_VERT,
                                  0, 0, 0, 0,
                                  hwnd, (HMENU) 1, hInst, NULL) ;
 
However, when I debug this, I can see hInst has a null value, which probably means this is Win95 code???

I'm thinking that the FindResource call needs the handle to the parent window to get to the resource, and instead it's currently using a handle to the child control that returns null in Win2k.  How do I get the right handle?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11689052
FindResource  don't need the parent to a window (HWND). It need a parent to your application (HINSTANCE).

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Hiblet
ID: 11689161
OK, so I set up a global variable hInst, and set it to the value of hInstance in WinMain.  In this way, it should be in scope, and should hold a value for when I'm in the call back Windows procedure.

Debugging shows that the value is held, to the Callback procedure.  So far so good, I have a valid hInst value now, not null, and it's the value of hInstance from the WinMain routine.

I have also set up an HRSRC hRes variable to hold the resource handle before attempting to loadResource it.

As such I can now get the error from FindResource, even when it has (what I believe to be is a) valid handle to the current instance.  

Now I get error 1814: The specified resource name cannot be found in the image file, which at least is a bit more sensible than previous errors...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11689236
try with

hResource = LoadResource (hInst, FindResource (hInst, MAKEINTRESOURCE(102),TEXT ("TEXT"))) ;


0
 

Author Comment

by:Hiblet
ID: 11689385
With this, I get Error 183 "Cannot create a file when that file already exists".
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 11689429
Who is the error from? LoadResource or FindResource?
0
 

Author Comment

by:Hiblet
ID: 11689459
This is from FindResource, now that I've separated it from the composite LoadResource statement.

*Here, our Noobie programmer figures it out*

I have left the error trap in the path, so no matter what happens, I get an error.  I have just take the error trap out, and the programme now works.

I think the correction occurred when you pointed the finger at hInst, so I couldn't have got it working without this help.  Petzold's code does not set hInst to the hInstance value, so that was where it was going wrong, hInst was NULL, hence the FindResource returned NULL also.

Job done.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11689472
Ok, glad to help you. See you on the next Noobie question. ;-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now