Solved

Win32 API Noobie - Custom resource not found, error 1812

Posted on 2004-08-01
9
1,528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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 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…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

734 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