Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made the following code to take control of MS Word that is already running. But when I run the BOLD LINE of the following code, I get an error like this.

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 no idea what problem I made.

Please help me. Thanks.

What I have tried:

C++
USES_CONVERSION;

CLSID clsid;
HRESULT hr = CLSIDFromProgID(A2OLE("Word.Application"), &clsid);

LPDISPATCH pDispatch = NULL;
hr = GetActiveObject(clsid, NULL, (IUnknown**)&pDispatch);
if (hr == S_OK) {
    iWordApp.AttachDispatch(pDispatch);
}

iWordDoc = iWordApp.get_Documents();
Posted
Updated 16-Feb-22 9:16am
v4

The message says it all; your code and the code in Word use different calling conventions, which means the stack can get corrupted via a call between the two. You need to read the documentation for the Word Interop to check which it uses. See the explanations at Calling Conventions | Microsoft Docs[^].
 
Share this answer
 
Comments
Member 14877890 16-Feb-22 20:48pm    
Thank you for your comment.
Although i have not tested above scenario, problem seems to be in the out parameter as GetActiveObject does not directly return IDispatch interface rather you need to query it from the IUknown interface.

Please see below an excerpt from Office Automation Using Visual C++[^]

C++
USES_CONVERSION;

CLSID clsid;
HRESULT hr = CLSIDFromProgID(A2OLE("Word.Application"), &clsid);
IUnknown *pUnk;

IDispatch *pDispatch;
hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk;
if (hr == S_OK) {

	hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pDispatch );
	ASSERT(!FAILED(hr));
    
	iWordApp.AttachDispatch(pDispatch);
}

iWordDoc = iWordApp.get_Documents();
pUnk->Release();
 
Share this answer
 
v2
Comments
Member 14877890 16-Feb-22 20:47pm    
Wow! You solved my problem! Thank you very much!
Maciej Los 21-Mar-22 7:54am    
5ed!
I am not all versed in C++ or its functions. The little bit I read upon though makes me believe that your problem might be with your LPDISPATCH bit of code -

I presume that clsid has a value, not shown in your code above. LPDISPATCH however was given a null(nothing) value/attribute and you then make a call to this "nothing"...

See more HERE and HERE on this error - referring to stack calls.

Not sure if this will help, good luck.
 
Share this answer
 
v2
Comments
Member 14877890 16-Feb-22 20:48pm    
Thank you for your comment.
Take the error message serious: some code setting for your project are wrong. So check that the string define is producing correct version for the running app.

Your conversion macro may be wrong in your code. Read that discussion. Best is to create a real BSTR.

Even it isnt the solution: take care that you have ONLY ONE version of MS Word installed. Else deinstall ALL and re-install the target version.
 
Share this answer
 
Comments
Member 14877890 16-Feb-22 20:48pm    
Thank you for your comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900