Solved

Accessing VARIANT FAR* URL string in OnBeforeNavigate2 function. Eek!

Posted on 1998-08-28
9
606 Views
Last Modified: 2013-11-19
Hi,
  The VARIANT data structure is giving me a little trouble, and all I need to do is something (what I thought was)really simple: access the VARIANT FAR* URL that is passed to this function(specifically convert it to a CString or char*, and vice-versa; I'd really like to be able to modify it.)

void CNavThingy::OnBeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT FAR* URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR* PostData, VARIANT FAR* Headers, BOOL FAR* Cancel)
{
CString m_szURLValue;
//All I want to do is access the VARIANT FAR* URL
//but I've had major problems figuring out how to read
//the URL->bstrVal value. ARgh!

AfxMessageBox("Finally! Thank God!");
}

I've spent way too much time attempting to do this with all the odd VARIANT and BSTR manipulation functions, but I know it's easy. Could anyone help me out?

Thanks in advance.

--
John McManus
0
Comment
Question by:doggus
  • 4
  • 4
9 Comments
 
LVL 2

Accepted Solution

by:
milenvk earned 50 total points
ID: 1321450
Here is the code:

void CNavThingy::OnBeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT FAR* URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR* PostData, VARIANT FAR* Headers, BOOL FAR* Cancel)
{
// use the CString::CString(LPCWSTR) constructor
CString m_szURLValue(V_BSTR(URL));

// Do some changes to m_szURLValue

::SysFreeString(V_BSTR(URL)); // Free the previous string
V_BSTR(URL) = m_szURLValue.AllocSysString();  // Create the  new BSTR

AfxMessageBox("Finally! Thank God! :-)");
}

BSTR is simply an alias to LPWSTR (and also to LPOLECHAR) - the CString constructor that takes LPCWSTR will simply convert the BSTR value to CString's internal data representation. Then you can modify the string and finally you can allocate a new BSTR using CString::AllocSysString() and return it to URL.
0
 
LVL 1

Expert Comment

by:MuraliKrishna
ID: 1321451
The documentation says there's only one OUT paramerter in the BeforeNavigate2 handler- and that is Cancel.

I don't see how any modification to the URL as suggested above will work. In fact, I'm sure it will not.

Here's my solution, which to my knowledge, is the only one:
      CString strUrlIN( V_BSTR(URL) );
      TRACE( "Incoming URL = \"%s\"\n", strUrl );
      CString strMyUrl = "www.whowhere.com";
      *Cancel = TRUE; // OR *Cancel = VARIANT_TRUE )
      TRACE( "Navigating to %s\n", strUrl );

      // If you want to use Navigate, do:
      Navigate( strMyUrl, Flags, TargetFrameName, PostData, Headers );
      // or if you want to use Navigate2, do the following:
      // COleVariant varMyUrl( strMyUrl );
      //Navigate2( varMyUrl, Flags, TargetFrameName, PostData, Headers );


Regards,
Murali Krishna Devarakonda
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1321452
Well, if the parameter is not [out] then even if you change the passed parameter it won't be transported back to the caller. After all that's the idea of [in], [out] and [in, out] parameters. Your question was how to work with BSTRs and how to transfer them to and from CString or char*, and I believe I answered your question.
0
 
LVL 1

Expert Comment

by:MuraliKrishna
ID: 1321453
'milenvk',
   Let me quote from the question posted by 'doggus':
"all I need to do is something really simple:
access the VARIANT FAR* URL that is passed to THIS FUNCTION      
(specifically convert it to a CString or char*,
     AND
vice-versa; I'd really like to be able to modify it."

You did answer the first question.
You didn't answer the second correctly. In fact, your answer was completely misleading in the context of BeforeNavigate2 handler.

It's curious that you can still say, and I quote again:
  "and I believe I answered your question".
All for a measly 50 points!

Although in fairness, the points need to be split, the only way you can do that (based on my experience more than 2 months ago) is for doggus to send mail to Linda, and ask her to do it.

So, to simplify, here's what I have to say: KEEP IT 'milenvk'.

Regards,
Murali Krishna Devarakonda
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:milenvk
ID: 1321454
Murali Krishna Devarakonda:

To me the question still is, I quote:
"The VARIANT data structure is giving me a little trouble, and all I need to do is something (what I thought was)really simple: access the VARIANT FAR* URL that is passed to this function(specifically convert it to a CString or char*, and vice-versa; I'd really like to be able to modify it.) " - what simplified means: "How do I convert a VARIANT BSTR to a CString or char* and how do I convert it back to VARIANT BSTR?" I am not aware of what the function of interest does and I don't have to be. If the guy wants to access a VARIANT BSTR and I know how, I am ready to help. It's not because of the, quote: "measly 50 points". And I will say again: I believe I answered the question, and I don't do so because of the 50 points, but because I know I did. And finally it would be much nicer if you kept your attitude away.
0
 
LVL 1

Expert Comment

by:MuraliKrishna
ID: 1321455
'milenvk',
     This my last response to this thread. All I have to say to you is- READ THE TITLE OF THIS QUESTION AGAIN.

You say "I am not aware of what the function of interest does and I don't have to be".
  REALLY?
The question's title says:
  "Accessing VARIANT FAR* URL string in OnBeforeNavigate2
   function. Eek!"

In my view, the idea of this group is to exchange information and help each other out. If someone asks a question, because of the points system, several people respond. It's just a competitive instinct that guys here exhibit (even though the points- but for the bragging rights- are worthless so far), and it's easy to get carrried away. People here do it all the time.

What's apalling to me is that on one hand there are incidents where the original question has been answered in entirety, but the person who asked the question expands the scope of the question and holds at ransom, the guy who answered the question originally, by refusing to give points until he's bled the poor guy trying to help of all his patience. Often, this also results in a rat race, and sometimes, the points end up getting assigned to the wrong guy.  
On the other hand, you might ask a question within a context, which is explicit in it's title. If the question is not answered within that context, and doesn't really solve the problem addressed, you can't be expected to be satisfied. Because you stil have your original problem, which made you post the question in the first place.

Often the context is not obvious, and there are exchanges back and forth to determine the context, or to clarify.

In this question, the context was explicit in the title. But if you didn't know about the WebBrowser Control, you wouldn't know about the BeforeNavigate2 handler. But that doesn't change the fact about what 'doggus' really wanted to be able to do.

And please don't even talk about attitude. You had answered the question. Then I added mine. Then it was upto 'doggus' to decide.
You were the one who made the provocative claim:
  "Your question was how to work with BSTRs and how to transfer
   them to and from CString or char*, and I believe I answered
   your question"?
It's clear that you didn't read the title before you said that.

I admit that I shouldn't have addressed you or provoked you by making the statement about the points.

Regards,
Murali Krishna Devarakonda
0
 
LVL 1

Expert Comment

by:MuraliKrishna
ID: 1321456
'milenvk',
     This my last response to this thread. All I have to say to you is- READ THE TITLE OF THIS QUESTION AGAIN.

You say "I am not aware of what the function of interest does and I don't have to be".
  REALLY?
The question's title says:
  "Accessing VARIANT FAR* URL string in OnBeforeNavigate2
   function. Eek!"

In my view, the idea of this group is to exchange information
and help each other out. If someone asks a question, because of
the points system, several people respond. It's just a
competitive instinct that guys here exhibit (even though the
points- but for the bragging rights- are worthless so far), and
it's easy to get carrried away. People here do it all the time.

What's apalling to me is that on one hand there are incidents
where the original question has been answered in entirety, but
the person who asked the question expands the scope of the
question and holds at ransom, the guy who answered the question
originally, by refusing to give points until he's bled the poor
guy trying to help of all his patience. Often, this also results
in a rat race, and sometimes, the points end up getting assigned
to the wrong guy.  

On the other hand, you might ask a question within a context,
which is explicit in it's title. If the question is not answered
within that context, and doesn't really solve the problem
addressed, you can't be expected to be satisfied. Because you
stil have your original problem, which made you post the
question in the first place.

Often the context is not obvious, and there are exchanges back
and forth to determine the context, or to clarify.

In this question, the context was explicit in the title. But if
you didn't know about the WebBrowser Control, you wouldn't know
about the BeforeNavigate2 handler. But that doesn't change the
fact about what 'doggus' really wanted to be able to do.

And please don't even talk about attitude. You had answered the
question. Then I added mine. Then it was upto 'doggus' to decide.

You were the one who made the provocative claim:
  "Your question was how to work with BSTRs and how to transfer
   them to and from CString or char*, and I believe I answered
   your question"?
It's clear that you didn't read the title before you said that.

I admit that I shouldn't have addressed you or provoked you by
making the statement about the points.

Regards,
Murali Krishna Devarakonda
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1321457
Well Murali,

I agree with all you said here. Thanks for admitting you provoked me. I would be glad if I had a chance to withdraw my answer, and I think the only way to do that is to ask doggus.

So doggus, please reject my answer.
0
 

Author Comment

by:doggus
ID: 1321458
I appreciate both your input on the matter; I should have made it more apparent that I actually wanted to know two things: how to access it (in general), and how to actually change the URL. I'm going to give the points to milenvk because it really helped me out, but I really appreciate your input as well, Murali. Thanks, and I hope the answers you gave me will help out others too.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

15 Experts available now in Live!

Get 1:1 Help Now