Binary resource in Windows service?
-
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
-
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
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 byAfxGetApp
to ensure that it is notNULL
right? Move away from the MFC-based resource functions and take a look at the native Win32 ones, likeFindResourceEx(...)
andLoadResource
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!) -
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 byAfxGetApp
to ensure that it is notNULL
right? Move away from the MFC-based resource functions and take a look at the native Win32 ones, likeFindResourceEx(...)
andLoadResource
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!)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
-
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
Have you tried calling
FindResourceEx
with aNULL
instance parameter directly? Also, try usingFindResource
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!) -
Have you tried calling
FindResourceEx
with aNULL
instance parameter directly? Also, try usingFindResource
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!) -
Yes, I tried FindResourceEx() with the NULL instance parameter. The resources are identified by number, their type is "TEXT".
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!) -
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!)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
-
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
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: -
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
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!) -
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!) -
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!) -
Have you tried using it? Even if it was invalid, using
FindResource(...)
with aNULL
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!) -
Have you tried using it? Even if it was invalid, using
FindResource(...)
with aNULL
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!) -
Have you tried using it? Even if it was invalid, using
FindResource(...)
with aNULL
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!)