Solved

Win32 API Noobie - Custom resource not found, error 1812

Posted on 2004-08-01
9
1,503 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

15 Experts available now in Live!

Get 1:1 Help Now