Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Binary resource in Windows service?

Binary resource in Windows service?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhardwarejsonlearning
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R RoyceF

    Hi, I don't know if this is the appropriate forum for this question, but I don't know any other place to post it. I have a Windows service in which I have an embedded binary resource that I want to load at service startup. However, the API functions below fail with an exception or return NULL: Both of these calls throw an exception when called by my service: HINSTANCE hInstance = AfxGetInstanceHandle(); HINSTANCE hInstance = AfxGetResourceHandle(); This always returns a NULL when called by my service: CWinApp* lpApp = AfxGetApp(); hInstance = lpApp->m_hInstance; How can I access this resource? Thanks, Royce

    J Offline
    J Offline
    James R Twine
    wrote on last edited by
    #2

    This might sound like a stupid question, but is it an MFC-based service?  For example, if this is an ATL-based service, functions like that will fail because you are likely to not have a real MFC CWinApp around.    You DID check the pointer returned by AfxGetApp to ensure that it is not NULL right?    Move away from the MFC-based resource functions and take a look at the native Win32 ones, like FindResourceEx(...) and LoadResource if you have no useful MFC "state" available in your service.    Peace! -=- James


    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
    Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
    DeleteFXPFiles & CheckFavorites (Please rate this post!)

    R 1 Reply Last reply
    0
    • J James R Twine

      This might sound like a stupid question, but is it an MFC-based service?  For example, if this is an ATL-based service, functions like that will fail because you are likely to not have a real MFC CWinApp around.    You DID check the pointer returned by AfxGetApp to ensure that it is not NULL right?    Move away from the MFC-based resource functions and take a look at the native Win32 ones, like FindResourceEx(...) and LoadResource if you have no useful MFC "state" available in your service.    Peace! -=- James


      If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
      Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
      DeleteFXPFiles & CheckFavorites (Please rate this post!)

      R Offline
      R Offline
      RoyceF
      wrote on last edited by
      #3

      James, Thanks for your help. No, it is not a stupid question. I should have said that it is a MFC-based service. I tried FindResourceEx() and LoadResource() API functions. They return NULL. AfxGetApp() returns a valid (?) pointer, but the member m_hInstance is NULL. This problem is complicated by the fact that this resource is an encryption key and password and is used by two other applications and numerous DLLs. Difficult to understand, let alone explain to someone else. Thanks, Royce

      J 1 Reply Last reply
      0
      • R RoyceF

        James, Thanks for your help. No, it is not a stupid question. I should have said that it is a MFC-based service. I tried FindResourceEx() and LoadResource() API functions. They return NULL. AfxGetApp() returns a valid (?) pointer, but the member m_hInstance is NULL. This problem is complicated by the fact that this resource is an encryption key and password and is used by two other applications and numerous DLLs. Difficult to understand, let alone explain to someone else. Thanks, Royce

        J Offline
        J Offline
        James R Twine
        wrote on last edited by
        #4

        Have you tried calling FindResourceEx with a NULL instance parameter directly?  Also, try using FindResource if language is not an concern (and it sounds like it is not).    Also, how are the resources identified, by number or by string?  Are you using the correct Resource Type value?    Peace! -=- James


        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        DeleteFXPFiles & CheckFavorites (Please rate this post!)

        R 1 Reply Last reply
        0
        • J James R Twine

          Have you tried calling FindResourceEx with a NULL instance parameter directly?  Also, try using FindResource if language is not an concern (and it sounds like it is not).    Also, how are the resources identified, by number or by string?  Are you using the correct Resource Type value?    Peace! -=- James


          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          DeleteFXPFiles & CheckFavorites (Please rate this post!)

          R Offline
          R Offline
          RoyceF
          wrote on last edited by
          #5

          Yes, I tried FindResourceEx() with the NULL instance parameter. The resources are identified by number, their type is "TEXT".

          J 1 Reply Last reply
          0
          • R RoyceF

            Yes, I tried FindResourceEx() with the NULL instance parameter. The resources are identified by number, their type is "TEXT".

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #6

            Are you using MAKEINTRESOURCE to specify the identifying number?    This might go faster if we had a code snippet of the code you are using...    Peace! -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            R 1 Reply Last reply
            0
            • J James R Twine

              Are you using MAKEINTRESOURCE to specify the identifying number?    This might go faster if we had a code snippet of the code you are using...    Peace! -=- James


              If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              DeleteFXPFiles & CheckFavorites (Please rate this post!)

              R Offline
              R Offline
              RoyceF
              wrote on last edited by
              #7

              This code fails - the m_hInstance member is null: CWinApp* lpApp = AfxGetApp(); HINSTANCE hInstance = lpApp->m_hInstance; This code fails - fires an assert in debug, throws an exception in release version: HINSTANCE hInstance = AfxGetInstanceHandle(); This code never executes because of the above failures: HANDLE hHandle1 = LoadResource( hInstance, FindResource( hInstance, MAKEINTRESOURCE( IDR_RES1 ), "TEXT" ) ); HANDLE hHandle2 = LoadResource( hInstance, FindResource( hInstance, MAKEINTRESOURCE( IDR_RES2 ), "TEXT" ) ); This code works in 3 other Windows desktop applications. It just doesn't work here because of the runtime environment, the service. I am trying to find a way around the fact that the embedded resources aren't accessible when the service is loading, or even after it is loaded. Thanks for your help, Royce

              B J 2 Replies Last reply
              0
              • R RoyceF

                This code fails - the m_hInstance member is null: CWinApp* lpApp = AfxGetApp(); HINSTANCE hInstance = lpApp->m_hInstance; This code fails - fires an assert in debug, throws an exception in release version: HINSTANCE hInstance = AfxGetInstanceHandle(); This code never executes because of the above failures: HANDLE hHandle1 = LoadResource( hInstance, FindResource( hInstance, MAKEINTRESOURCE( IDR_RES1 ), "TEXT" ) ); HANDLE hHandle2 = LoadResource( hInstance, FindResource( hInstance, MAKEINTRESOURCE( IDR_RES2 ), "TEXT" ) ); This code works in 3 other Windows desktop applications. It just doesn't work here because of the runtime environment, the service. I am trying to find a way around the fact that the embedded resources aren't accessible when the service is loading, or even after it is loaded. Thanks for your help, Royce

                B Offline
                B Offline
                Blake Miller
                wrote on last edited by
                #8

                If your resources are contained directly within your EXE file, then use GetModuleHandle(NULL) to retrieve the 'instance' handle used for loading the resources :cool:

                1 Reply Last reply
                0
                • R RoyceF

                  This code fails - the m_hInstance member is null: CWinApp* lpApp = AfxGetApp(); HINSTANCE hInstance = lpApp->m_hInstance; This code fails - fires an assert in debug, throws an exception in release version: HINSTANCE hInstance = AfxGetInstanceHandle(); This code never executes because of the above failures: HANDLE hHandle1 = LoadResource( hInstance, FindResource( hInstance, MAKEINTRESOURCE( IDR_RES1 ), "TEXT" ) ); HANDLE hHandle2 = LoadResource( hInstance, FindResource( hInstance, MAKEINTRESOURCE( IDR_RES2 ), "TEXT" ) ); This code works in 3 other Windows desktop applications. It just doesn't work here because of the runtime environment, the service. I am trying to find a way around the fact that the embedded resources aren't accessible when the service is loading, or even after it is loaded. Thanks for your help, Royce

                  J Offline
                  J Offline
                  James R Twine
                  wrote on last edited by
                  #9

                  When you said an "MFC based service" before, do you really mean a MFC service, or an ATL service with "support MFC" checked.  If you have the latter, you need to "fool" MFC by doing the following:

                  	CWinApp	waWA;
                  
                  	waWA.m_hInstance = _Module.GetModuleInstance();
                  	afxCurrentInstanceHandle = _Module.GetModuleInstance();    // Or ::GetModuleHandle( NULL )
                  	afxCurrentResourceHandle = _Module.GetResourceInstance();  // Or ::GetModuleHandle( NULL )
                  

                  Then things that refrence the global CWinApp instance will work correctly.  Kludgy, but that is what you get for mixing MFC and ATL with VC++ 6.0! :)    Peace! -=- James


                  If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  DeleteFXPFiles & CheckFavorites (Please rate this post!)

                  R 2 Replies Last reply
                  0
                  • J James R Twine

                    When you said an "MFC based service" before, do you really mean a MFC service, or an ATL service with "support MFC" checked.  If you have the latter, you need to "fool" MFC by doing the following:

                    	CWinApp	waWA;
                    
                    	waWA.m_hInstance = _Module.GetModuleInstance();
                    	afxCurrentInstanceHandle = _Module.GetModuleInstance();    // Or ::GetModuleHandle( NULL )
                    	afxCurrentResourceHandle = _Module.GetResourceInstance();  // Or ::GetModuleHandle( NULL )
                    

                    Then things that refrence the global CWinApp instance will work correctly.  Kludgy, but that is what you get for mixing MFC and ATL with VC++ 6.0! :)    Peace! -=- James


                    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    DeleteFXPFiles & CheckFavorites (Please rate this post!)

                    R Offline
                    R Offline
                    RoyceF
                    wrote on last edited by
                    #10

                    No, there is no ATL support in this project.

                    1 Reply Last reply
                    0
                    • J James R Twine

                      When you said an "MFC based service" before, do you really mean a MFC service, or an ATL service with "support MFC" checked.  If you have the latter, you need to "fool" MFC by doing the following:

                      	CWinApp	waWA;
                      
                      	waWA.m_hInstance = _Module.GetModuleInstance();
                      	afxCurrentInstanceHandle = _Module.GetModuleInstance();    // Or ::GetModuleHandle( NULL )
                      	afxCurrentResourceHandle = _Module.GetResourceInstance();  // Or ::GetModuleHandle( NULL )
                      

                      Then things that refrence the global CWinApp instance will work correctly.  Kludgy, but that is what you get for mixing MFC and ATL with VC++ 6.0! :)    Peace! -=- James


                      If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                      Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                      DeleteFXPFiles & CheckFavorites (Please rate this post!)

                      R Offline
                      R Offline
                      RoyceF
                      wrote on last edited by
                      #11

                      GetModuleHandle(NULL) returns 0x00400000, which is not a valid handle.

                      J 1 Reply Last reply
                      0
                      • R RoyceF

                        GetModuleHandle(NULL) returns 0x00400000, which is not a valid handle.

                        J Offline
                        J Offline
                        James R Twine
                        wrote on last edited by
                        #12

                        Have you tried using it?  Even if it was invalid, using FindResource(...) with a NULL hModule parameter should default to using the load image (EXE).    Are the resources present in the executable itself, and not in another DLL?  Are you sure they are being linked in correctly?  One way to test is to open a new instance of DevStudio, and open the EXE file being sure to specify "Resources" in the "Open As" combobox.  You can them be sure that the resources are actually there and accessable - you should be able to double-click on them and see them in the binary editor.    I am running out of ideas here...    Peace! -=- James


                        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                        Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                        DeleteFXPFiles & CheckFavorites (Please rate this post!)

                        R 2 Replies Last reply
                        0
                        • J James R Twine

                          Have you tried using it?  Even if it was invalid, using FindResource(...) with a NULL hModule parameter should default to using the load image (EXE).    Are the resources present in the executable itself, and not in another DLL?  Are you sure they are being linked in correctly?  One way to test is to open a new instance of DevStudio, and open the EXE file being sure to specify "Resources" in the "Open As" combobox.  You can them be sure that the resources are actually there and accessable - you should be able to double-click on them and see them in the binary editor.    I am running out of ideas here...    Peace! -=- James


                          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                          Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                          DeleteFXPFiles & CheckFavorites (Please rate this post!)

                          R Offline
                          R Offline
                          RoyceF
                          wrote on last edited by
                          #13

                          Yea, so am I! Yes, when I use the invalid handle, I just get another null.

                          1 Reply Last reply
                          0
                          • J James R Twine

                            Have you tried using it?  Even if it was invalid, using FindResource(...) with a NULL hModule parameter should default to using the load image (EXE).    Are the resources present in the executable itself, and not in another DLL?  Are you sure they are being linked in correctly?  One way to test is to open a new instance of DevStudio, and open the EXE file being sure to specify "Resources" in the "Open As" combobox.  You can them be sure that the resources are actually there and accessable - you should be able to double-click on them and see them in the binary editor.    I am running out of ideas here...    Peace! -=- James


                            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                            Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                            DeleteFXPFiles & CheckFavorites (Please rate this post!)

                            R Offline
                            R Offline
                            RoyceF
                            wrote on last edited by
                            #14

                            Yes, the resources are in the EXE. I have opened the EXE in binary mode with the IDE's resource editor and I can see them.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups