Hi everyone !
I got hold of a valid IWebBrowser2 object.
I am passing it to a function that should return the value of href in a link tag in the IHTMLDocument3 object, here is the function: -
LPCTSTR CInternetExplorer::GetHref(IWebBrowser2 *pWb)
-
{
-
CString empty("");
-
CComQIPtr<IHTMLDocument3> doc3 = (IHTMLDocument3*)pWb;
-
if (doc3 == NULL)
-
{
-
return empty;
-
}
-
//CComQIPtr<IHTMLElementCollection> col;
-
//CComPtr<IHTMLElementCollection> col;
-
IHTMLElementCollection *col;
-
//CComBSTR link("LINK");
-
USES_CONVERSION;
-
BSTR link = A2BSTR("LINK");
-
-
HRESULT hr = doc3->getElementsByTagName(link, &col); // <-- At this line i get the Run-Time Check Failure #0...
-
-
if (FAILED(hr)) return empty;
-
/*
-
some more code here left out for clarity
-
*/
-
return empty;
-
}
-
Any ideas ?
Many Thanks
Andrea
11 13167
This function returns an LPCTSTR and you are returning a CString.
I'm surprised you are getting this to compile.
Also, you are not using any of the TCHAR mappings in your code. That is, you should use CStringT and not Cstring. Your literals should use the TEXT macro, etc..
I would create the BSTR by using SysAllocString or use CComBSTR rather than a macro.
I assume your typecast in line 4 makes sense. I am susicious of all typecasts on COM interfaces that bypass IUnknown::Query Interface.
Hi weaknessforcats ,
thank you for your reply.
However you shouldn't be surprised the code compiles since the CString class has several overload operators that transform a CString to a LPCTSTR.
Farther more a CString is basically a CStringT in fact it implements it, CStringT is a template class.
I don't think the run-time error i am getting has any thing to do with the way a BSTR is created, and although you are suspicious regarding the use of CComQIPtr and CComPtr they exist to keep the code cleaner, more readable and bug free, references are automatically added and released.
This is the full run-time error i am getting: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
I have tried with different Calling Conventions at compile time like /Gd and /Gz, and also i tried to declared the function with __stdcall no success so far.
I am running out of options, any ideas ?
Andrea
-
LPCTSTR CInternetExplorer::GetHref(IWebBrowser2 *pWb)
-
{
-
CString empty("");
-
CComQIPtr<IHTMLDocument3> doc3 = (IHTMLDocument3*)pWb;
-
Why arent you doing a QueryInterface on the IWebBrowser2 interface pointer to get an IHTMLDocument interface ptr, instead of that cast?
Hi Cucumber,
good point, you're right:
i changed the casting with this: -
LPCTSTR CInternetExplorer::GetHref(IWebBrowser2 *pWb)
-
{
IDispatch *pDisp = NULL; HRESULT hr = pWb->get_Document(&pDisp); CComQIPtr<IHTMLDocument3> doc3(pDisp); // *****
The run-time error is gone.
Many thanks !
Andrea
I assume your typecast in line 4 makes sense. I am susicious of all typecasts on COM interfaces that bypass IUnknown::Query Interface.
Hi weaknessforcats ,
your suspiciousness was totally right there.
Thanks.
Andrea
However you shouldn't be surprised the code compiles since the CString class has several overload operators that transform a CString to a LPCTSTR.
Neat trick. You return a pointer to your CString that is a local variable and gets destroyed when the function completes??
Neat trick. You return a pointer to your CString that is a local variable and gets destroyed when the function completes??
This is exactly what happens.
This is exactly what happens.
So where is the string pointed at by the returned LPCTSTR?? And who cleans that up?
- // returns the href attribute of a link tag given the rel attribute
-
// the first match is returned
-
// <link rel="stylesheet" type="text/css" href="style.css" />
-
LPCTSTR CInternetExplorer::GetLinkHref(IWebBrowser2 *pWb, const CString &rel)
-
{
-
CString empty(_T(""));
-
if (pWb == NULL) return empty;
-
-
IDispatch *pDisp = NULL;
-
HRESULT hr = pWb->get_Document(&pDisp);
-
if (FAILED(hr)) return empty;
-
-
CComQIPtr<IHTMLDocument3> doc3(pDisp);
-
if (pDisp)
-
{
-
pDisp->Release();
-
pDisp = NULL;
-
}
-
if (doc3 == NULL) return empty;
-
-
CComPtr<IHTMLElementCollection> col;
-
CComBSTR link("LINK");
-
hr = doc3->getElementsByTagName(link, &col);
-
if (FAILED(hr) || (col == NULL)) return empty;
-
-
long size = 0;
-
hr = col->get_length(&size);
-
if (FAILED(hr)) return empty;
-
-
for (long i = 0; i < size; i++)
-
{
-
COleVariant index(i);
-
hr = col->item(index, index, &pDisp);
-
if (SUCCEEDED(hr))
-
{
-
CComQIPtr<IHTMLLinkElement> l(pDisp);
-
if (pDisp)
-
{
-
pDisp->Release();
-
pDisp = NULL;
-
}
-
if (l == NULL) return empty;
-
-
CComBSTR r;
-
hr = l->get_rel(&r);
-
if (SUCCEEDED(hr) && (r == CT2A(rel)))
-
{
-
CComBSTR href;
-
hr = l->get_href(&href);
-
if (SUCCEEDED(hr))
-
{
-
return COLE2CT(href.m_str);
-
}
-
}
-
}
-
}
-
return empty;
-
}
-
So where is the string pointed at by the returned LPCTSTR?? And who cleans that up?
Above there is the whole function that is now working, there are no memory leaks, or stack corruptions.
The above function works because the string is empty, therefore the return pointer points to 0 or NULL, i couldn't return a non empty CString with LPCTSTR as the return type becase the return pointer would be invalid if the CString is allocated on the stack.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: leroybt.rm |
last post by:
Can someone tell me how to run a script from a interactive shell
I type the following:
>>>python filename
>>>python filename.py
>>>run filename
>>>run filename.py
>>>/run filename
>>>/run filename.py
|
by: Jonathan Polley |
last post by:
I have one account on a WindowsXP machine that refuses to run IDLE (or
any other python script that uses Tk). Other people can login to that
PC and IDLE runs just fine, so it is not an installation issue. When
the person who has the problem logs into another PC the problem follows
them. Any ideas as to what might me wrong?
This is the traceback from IDLE:
C:\Python20\Tools\idle>..\..\python.exe idle.py
Traceback (most recent call...
|
by: Ed |
last post by:
Hello,
I took a course in asp about 2 years ago and I was
practicing with IIS 5.0. Then I put it down for a while.
Now trying to get back to it. I can't run asp files from
subdirectories of my root directory, but I can run asp
files from the root directory of my website and I can run
htm files from the subdirectories. If I run
localhost/mytest.asp
|
by: orekin |
last post by:
Hi There
I have been trying to come to grips with Application.Run(),
Application.Exit() and the Message Pump and I would really appreciate
some feedback on the following questions .. There are quite a few
words in this post but the questions are actually quite similar and
should be fairly quick to answer ...
(1) What is Happening with the Threads
|
by: Bob Day |
last post by:
Using vs2003, vb.net
I start a thread, giving it a name before start. Code snippet:
'give each thread a unique name (for later identification)
Trunk_Thread.Name = "Trunk_0_Thread"
' allow only 1 thread per line
Trunk_Thread.ApartmentState = ApartmentState.STA
| |
by: Brett Wesoloski |
last post by:
I am new to VS2005.
I changed my program.cs file to be a different form I am working on. But
when I go to run the application it still brings up the form that was
originally declared as new. When I put in a break point the program does
not stop. It is in debug mode. If I change the program.cs file back to the
form that was originally being used. The program does go into debug mode.
But if I change code in that file it isn't using...
|
by: Lee Crabtree |
last post by:
I remember when I was first getting into .NET Forms programming that
there was a rather emphatic rule about not constructing a form before
calling Application.Run with it. So this:
Application.Run(new Form1());
was okay, but this:
Form1 form = new Form1();
|
by: David Thielen |
last post by:
Hi;
In our setup program how do I determine if I need to run "aspnet_regiis –i"
and if so, is there an API I can calll rather than finding that program on
the user's disk and calling it?
--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
|
by: traceable1 |
last post by:
Is there a way I can set up a SQL script to run when the instance
starts up?
SQL Server 2005 SP2
thanks!
|
by: mxdevit |
last post by:
Task: run application from ASP.NET
for example,
you have a button on ASP.NET page, when press this button - one
application is invoked.
the code to run application (for example, notepad) is (on C#)
System.Diagnostics.Process.Start("notepad");
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |