473,761 Members | 7,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function

Scieck
10 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. LPCTSTR CInternetExplorer::GetHref(IWebBrowser2 *pWb)
  2. {
  3.     CString empty("");
  4.     CComQIPtr<IHTMLDocument3> doc3 = (IHTMLDocument3*)pWb;
  5.     if (doc3 == NULL)
  6.     {
  7.         return empty;
  8.     }
  9.     //CComQIPtr<IHTMLElementCollection> col;
  10.     //CComPtr<IHTMLElementCollection> col;
  11.     IHTMLElementCollection *col;
  12.     //CComBSTR link("LINK");
  13.     USES_CONVERSION;
  14.     BSTR link = A2BSTR("LINK");
  15.  
  16.     HRESULT hr = doc3->getElementsByTagName(link, &col);   // <-- At this line i get the Run-Time Check Failure #0...
  17.  
  18.     if (FAILED(hr)) return empty;
  19.     /*
  20.        some more code here left out for clarity
  21.      */
  22.     return empty;
  23. }
  24.  
Any ideas ?
Many Thanks

Andrea
Oct 3 '07 #1
11 13167
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Oct 3 '07 #2
Scieck
10 New Member
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
Oct 3 '07 #3
Cucumber
90 New Member
Expand|Select|Wrap|Line Numbers
  1. LPCTSTR CInternetExplorer::GetHref(IWebBrowser2 *pWb)
  2. {
  3.     CString empty("");
  4.     CComQIPtr<IHTMLDocument3> doc3 = (IHTMLDocument3*)pWb;
  5.  
Why arent you doing a QueryInterface on the IWebBrowser2 interface pointer to get an IHTMLDocument interface ptr, instead of that cast?
Oct 3 '07 #4
Scieck
10 New Member
Hi Cucumber,
good point, you're right:
i changed the casting with this:
Expand|Select|Wrap|Line Numbers
  1. LPCTSTR CInternetExplorer::GetHref(IWebBrowser2 *pWb)
  2. {
    IDispatch *pDisp = NULL;
    HRESULT hr = pWb->get_Document(&pDisp);
    CComQIPtr<IHTMLDocument3> doc3(pDisp);
    // *****
The run-time error is gone.
Many thanks !

Andrea
Oct 4 '07 #5
Scieck
10 New Member
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
Oct 4 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
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??
Oct 4 '07 #7
Scieck
10 New Member
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.
Oct 5 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
This is exactly what happens.
So where is the string pointed at by the returned LPCTSTR?? And who cleans that up?
Oct 5 '07 #9
Scieck
10 New Member
Expand|Select|Wrap|Line Numbers
  1. // returns the href attribute of a link tag given the rel attribute
  2. // the first match is returned
  3. // <link rel="stylesheet" type="text/css" href="style.css" />
  4. LPCTSTR CInternetExplorer::GetLinkHref(IWebBrowser2 *pWb, const CString &rel)
  5. {
  6.     CString empty(_T(""));
  7.     if (pWb == NULL) return empty;
  8.  
  9.     IDispatch *pDisp = NULL;
  10.     HRESULT hr = pWb->get_Document(&pDisp);
  11.     if (FAILED(hr)) return empty;
  12.  
  13.     CComQIPtr<IHTMLDocument3> doc3(pDisp);
  14.     if (pDisp) 
  15.     {
  16.         pDisp->Release();
  17.         pDisp = NULL;
  18.     }
  19.     if (doc3 == NULL) return empty;
  20.  
  21.     CComPtr<IHTMLElementCollection> col;
  22.     CComBSTR link("LINK");
  23.     hr = doc3->getElementsByTagName(link, &col);
  24.     if (FAILED(hr) || (col == NULL)) return empty;
  25.  
  26.     long size = 0;
  27.     hr = col->get_length(&size);
  28.     if (FAILED(hr)) return empty;
  29.  
  30.     for (long i = 0; i < size; i++)
  31.     {
  32.         COleVariant index(i);
  33.         hr = col->item(index, index, &pDisp);
  34.         if (SUCCEEDED(hr))
  35.         {
  36.             CComQIPtr<IHTMLLinkElement> l(pDisp);
  37.             if (pDisp)
  38.             {
  39.                 pDisp->Release();
  40.                 pDisp = NULL;
  41.             }
  42.             if (l == NULL) return empty;
  43.  
  44.             CComBSTR r;
  45.             hr = l->get_rel(&r);
  46.             if (SUCCEEDED(hr) && (r == CT2A(rel)))
  47.             {
  48.                 CComBSTR href;
  49.                 hr = l->get_href(&href);
  50.                 if (SUCCEEDED(hr))
  51.                 {
  52.                     return COLE2CT(href.m_str);
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     return empty;    
  58. }
  59.  
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.
Oct 6 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4930
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
8
3773
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...
4
3217
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
6
20087
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
13
5095
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
9
4685
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...
7
2949
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();
8
3017
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
3
11300
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!
7
11746
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");
0
9531
marktang
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...
0
9345
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,...
0
10115
Oralloy
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...
1
9905
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,...
0
9775
tracyyun
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...
0
8780
agi2029
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...
0
6609
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();...
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.