Solved

Why can't the VC++ compiler choose the right overloaded method?

Posted on 2001-06-22
11
867 Views
Last Modified: 2010-05-18
Hi,

In atlcom.h a class called CComDispatchDriver is declared and implemented. It is quite handy to use, yet I wondered why noone ever mentioned (not even in the documentation) the InvokeX range of functions. But maybe the following story is why: I have this piece of code, that works perfectly, and another, just below that one, that doesn't. I want to know why it compiles faulty.

STDMETHODIMP CFastGrid::_CB_Click(IDispatch *CallBackObj, long Row, long Col)
{
    if(!m_pdispCallback)
        return S_OK;

    CComDispatchDriver cb(m_pdispCallback);
   
    //InvokeN inverts the array: arguments are in reverse order!!!
    CComVariant varArgs[3] = {Col, Row, CallBackObj};
    //Mind you: the article on DevX is wrong: don't use sizeof in the third param!!!
    cb.InvokeN(m_bstrCallBackPrefix + "Click", varArgs, 3);        
    return S_OK;
}

//Test the test callback function manually
STDMETHODIMP CFastGrid::_CB_Test()
{
    if(!m_pdispCallback)
        return S_OK;

    CComDispatchDriver cb(m_pdispCallback);
    CComVariant var(1);
    cb.Invoke1(m_bstrCallBackPrefix + "Click", var);
    return S_OK;
}

These functions work well as long as I use either Invoke0 or InvokeN. For some reason Invoke1 and Invoke2 don't seem to work. The error I get is:

error C2664: 'long __thiscall ATL::CComDispatchDriver::Invoke1(long,struct tagVARIANT *,struct tagVARIANT *)' : cannot convert parameter 1 from 'class _bstr_t' to 'long'

which is a very general error and actually means that the second overloaded method, having its first parameter defined as an LPCOLESTR, is not found by the compiler. I get a similar error using L"somestring" as argument. When I look up the declarations, all the functions are there and the other ones, Invoke0 and InvokeN use exactly the same way of calling (also an LPCOLESTR argument). What is happening here? Is this the compiler's fault, or does the atlcom.h have an error? I couldn't find any in this part.

Although I can overcome this error by just sticking to Invoke0 and InvokeN, I had this error before (and solved it back then). I just want to know what is happening here and what the compiler is "thinking".

If the problem appears to be more difficult than I initially thought, I'll increase the points accordingly.

Thanks in advance,
12Bsure corp.
0
Comment
Question by:12bsure
  • 4
  • 4
  • 3
11 Comments
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
The compiler will always complain if it can't find a match for any overload.  

Show how you declared

  m_bstrCallBackPrefix

Do you have the same problem if you simply provide some coersion to let the compiler know which override you want?:
  cb.Invoke1((LPCOLESTR)(m_bstrCallBackPrefix + "Click"), var);

Do you have the same problem if you don't try to add two strings?  For instance, does...

  cb.Invoke1( m_bstrCallBackPrefix, var);

work?  The result of the += operator is a bstr_t&

-- Dan
0
 

Author Comment

by:12bsure
Comment Utility
//This is the declaration line:
CComBSTR    m_bstrCallBackPrefix;

//This is the initialization line (in the constructor):
m_bstrCallBackPrefix     =  "CB_";

//And fyi, These are the declarations of all the InvokeX functions:
HRESULT Invoke0(DISPID dispid, VARIANT* pvarRet = NULL);
HRESULT Invoke0(LPCOLESTR lpszName, VARIANT* pvarRet = NULL);
HRESULT Invoke1(DISPID dispid, VARIANT* pvarParam1, VARIANT* pvarRet = NULL);
HRESULT Invoke1(LPCOLESTR lpszName, VARIANT* pvarParam1, VARIANT* pvarRet = NULL);
HRESULT Invoke2(DISPID dispid, VARIANT* pvarParam1, VARIANT* pvarParam2, VARIANT* pvarRet = NULL);
HRESULT Invoke2(LPCOLESTR lpszName, VARIANT* pvarParam1, VARIANT* pvarParam2, VARIANT* pvarRet = NULL);
HRESULT InvokeN(DISPID dispid, VARIANT* pvarParams, int nParams, VARIANT* pvarRet = NULL);
HRESULT InvokeN(LPCOLESTR lpszName, VARIANT* pvarParams, int nParams, VARIANT* pvarRet = NULL);

I tried the cast. As a matter of fact, I tried these:

cb.Invoke1(L("Test"), var);
cb.Invoke1((LPCOLESTR) L("Test"), var);
cb.Invoke1((LBCOLESTR) m_bstrCallBackPrefix, var);
cb.Invoke1((LBCOLESTR) m_bstrCallBackPrefix.m_str, var);
cb.Invoke1(m_bstrCallBackPrefix.m_str, var);

and some combinations of it. Most of these work with the Invoke0 and InvokeN methods, but none work with the Invoke1 and Invoke2 methods. The compiler keeps complaining about its inability to convert the given parameter type to a long.
0
 

Author Comment

by:12bsure
Comment Utility
Sorry, typo. Must be: L"Test" instead of L("Test"). Otherwise the compiler will complain.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
Comment Utility
I can't understand it and the bowels of ATL are filled with what one might expect to find in bowels.

This seems to work:

CComVariant varStr("this is varStr");
CComVariant varInt(1);

LPCOLESTR ps1= L"This is ps1";
LPCOLESTR ps2= (LPCOLESTR)varStr.pbstrVal;

cb.Invoke1(ps1, &varInt);
cb.Invoke1(ps2, &varInt);

I verified that these end up at the implementation of

  HRESULT Invoke1(LPCOLESTR lpszName, VARIANT* pvarParam1, VARIANT* pvarRet = NULL);

-==-=-=-=-=-=-=-=-=-=-=-=-
Also, I see in your tests that you are passing var, but the prototype calls for VARIANT* ;i,e, &var

-- Dan
0
 

Author Comment

by:12bsure
Comment Utility
Thanks, you are completely and totally right! It was just a typo in my code! Leaving the ampersand out gives the error I wrote, so I focused on the first argument. Passing the reference to the variant didn't give an error at all. This also explaines why the other functions Invoke0 and InvokeN worked: they don't need a VARIANT* the way Invoke1 and Invoke2 need one.

I was fooled by the erroneous message of the compiler and didn't see the error. The funny thing is, that once I copied the methods into a new class, inherited from CComDispatchDriver and renaming the methods to InvokeByNameX, I did use the right VARIANT*. (after the compiler issued a more understandable error about it.)

I am very glad it wasn't the compiler or the ATL code this time. Instead, it was just me :-)

Thanks for helping me out. You receive a full A for a full solution albeit a different one than I expected.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
Does anyone know if this accepted answer was posted as a comment or as an answer?
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
nietod:
>>Does anyone know if this accepted answer was posted as a comment or as an answer?

I do, without even checking my records.  

I click Answer in only one situation -- on abandoned questions to which I have expended effort.  I consider a question to be abandoned after the Asker has not responded two two verbal pings, spaced about 5 days apart.  In other words, after the Asker's last response was over 10 days old. I'll wait longer if other experts continue proposing meaningful suggestions.

That was not the situation here -- by several criteria.  Thus there is no possibility that I locked this question. 12bsure clicked the [Accept comment as Answer] as I'm sure he'll verify.

-- Dan
0
 

Author Comment

by:12bsure
Comment Utility
>>> Does anyone know if this accepted answer was posted as a comment or as an answer?
> as I'm sure he'll verify.
Herewith: he's right, it was a comment.

Btw, Nietod, why did you ask? Did you expect a notification, but didn't you get any? Or did you have a meaningful suggestion? They're of course still welcome.

Btw2, Dan, that's a good policy you have :-)

Regards
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Thanks,

I'm trying to measure the number of PAQs closed with comments vs answers.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
hi 12bsure,
There has been a discussion thread in which nietod and a few others tried to defend the practice of posting answers to lock questions.  Axter and myself and a few others object to this and would like to see all C++ section experts simply post comments.  Our basic premise is that even if a post answers the question, it may not be a perfect solution for the Asker.  This question is typical.  My 'sidebar' comment -- that actually led you to a solution -- came only after some give-and-take.

Nietod seems to be checking up on me to see if I actually practice what I preach.

-- Dan
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Don't take it personally.  I'm not checking up on you.  I posted this comment to the 20 most recent (at the time) PAQs to try to measure how they were "handled".    You can check the other PAQs around this one to see the same comment..  Axter is claiming that in this topic area by far the most questions are handled through comments, not answers.  So far the evidence does not really support that.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now