Solved

LPVARIANT by ref parameter setting: First-chance exception

Posted on 2002-07-26
8
323 Views
Last Modified: 2007-12-19
Hi,  (VC++ 6.0 ATL project)

i'm getting a First-chance exception in OLEAUT32.DLL when trying to execute the VariantCopy statement below.  In MSDN, it says VariantCopy should be used on initialised VARIANT objects.  However, this code was in CodeGuru and MSDN online.  The parameter passed in a LPVARIANT which has not been initialised.  I wanted it to point to the
objects held in Map &pIt->second variable (which incidentally is a VARIANT).  I cannot initialise the pointer as it is returned by the calling function (i.e. i don't want to return a locally initialised variable.

Basically, I want a way of setting the pointer to the address of the &pIt->second object.

I have tried this as well:

*pCopy = pIt->second;

but get a First-chance exception thrown as well.

Thanks


// VarMap::iterator to the external representation, VARIANT
class _CopyVarMapToVariant
{
public:
     static HRESULT copy(LPVARIANT pCopy, std::pair<const CAdapt<CString>, VARIANT>* pIt)
     {
          VariantCopy(pCopy, &pIt->second);
          return S_OK;
     }
     static void init(VARIANT* p) {VariantInit(p);}
     static void destroy(VARIANT* p) {VariantClear(p);}
};
0
Comment
Question by:ossentoo
  • 7
8 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Stry changing your std::pair to the following:
std::pair<const CAdapt<CString>, LPVARIANT>*
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Actually, I think your problem is that you have the arguments switched around.

HRESULT VariantCopy(
  VARIANTARG FAR*  pvargDest,  
  VARIANTARG FAR*  pvargSrc  
);
 

So try the following:
VariantCopy(&pIt->second, pCopy);
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Disregard last comment.

If you're trying to get pCopy to point to pIt->second, then you should use the following syntax:

pCopy = &pIt->second;

Continue.....
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
pCopy = &pIt->second;

Although the above method will get pCopy to point to the value of pIt->second, this change will not get reflected on the calling function.

In order to change the variable of the calling function you need to modify your function argument to the following:

static HRESULT copy(LPVARIANT &pCopy, std::pair<const CAdapt<CString>, VARIANT>* pIt)

Notice the "&" next to pCopy.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
Comment Utility
The following is an example showing why you need the "&".
Both functions try to change the pointer being pass to it.
If the functions are successful, the pointer will be non-zero.
If you run this code, you'll notice only the second function is able to change the pointer.

void Funct1(LPVARIANT pCopy)
{
     ++pCopy;
}
void Funct2(LPVARIANT &pCopy)
{
     ++pCopy;
}

void CallingFunction()
{
     LPVARIANT data1 = NULL;
     LPVARIANT data2 = NULL;
     Funct1(data1);
     Funct2(data2);

     CString Msg;
     Msg.Format("Funct1 = %i, Funct2 = %i", data1, data2);
     AfxMessageBox(Msg);
}
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
One last point.
You should avoid using names that beging with an underscore.
class _CopyVarMapToVariant //Not a portable name

Using names that begin with an underscore can make your code non-portable.  Not only that, you could have your code working in one version of the compiler, and not work on a new version of the compiler.

According to the C++ standard, names that begin with underscore are reserved to the implementation for any use.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Check out the following link:

http://www.codeproject.com/shell/surfhelper.asp

The program is free, and the source code is available on the same link.

I hate to see you reinvent the wheel.
0
 

Author Comment

by:ossentoo
Comment Utility
thx a lot Axter for all your answers.  
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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

11 Experts available now in Live!

Get 1:1 Help Now