4

If I've got a resource in an EXE and I want to extract it, it's pretty simple. Something like:

stream := TResourceStream.Create(HInstance, ResourceName, RT_RCDATA);

This works because the global variable HInstance is a handle to the EXE. Is there any similar way for code within a BPL to get a handle to the package it's a part of so I can extract a resource from it?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477

1 Answers1

7

HInstance is actually in the SysInit unit which is unique for each PE (package, exe, dll). System, where the bulk of the compiler RTL and helper functions live is shared among packages if using the rtl package.

So, the code you showed above should work fine from within a package. MainInstance is probably what you're thinking of as being the handle to the loading exe (or even DLL in some cases).

Allen Bauer
  • 16,657
  • 2
  • 56
  • 74
  • Yes, SysInit.HInstance is the instance for the module in which the executing code resides. – David Heffernan Mar 21 '11 at 07:39
  • Well, theoretically that's very nice, but it's not working. I have a unit in a BPL with a line like this, and it raises a not found error. I've used ResourceHacker to verify that the resource is there, under the right name. Any idea what's going on? – Mason Wheeler Mar 21 '11 at 14:41
  • Just tested it to be sure, and the `HInstance` value from the unit within the BPL is identical to the value returned from a unit within the EXE hosting the BPL. – Mason Wheeler Mar 21 '11 at 15:29
  • Make sure you don't have a global variable called HInstance in some other shared unit. You can also qualify the ident with SysInit.HInstance to ensure you're getting the right one. – Allen Bauer Mar 21 '11 at 15:39
  • Nope. `SysInit.HInstance` returns the same value. Strangely enough, so do `FindHInstance(@ThisProc)` and `FindClassHInstance(TFormInThisBPL)`. – Mason Wheeler Mar 21 '11 at 16:12
  • 1
    That probably means your unit is compiled into the main executable. – Ondrej Kelle Mar 22 '11 at 15:39