Announcement

Collapse
No announcement yet.

Module confusion - FindResource

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Module confusion - FindResource

    One thing that has always bothered me, and I REALLLLLLYYYY need to get straight in my head is "What the heck exactly is a MODULE????" Is it the idea that each exe is a module, each dll is a module? each resource is a module?

    and if it is, then what if a resource is in a dll or exe? are there 2 modules to look for???

    the other part is if I do a FindResource to find a particular resource then most code uses ByVal 0 to indicate the "Module" that created the resource which is fine in most cases is fine, but how do I use it the way it was really intended?

    In my particular case I use FindResource to find a dll or bitmap that I have in a resource file, and then LoadResource to load that particular dll into memory.
    The problem being is that according to docs the "LockResource" has remarks about
    Trying to lock a resource by using the handle returned by the FindResource or FindResourceEx function will not work. You will get back a value that is incorrect and points to random data.
    So how the heck is it CORRECTLY done?????
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    Yes, the EXE is one module and each DLL loaded into memory are separate modules with different handles.

    This allows you to load resources not only from the EXE, but also each DLL loaded into memory.

    The API:

    GetModuleHandle

    can be used to get the module handle for DLL loaded into memory, simply by passing the DLL's name.

    hLib& = GetModuleHandle("MyDLL.dll")

    You can also get the EXE's module handle using:

    hEXE& = GetModuleHandle(BYVAL %NULL)

    These module handles can then be used with the resource API's to read resources from the DLL's and the EXE.
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

    Comment


    • #3
      Module = Compiler output of a single compilation. Will be either an "EXE" or "DLL" PE (portable executable) file.

      For good idea of the number of modules typically loaded with your program...
      Show Loaded modules and path from which loaded June 12 2003
      Show Loaded Modules Source and Executable package for PB and VB
      Visual Basic include files/demo code by Balthasar Indermuehle. (PB code by YT.)

      This is also handy if you don't know "which version" (i.e., from which folder) a particular module is loaded.


      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        To answer part deux ...

        When you are doing the typical sequence of finding, loading and locking a resource, there are three (3) handles involved ..
        - Handle to the module containing the resource
        - 'Find' handle returned by FindResource
        - Resource handle returned by LoadResource

        Typical sequence
        Code:
            hMod      =  GetModulehandle ( module name) 
            hFind      = FindResource (hModule, resource type and name) 
            hRes       = LoadResource (hModule, hFind)
            pResData = LockResource (hRes)
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          I just figured out your problem, Cliff:

          With resources you load and lock; it's with weapons you lock and load.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            FWIW, if you just want to get a certain resource as a string variable you can use the "ResourceAsString" function in this demo:

            User-Defined Resource Demo January 26 2003
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              I always prefer using GetWindowLong(hWnd, %GWL_HINSTANCE) instead of GetModuleHandle(ByVal %NULL) because if you later move code to a dll from an exe, it still works as desired. It will return the handle for whatever it is in (handle of the dll when in the dll, handle of the exe when in the exe).
              Scott Slater
              Summit Computer Networks, Inc.
              www.summitcn.com

              Comment


              • #8
                That's not a good approach Scott.
                The instance handle for the hWnd will be in the module where it's created and might not be the module where you placed your resources.
                Best is to use the instance handle in all cases.
                Yes one global is allowed

                Obtain the instance handle where you get it (winmain/libmain) and not some technique to obtain the instance via another route like via the instance name or some hwnd.
                hellobasic

                Comment


                • #9
                  I just figured out your problem, Cliff:

                  With resources you load and lock; it's with weapons you lock and load.
                  from my military days, "Lock and Load" makes perfect sense because you know what you are loading

                  From my Programming days then you Find what you are loading, before you load the wrong round.

                  but according to the doc's
                  Trying to lock a resource by using the handle returned by the FindResource or FindResourceEx function will not work. You will get back a value that is incorrect and points to random data.
                  Would I take that literally that once I loaded then the loading somehow magically gives me the correct value? or am I stuck on guessing I am loading the correct round????

                  aka...if you hand me a 12 guage and it turns out to be a 22, then sure it may or may not fire (if I am lucky), but does that make it right????
                  Engineer's Motto: If it aint broke take it apart and fix it

                  "If at 1st you don't succeed... call it version 1.0"

                  "Half of Programming is coding"....."The other 90% is DEBUGGING"

                  "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                  Comment


                  • #10
                    The doc is correct; just, shall we say, not on the short list for ANY of the Pulitzer Prizes.

                    Just follow the sequence I gave above and you'll be just fine.
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment

                    Working...
                    X