How to do a proper way to copy URL->bstrVal to a global variable?

I have this function inside my program:

void CBar::NavigateComplete2(IDispatch *pDisp, VARIANT *URL) { try { UpdateBar(UpdateNavigateComplete); } catch (...) { ASSERT(0); } }

I need to have the URL value from URL->bstrVal from this function and to copy it to a global variable so that my other function can easily refer to it.

What is the appropriate way to do this? since my current method destroys the stability of my crash all the time.


I'm not sure I understand the question or the motivation. If all you want to do is, as described in the question, copy the value of URL->bstrVal then the simplest way to to use a wrapper BSTR class for the global.

namespace { // keep global private to .cpp file
CComBSTR globalURL;

void CBar::NavigateComplete2(IDispatch *pDisp, VARIANT *URL)
if (URL != NULL && URL->vt == VT_BSTR)
globalURL = URL->bstrVal;

Otherwise you should use a raw BSTR and copy it with SysAllocString (and don't forget to SysFreeString it when you're done with the global.

If you don't copy the string (and only copy the pointer) the VARIANT's owner will destroy it giving you a global with a dangling pointer.

Use SysAllocString(URL->bstrVal) to create a copy off the BSTRVAL. you will have to free it when you're done with it using SysFreeString().

Though I have to agree with @Space_C0wb0y that using global varsity for this is error prone. Unless UpdateBar does something asynchronously, you should just pass URL->bstrVal as a parameter to it. And even if UpdateBar schedules asynchronous work, it should still encapsulate creating the BSTR copy and associating it with the asynchronous work, to avoid race between the background code and another NavigateComplete event both trying to access the global var. (Or you have to lock the access to it, which pretty much negates the asynchronous idea, unless you know how to optimize your locks really well)

Category:visual c++ Time:2010-11-03 Views:0
Tags: visual c++

Related post

  • memcpy vs for loop - What's the proper way to copy an array from a pointer? 2011-01-18

    I have a function foo(int[] nums) which I understand is essentially equivalent to foo(int* nums). Inside foo I need to copy the contents of the array pointed to by numsinto some int[10] declared within the scope of foo. I understand the following is

  • How to give user trying to copy URL other URL than the A HREF value? 2011-12-01

    I use skeleton framework to sketch tabs inside IFRAME widget. Each tab has code like this: <a class="active" href="#skeletonTab0" target="_top">Name of tab</a> href pointed to #.... is required for the menu to work. When user right clicks

  • Is it proper to get and especially set Perl module's global variables directly? 2010-05-27

    I was wondering what the best practice in Perl is regarding getting - or, more importantly, setting - a global variable of some module by directly accessing $Module::varName in case the module didn't provide getter/setter method for it. The reason it

  • 64-bit issues: trying to copy value from %rsp into temp variable using inline assembly 2010-06-24

    I'm taking an operating systems design class in which they have given us a microkernel written in C that we're building on top of. The kernel seems to have been designed with 32-bit machines in mind and I'm running snow leopard. So a friend in the cl

  • Is $view in an MVC php program a proper use of global variables? 2010-09-06

    I understand that global variables should be avoided generally. As a beginner, I'm trying to understand whether making a $view variable global in a dynamic web program built following MVC principles is one of those cases where globals are a good idea

  • Proper way to add an array to a $_SESSION variable that already contains arrays in PHP? 2010-09-09

    What is the proper way to add an array to a $_SESSION variable that already contains arrays in PHP? For example I have a session variable: $_SESSION['test'] Then I do this: $_SESSION['test'] = array('sample' => '1', 'sample2' => 2); THEN, I com

  • Deep copy of a QScriptValue as Global Object 2011-02-16

    I have a program using QtScript for some automation. I have added a bunch of C++ functions and classes to the global scope of the script engine so that scripts can access them, like so: QScriptValue fun = engine->newFunction( systemFunc ); engine-

  • what does svn copy URL->WC do? 2011-06-29

    Why would one do svn copy URL -> WC? Does it get the file in readonly mode? or it is same as checkout? I had checked out a branch. I deleted one directory under branch (name is dummy). Then I tried svn copy URL(of dummy) .(current dir). I got the

  • jQuery GetJSON Security Issues. Copying URL and pasting in the browser 2011-07-08

    This question already has an answer here: jQuery GetJSON Security Issues. Copying URL and pasting in the browser 2 answers Im using the jQuery GetJSON call in my asp page. Its something like this.. $.ajax({ url: myUrl/myPage.aspx?callback=BookARoom,

  • Mimicking modern browsers' copy url using jQuery (add http:// when copied) 2011-12-14

    I'm trying to mimic modern browsers' "copy url" functionality. When viewing the url you don't see the http://. If you copy it to the clipboard, http:// will be added. So far I have this: $('#address input').bind('copy', function() { $.fn.changevalue

  • Using ZeroClipboard to Copy URL to Clipboard 2012-02-01

    I'm trying to utilize ZeroClipboard ( to copy the current URL to the user's clipboard. I know I'm missing something here, but I am not getting any kind of error in the console, nor is it workin

  • copy URL links in Internet Explorer 2014-06-21

    Anyone know an easy way to copy URL links in Internet Explorer? In Chrome I just right click on a picture like in Farmville and I get the option to copy LINK URL or IMAGE URL. Can you do that in IE too? Chrome is my favorite but I've been having trou

  • Proper way to copy a readonly NSMutableArray 2010-06-06

    I have an object with a readonly property that I am trying to implement NSCopying for. It has a mutableArray called "subConditions" (which holds "SubCondition" objects). I have made it readonly because I want callers to be able to change the data in

  • Proper way to copy C-strings 2012-03-06

    Is there an easy way to copy C-strings? I have const char *stringA, and I want char *stringB to take the value (note that stringB is not const). I tried stringB=(char*) stringA, but that makes stringB still point to the same memory location, so when

  • php + webdav to do a 'svn copy url url' command 2011-01-31

    I am trying to create an automagic svn 'tag/export' button for moving from our dev-test-stage-live environments. I am using a slightly modified (fixed) version of phpsvnclient to send http requests in the webdav format to our jira svn repository. I'd

  • What's the proper term for the URL sans query param? 2009-10-01

    Say you have the following URL: "param=value" is the query string "/path" is path "http://" is the protocol What's the proper name for "" only? thanks! -nikita --------------Solutions--------

  • Proper way in Python to raise errors while setting variables 2010-03-26

    What is the proper way to do error-checking in a class? Raising exceptions? Setting an instance variable dictionary "errors" that contains all the errors and returning it? Is it bad to print errors from a class? Do I have to return False if I'm raisi

  • copying multiple char* into one char* variables C language 2010-06-11

    How can i copy multiple char* variables into one char* at single instance operation. say i have char* text1 = "Hello"; char* text2 = "World"; i want to copy text1, text2 and '2' and "12345" into char* text3 in single function call. --------------Solu

  • C - Copy string into a linked list variable 2010-10-05

    #define STRLEN 65 /*Create linked list */ struct node { char str[STRLEN]; struct node *next; }; newInput = malloc(sizeof(struct node)); strcpy(newStr, newInput->str); I left the other parts of the code, but it doesnt seem to be copying the string

Copyright (C), All Rights Reserved.

processed in 0.513 (s). 13 q(s)