Click here to Skip to main content
16,008,183 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: listboxes Pin
Ravi Bhavnani12-Mar-02 11:59
professionalRavi Bhavnani12-Mar-02 11:59 
GeneralRe: How can I get the item text from other ListView Pin
Tim Smith12-Mar-02 10:33
Tim Smith12-Mar-02 10:33 
Generalequal_range Pin
12-Mar-02 9:50
suss12-Mar-02 9:50 
GeneralRe: equal_range Pin
Joaquín M López Muñoz12-Mar-02 9:52
Joaquín M López Muñoz12-Mar-02 9:52 
QuestionFindResourceEx not working ??????? Pin
Wolfram Steinke12-Mar-02 9:33
Wolfram Steinke12-Mar-02 9:33 
AnswerRe: FindResourceEx not working ??????? Pin
Joaquín M López Muñoz12-Mar-02 9:50
Joaquín M López Muñoz12-Mar-02 9:50 
GeneralRe: FindResourceEx not working ??????? Pin
Wolfram Steinke12-Mar-02 10:15
Wolfram Steinke12-Mar-02 10:15 
QuestionI found 2 issues in ATL 7.0 Service projects: Should I put this somewhere here? Pin
Le centriste12-Mar-02 8:19
Le centriste12-Mar-02 8:19 
I found 2 issues in ATL 7.0 (code in atlbase.h) regarding services. I may or may not be right, some of you will tell me if I'm not. Here they are:

1- A service with no COM interface refuses to start

When creating a service, and you don't define any COM object in it (let's say you want to write a service in ATL, but don't have any use of a COM server in it), it won't start with the SCM reporting that it refuses to start without returning an error code.

This is because of the call to RegisterClassObjects in CAtlExeModule<T>::PreMessageLoop(). When there is no object to register, this method returns S_FALSE. In the CAtlServiceModule<T>::Run() method, there is a line that says:

if (hr == S_OK) {
    //...
}


The problem with that is that hr = S_FALSE. Replace with the following:

if (SUCCEEDED(hr)) {
    //...
}


Some may argue that there is no point in having a service not exposing a COM object. To those I say "hey, COM is not the only thing that a server could implement". Heh. And I like to write services using ATL because it is easy.

2- Multithreaded service

If you write a service containing objects with the "free" threading model, you may want to change the _ATL_APARTMENT_THREADED define to _ATL_FREE_THREADED in stdafx.h. I can tell you upfront that your service will start but you won't be able to activate any of the COM objects in it.

The problem is with the m_bDelayShutdown member variable in CAtlExeModule<T>, from which CAtlServiceModule<T> derives. In CAtlServiceModule<T>::ServiceMain, this member is set to false, probably because it is irrelevant (and maybe harmful, who knows) in a service.

This generates a problem in CAtlExeModule<T>::PreMessageLoop. Look at this snippet from the function:

hr = pT->RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
			REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);

if (FAILED(hr))
    return hr;

if (hr == S_OK)
{
    if (m_bDelayShutdown)
	{
		CHandle h(pT->StartMonitor());
		if (h.m_h == NULL)
		{
			hr = E_FAIL;
		}
		else
		{
			hr = CoResumeClassObjects();
			ATLASSERT(SUCCEEDED(hr));
			if (FAILED(hr))
			{
				::SetEvent(m_hEventShutdown); 						::WaitForSingleObject(h, m_dwTimeOut * 2);
			}
		}
	}

	if (FAILED(hr))
		pT->RevokeClassObjects();
        }
	else
	{
		m_bDelayShutdown = false;
	}


NOTE: This code is only available with _ATL_FREE_THREADED defined.

As your keen eye may have noticed (see the bolded lines of code if not), the RegisterClassObjects method is called with the REGCLS_SUSPENDED flag. This requires a call to CoResumeClassObjects in order for a client to be able to activate an object from your exec.

If you read the code carefully, this method is never called if m_bDelayShutdown is set to false. What I did is simply override the PreMessageLoop method (tell me if you don't know how to do it), and modify it as follow (see bolded line of code):

hr = pT->RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
			REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);

if (FAILED(hr))
    return hr;

if (hr == S_OK)
{
    if (m_bDelayShutdown)
	{
		CHandle h(pT->StartMonitor());
		if (h.m_h == NULL)
		{
			hr = E_FAIL;
		}
		else
		{
			hr = CoResumeClassObjects();
			ATLASSERT(SUCCEEDED(hr));
			if (FAILED(hr))
			{
				::SetEvent(m_hEventShutdown); 						::WaitForSingleObject(h, m_dwTimeOut * 2);
			}
		}
	} else {
               // Error handling not shown for clarity
               hr = CoResumeClassObjects();
        }

	if (FAILED(hr))
		pT->RevokeClassObjects();
        }
	else
	{
		m_bDelayShutdown = false;
	}


This will make your objects available to the outside world.

------------------------------------------------------------------------------

I don't know if I'm right or not, but it resolved some issues for me. If some of you find that what I'm saying is completely dumb, please demonstrate.

Tx for listening

Michel

If I am wrong or said something stupid, I apologize in advance Wink | ;)
AnswerRe: I found 2 issues in ATL 7.0 Service projects: Should I put this somewhere here? Pin
Tomasz Sowinski12-Mar-02 8:57
Tomasz Sowinski12-Mar-02 8:57 
GeneralCFileDialog UNICODE concern Pin
12-Mar-02 8:12
suss12-Mar-02 8:12 
GeneralRe: CFileDialog UNICODE concern Pin
Joaquín M López Muñoz12-Mar-02 9:16
Joaquín M López Muñoz12-Mar-02 9:16 
GeneralRe: CFileDialog UNICODE concern Pin
12-Mar-02 10:55
suss12-Mar-02 10:55 
GeneralRe: CFileDialog UNICODE concern Pin
Joaquín M López Muñoz12-Mar-02 20:00
Joaquín M López Muñoz12-Mar-02 20:00 
GeneralRe: CFileDialog UNICODE concern Pin
13-Mar-02 5:00
suss13-Mar-02 5:00 
GeneralPrinting - size of font Pin
Wizard_0112-Mar-02 4:42
Wizard_0112-Mar-02 4:42 
GeneralRe: Printing - size of font Pin
Tomasz Sowinski12-Mar-02 4:56
Tomasz Sowinski12-Mar-02 4:56 
GeneralRe: Printing - size of font Pin
Tomasz Sowinski12-Mar-02 5:00
Tomasz Sowinski12-Mar-02 5:00 
GeneralRe: Printing - size of font Pin
Wizard_0112-Mar-02 5:02
Wizard_0112-Mar-02 5:02 
GeneralRe: Printing - size of font Pin
Tomasz Sowinski12-Mar-02 5:09
Tomasz Sowinski12-Mar-02 5:09 
Generalbitwise command Pin
SilverShalkin12-Mar-02 4:29
SilverShalkin12-Mar-02 4:29 
GeneralRe: bitwise command Pin
Tim Smith12-Mar-02 4:39
Tim Smith12-Mar-02 4:39 
GeneralRe: bitwise command & MFC Pin
SilverShalkin12-Mar-02 4:50
SilverShalkin12-Mar-02 4:50 
GeneralRe: bitwise command & MFC Pin
alex.barylski12-Mar-02 10:13
alex.barylski12-Mar-02 10:13 
GeneralRe: bitwise command & MFC Pin
Nemanja Trifunovic12-Mar-02 10:52
Nemanja Trifunovic12-Mar-02 10:52 
GeneralRe: bitwise command & MFC Pin
alex.barylski12-Mar-02 12:47
alex.barylski12-Mar-02 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.