C++ Optimization breaking OLE Automation program (non-MFC) -
i'm writing program parse word document , export data out excel workbook using ole automation (the non-mfc way guess). works fine in debug, not in release (specifically if optimization enabled). error idispatch::invoke call failed, specifically:
0x80020004 disp_e_paramnotfound parameter not found
i checked stackoverflow suggestions , main 1 seems uninitialized variables. might what's going on, still don't understand specific case. i've narrowed down single function in program automation::dispatch::invoke
responsible calling idispatch::invoke
. arguments being passed automation::dispatch::invoke
correct problem somewhere in code.
looking @ base code (from msdn) adapted from, able working , narrow down exact problem line. below shows code not work, comments indicate line moved working (look 2 lines <--- problem line
comment). in debug mode, location of line not matter , works in either spot.
my question fix, , why issue start with? thank , let me know if can make question more clear.
hresult automation::dispatch::invoke(int cmd, std::string name, std::vector<variant> values) { uses_conversion; hresult result; /* dispid name passed */ dispid dispid; lpolestr nameole=a2ole(name.c_str()); result=pobjectint->getidsofnames(iid_null, &nameole, 1, locale_user_default, &dispid); if (failed(result)) { return result; } /* reverse elements in values vector invoked in correct order */ std::reverse(values.begin(), values.end()); /* allocate memory object values */ variant *pvalues=new variant[values.size() + 1]; (unsigned int i=0; < values.size(); ++i) { pvalues[i]=values[i]; } /* build dispparams */ dispparams dispparams= {null, null, 0, 0}; /* dispid dispidnamed=dispid_propertyput; <--- problem line moved here makes work */ dispparams.cargs=values.size(); dispparams.rgvarg=pvalues; /* handle special-case property-puts */ if (cmd==dispatch_propertyput) { dispid dispidnamed=dispid_propertyput; /* <--- problem line here */ dispparams.cnamedargs=1; dispparams.rgdispidnamedargs=&dispidnamed; } /* make call */ if (cmd==dispatch_method || cmd==dispatch_propertyput) { result=pobjectint->invoke(dispid, iid_null, locale_system_default, cmd, &dispparams, null, null, null); } else { variantinit(&objectdata); result=pobjectint->invoke(dispid, iid_null, locale_system_default, cmd, &dispparams, &objectdata, null, null); } delete[] pvalues; return result; }
in code:
if (cmd==dispatch_propertyput) { dispid dispidnamed=dispid_propertyput; /* <--- problem line here */ dispparams.cnamedargs=1; dispparams.rgdispidnamedargs=&dispidnamed; }
dispidnamed
local variable code block in (i.e. area delimited {
}
).
after }
reached ceases exist. rgdispidnamedargs
dangling pointer because no longer points variable exists.
you got unlucky in debug mode didn't trigger error sooner.
Comments
Post a Comment