LPVARIANT by ref parameter setting: First-chance exception

Posted on 2002-07-26
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.


// VarMap::iterator to the external representation, VARIANT
class _CopyVarMapToVariant
     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);}
Question by:ossentoo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
LVL 30

Expert Comment

ID: 7180686
Stry changing your std::pair to the following:
std::pair<const CAdapt<CString>, LPVARIANT>*
LVL 30

Expert Comment

ID: 7180694
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);
LVL 30

Expert Comment

ID: 7180704
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;

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 30

Expert Comment

ID: 7180737
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.
LVL 30

Accepted Solution

Axter earned 50 total points
ID: 7180754
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)
void Funct2(LPVARIANT &pCopy)

void CallingFunction()
     LPVARIANT data1 = NULL;
     LPVARIANT data2 = NULL;

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

Expert Comment

ID: 7180760
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.
LVL 30

Expert Comment

ID: 7181043
Check out the following link:

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

I hate to see you reinvent the wheel.

Author Comment

ID: 7181855
thx a lot Axter for all your answers.  

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

627 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