Solved

Win32 API Noobie - Custom resource not found, error 1812

Posted on 2004-08-01
9
1,508 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

13 Experts available now in Live!

Get 1:1 Help Now