I am using this code to sink events in a IWebBrowser2 webbrowser on c++:
STDMETHODIMP AdviseSink::Invoke(DISPID dispIdMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS* pDispParams,
VARIANT* pVarResult,
EXCEPINFO* pExcepInfo,
UINT* puArgErr)
{
if (!pDispParams)
return DISP_E_PARAMNOTOPTIONAL;
switch (dispIdMember)
{
case DISPID_DOCUMENTCOMPLETE:
{
DocumentComplete(pVarResult);
return S_OK;
}
case DISPID_NAVIGATECOMPLETE2:
return S_OK;
default:
return DISP_E_MEMBERNOTFOUND;
}
return S_OK;
}
void DocumentComplete(VARIANT *url)
{
std::string strValue = (char*)_bstr_t(url);
}
When calling (void)DocumentComplete I get this error:
*Unhandled exception at 0x7c812afb in webhost.exe: Microsoft C++ exception: _com_error at memory location 0x0012ed50.*
If comment the line on DocumentComplete, it doesn't show any errors. Also try..catch blocks doens't catch the exception.
What I am trying to do is to use Variant * url to compare it with a std::string.
How can I do this?
You are using the return value (an [out] parameter) as one of the event parameters. This will cause bstr_t to throw a com_error exception because the VARIANT doesn't contain a BSTR.
See the MSDN documentation for the correct DocumentComplete signature.
The event parameters are available from pDispParams not pVarResult. Assuming that it's not called with named arguments (and this event shouldn't be), the url will be available at pDispParams->rgvarg[0] and the window/frame at pDispParams->rgvarg[1]. Parameters are in the opposite order in the rgvarg array as they're declared in the idl.
If you can, I recommend instead using ATL's IDispEventSimpleImpl to implement COM event interfaces in C++ instead of implementing IDispatch yourself.
http://msdn.microsoft.com/en-us/library/9k3ebasf(v=VS.100).aspx
the _bstr_t constructor takes only reference of VARIANT, instead of pointer to it.
YeenFei gave half of the answer. The other half is that after you get your bstr, it will point to a Unicode string not to an ANSI string. If you want to get an ANSI string, you have to do it by converting the string from Unicode to ANSI, not by converting the pointer.
Related
So I have tried to debug the program and as soon as I get into the Windows API function calls things get a little crazy, plus there isn't much help with debugging those files because I can't change them anyways. Basically what I am stuck on is the following two functions that I can change (FYI this is really old code and the program works in 32bit versions but when converted to 64bit this problem occurred):
void CSalvoPage::AdviseScrollingButtonPanel()
{
if ( m_SBPCookie == 0 )
{
IUnknown * pSinkUnk;
long * pCookie = &m_SBPCookie;
m_spSBPControlSink->QueryInterface(IID_IUnknown, (void **) &pSinkUnk);
if (pSinkUnk != NULL)
{
m_SalvoButtons.AddListener(pSinkUnk, pCookie);//here is the problem~~~~
pSinkUnk->Release();
}
}
}
Then we have the AddListener call which does this
void CNvButtonPanel::AddListener(LPUNKNOWN pUnk, long* pCookie)
{
static BYTE parms[] =
VTS_UNKNOWN VTS_PI4;
InvokeHelper(0x16, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
pUnk, pCookie);
}
I know for a fact that the InvokeHelper function throws the exception through debugging. All I seem to understand is that parms[] lets the InvokeHelper know what types of parameters it's getting and how many. I looked up the definitions and found that in fact
VTS_UNKNOWN = "\x0D" //IUNKNOWN*
and
VTS_PI4 = "\x43" //a 'long*'
Therefore I am telling the InvokeHelper the correct types of parameters to expect so I don't understand why I get a Type Mismatch Error in a popup window everytime I run the program... Any ideas as to why my InvokeHelper throws the Type Mismatch Error?
I have tried to look into the InvokeHelper method documentation and it's really confusing... What I do know is that it throws the COleException mentioned in the documentation and the SCODE returned from the Invoke method is -2147352571
[id(22), helpstring("method AddListener")]
HRESULT AddListener(
[in] IUnknown * pUnk,
[out] IUnknown ** pCookie
);
I was able to fix the issue by doing what RbMm suggested which was to change the function AddListener and RemoveListener functions to match the types declared in the .idl file.
void AddListener(LPUNKNOWN pUnk, LPUNKNOWN* pCookie);
void RemoveListener(LPUNKNOWN pCookie);
The functions now correctly match the types defined in the .idl file
[id(22), helpstring("method AddListener")]
HRESULT AddListener(
[in] IUnknown * pUnk,
[out] IUnknown ** pCookie
);
[id(23), helpstring("method RemoveListener")]
HRESULT RemoveListener(
[in] IUnknown * pCookie
);
I make an OCX in C++ Builder Borland. My OCX has these function as I describe it:
Login
Write
Read
DebugOutput
GetVal
I am using this OCX in my c# application. In c# side I have a button and list box. The button calls login method from OCX and the list box shows this method's output.
In OCX side, the login method creates a command for server (with socket programming) to get authentication. Then call Write function to writes on socket. Then gets response from socket and calls Read function to reads the socket response.
The Read method reading the result and send it to DebugOutput to debug the output stream and call GetVal to find Last main server response. Then they pass their parameters to each other. after all the C# side show the result (SUCCESS | FAIL ) for login methods.
I used BSTR. (I read the topics about BSTR in stackoverflow and on MSDN but I think I did not get it well in my solution). These are my code in OCX side:
BSTR STDMETHODCALLTYPE TVImpl::Login(BSTR PassWord)
{
wchar_t wcs1[500];
Var *var=new Var();
//here make the command
...................
//get the server response to show to user
BSTR read=::SysAllocString(Write(wcs1));
if(read!=NULL) ::SysFreeString(read);
return read;
}
BSTR STDMETHODCALLTYPE TVImpl::Read()
{
BSTR str ;
try
{
IdTCPClient1->ReadTimeout=100;
str =::SysAllocString( IdTCPClient1->IOHandler->ReadLn().c_str());
}
catch(Exception &e)
{
str= e.Message.c_str();
}
str=(DebugOutput(str));
return str;
}
BSTR STDMETHODCALLTYPE TVImpl::Write(BSTR str)
{
IdTCPClient1->IOHandler->WriteLn(str) ;
BSTR str2=::SysAllocString(L"TST");
str2=Read();
return str2;
}
BSTR STDMETHODCALLTYPE TVImpl::GetVal(BSTR st,BSTR ValTyp)
{
BSTR res;
AnsiString stAnsi;
//Do some thing with st and save it to stAnsi
.................
res=(BSTR)WideString(stAnsi);
return ::SysAllocString(res);
}
BSTR STDMETHODCALLTYPE TVImpl::DebugOutput(BSTR st)
{
Var *val=new Var();
BSTR res;
res=GetVal(st,val->CMD_CMD);
if(res==val->CMD_AUTHENTICATE)
res=GetVal(st,val->XPassword);
return res;
}
In C3 code it was hanging. I know my problem is to use sysAllocString. but I when I used ::sysFreestring for each one in each method again my C# code hanging.
This is my C# code :
private void button4_Click(object sender, EventArgs e)
{
VinSockCmplt.Vin vin = new Vin();
listBox1.Items.Add(vin.Login("1234"));
}
You should not return BSTR (or any "complex" type that various compilers compile in various ways). The recommended return type for COM methods is HRESULT (a 32-bit integer). Also, you must not release a BSTR you just allocated and return it to the caller.
Here is how you could layout your methods:
HRESULT STDMETHODCALLTYPE TVImpl::Login(BSTR PassWord, /*out*/ BSTR *pRead)
{
...
*pRead = ::SysAllocString(L"blabla"); // allocate a BSTR
...
// don't SysFreeString here, the caller should do it
...
return S_OK;
}
If you were defining your interface in a .idl file, it would be something like:
interface IMyInterface : IUnknown
{
...
HRESULT Login([in] BSTR PassWord, [out, retval] BSTR *pRead);
...
}
retval is used to indicate assignment semantics are possible for languages that support it, like what you were trying to achieve initially with code like this var read = obj.Login("mypass")
There are lots of mistakes in your code:
if(read!=NULL) ::SysFreeString(read); return read; frees a string and then returns it
str= e.Message.c_str(); ... return str; returns a pointer to something that isn't even a BSTR
BSTR str2=::SysAllocString(L"TST"); str2=Read(); allocates a string and then leaks that string immediately
res=(BSTR)WideString(stAnsi); creates a dangling pointer
All of these are undefined behaviour (except the leak) and might cause a crash.
Also, I'm not sure if it is valid to have your functions return BSTR; the normal convention in COM programming is that functions return HRESULT and any "return values" go back via [out] parameters. This is compatible with all languages that have COM bindings; whereas actually returning BSTR limits your code compatibility. (I could be wrong - would welcome corrections here). You can see this technique used in the other question you linked.
To get help with a question like "Why is my code crashing?", see How to Ask and How to create a Minimal, Complete, and Verifiable example.
I am trying to write a CLI wrapper around some low-level COM-related calls. One of the operations that I need to do specifically is to get a specific value from a PROPVARIANT, i.e.:
pwszPropName = varPropNames.calpwstr.pElems[dwPropIndex];
where pwszPropName is documented to be an LPWSTR type and dwPropIndex is a DWORD value passed into the function by the user.
I have a native function defined as follows:
HRESULT CMetadataEditor::GetPropertyNameByID(DWORD ID, wchar_t *PropertyName)
I would like to return the value of pwszPropName via *PropertyName.
Is the wchar_t* type the best way to do this, and would I need to pin *PropertyName in my CLI to ensure it does not move in memory? Do I need to define the length of *PropertyName before passing it to native code (buffer)?
If wchar_t* is the right variable type to pass into the native function, what is the proper conversion of LPWSTR to whar_t*, and how then would you convert that value to System::String?
I have tried a number of different techniques over the past few days and can't seem to get anything right.
------------UPDATE------------
Here is my full code. First, the CLI:
String^ MetadataEditor::GetPropertyNameByID(unsigned int ID)
{
LPWSTR mPropertyName = L"String from CLI";
m_pCEditor->GetPropertyNameByID(ID, mPropertyName);
//Convert return back to System::String
String^ CLIString = gcnew String(mPropertyName);
return CLIString;
}
And the native code:
HRESULT CMetadataEditor::GetPropertyNameByID(DWORD ID, LPWSTR PropertyName)
{
HRESULT hr = S_OK;
LPWSTR myPropName;
PROPVARIANT varNames;
PropVariantInit(&varNames);
hr = m_pMetadata->GetAllPropertyNames(&varNames);
if(hr != S_OK)
{
PropVariantClear(&varNames);
return hr;
}
myPropName = varNames.calpwstr.pElems[ID];
PropertyName = myPropName;
PropVariantClear(&varNames);
return hr;
}
It doesn't seem like the value (myPropName) is set properly and/or sustained back into the CLI function because the CLI returns the value I set on mPropertyName before calling the native function.. I'm not sure why or how to fix this.
UPDATE!!!!
I suspected my problem had something to do with variables going out of scope. So I changed the C++ function definition as follows:
LPWSTR GetPropertyNameByID(DWORD ID, HRESULT ErrorCode);
After adjusting the CLI as well, I now get a value returned, but the first character is incorrect, and in fact can be different with every call. I tried using ZeroMemory() in the native class before assigning the output of the PROPVARIANT to the variable (ZeroMemory(&myPropName, sizeof(myPropName +1)); but still no luck.
You can design unmanaged function by the following way:
HRESULT CMetadataEditor::GetPropertyNameByID(DWORD ID, LPWSTR PropertyName, size_t size)
{
....
wcscpy(PropertyName, varNames.calpwstr.pElems[ID]); // or wcsncpy
...
}
PropertyName is the buffer allocated by caller, size is its size. Inside the function wcscpy or wcsncpy the string varNames.calpwstr.pElems[ID] to PropertyName. Client code:
WCHAR mPropertyName[100];
m_pCEditor->GetPropertyNameByID(ID, mPropertyName, sizeof(mPropertyName)/sizeof(mPropertyName[0]));
Think, for example, how GetComputerName API is implemented, and do the same
everyone. I know Microsoft have given an example about the save method of IXMLDOMDocument like this:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd874226(v=vs.85).aspx
But when I changed the parameter of save to a variable like CString or char* instead of a constant, I got an exception in the save method like this:
"Unhandled exception in VisualADS.exe: 0xC0000005: Access Violation."
The exception position is:
#pragma implementation_key(76)
inline HRESULT MSXML2::IXMLDOMDocument::save ( const _variant_t & destination ) {
HRESULT _hr = raw_save(destination);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _hr;
}
This is a MFC MBCS project in Visual Studio 6, here's my code, thx!
CString strFilePathName = ar.GetFile()->GetFilePath();
CComBSTR ccbsFilePathName(strFilePathName);
CComVariant ccvFilePathName(ccbsFilePathName);
hr = pXMLDoc->save(ccvFilePathName);
The variant you pass to IXMLDOMDocument::save has to carry a BSTR. If your project is an UNICODE one, there is great chances that you'll have no conversions to do, and no reasons to crash using weird attempts.
If you are in a MBCS project, things are different: you have to somehow convert the file name from MBCS to UNICODE before using it in OLE/COM. I suggest doing so using the CComBSTR class
CString strFilePathName;
[...]
CComBSTR ccbsFilePathName( strFilePathName );
CComVariant ccvFilePathName( ccbsFilePathName );
hr = pXMLDoc->save( ccvFilePathName );
From book ATL Internals, I knew BSTR is different from OLECHAR*, and there are CComBSTR and CString for BSTR.
According MSDN Allocating and Releasing Memory for a BSTR, I knew memory management responsibility for caller/callee.
Take this line from MSDN,
HRESULT CMyWebBrowser::put_StatusText(BSTR bstr)
I still do not know how to handle bstr properly in my implementation. Since I still have a basic question for BSTR -- should we treat bstr as a value (like int) or as a reference (like int*), at least on COM interface boundary.
I want to convert BSTR as soon as possible to CString/CComBSTR in my implementation. Value or Reference semantic will be totally different case for the conversion. I've digged into CComBSTR.Attach, CComBSTR.AssignBSTR, etc. But the code cannot solve my doubts.
MSDN CComBSTR.Attach has some code snip, I feel it is wrong since it is not obey Allocating and Releasing Memory for a BSTR. ATL Internals said SetSysString will 'free the original BSTR passed in', if I used it, it will violate BSTR argument convention, just like CComBSTR.Attach.
All in all, I want to using CString to handle raw BSTR in implementation, but do not know the correct way...I've written some just work code in my projects, but I always feel nervous since I don't know whether I am correct.
Let me talk coding language
HRESULT CMyWebBrowser::put_StatusText(BSTR bstr)
{
// What I do NOT know
CString str1; // 1. copy bstr (with embeded NUL)
CString str2; // 2. ref bstr
// What I know
CComBSTR cbstr1;
cbstr1.AssignBSTR(bstr); // 3. copy bstr
CComBSTR cbstr2;
cbstr2.Attach(bstr); // 4. ref bstr, do not copy
// What I do NOT know
// Should we copy or ref bstr ???
}
CComBSTR is just a RAII wrapper around raw BSTR. So feel free to use CComBSTR instead of raw BSTR to help writing code that is exception-safe, that makes it harder to leak resources (i.e. the raw BSTR), etc.
If the BSTR is an input parameter, it's just like a const wchar_t* (with length prefixed, and potentially some NULs L'\0' characters inside). If the BSTR doesn't have NULs embedded inside, you can simply pass it to a CString constructor, that will make a deep-copy of it, and you can locally work with your CString. Modifications to that CString won't be visible on the original BSTR. You can use std::wstring as well (and note that std::wstring can handle embedded NULs as well).
void DoSomething(BSTR bstrInput)
{
std::wstring myString(bstrInput);
// ... work with std::wstring (or CString...) inside here
}
Instead, if the BSTR is an output parameter, then it is passed using another level of indirection, i.e. BSTR*. In this case, you can use CComBSTR::Detach() inside your method to release the BSTR safely wrapped into the CComBSTR, and transfer its ownership to the caller:
HRESULT DoSomething( BSTR* pbstrOut )
{
// Check parameter pointer
if (pbstrOut == nullptr)
return E_POINTER;
// Guard code with try-catch, since exceptions can't cross COM module boundaries.
try
{
std::wstring someString;
// ... work with std::wstring (or CString...) inside here
// Build a BSTR from the ordinary string
CComBSTR bstr(someString.c_str());
// Return to caller ("move semantics", i.e. transfer ownership
// from current CComBSTR to the caller)
*pbstrOut = bstr.Detach();
// All right
return S_OK;
}
catch(const std::exception& e)
{
// Log exception message...
return E_FAIL;
}
catch(const CAtlException& e)
{
return e; // implicit cast to HRESULT
}
}
Basically, the idea is to use BSTR (wrapped in a RAII class like CComBSTR) only at the boundary, and do the local work using std::wstring or CString.
As a "bouns reading", consider Eric Lippert's guide to BSTR semantics.
Having BSTR on input, you are not responsible to release it. Converting to CString is easy:
CString sValue(bstr);
or, if you prefer to keep Unicode characters on MBCS build:
CStringW sValue(bstr);
If you need to convert back when you have [out] parameter, you do (simple version):
VOID Foo(/*[out]*/ BSTR* psValue)
{
CString sValue;
*psValue = CComBSTR(sValue).Detach();
}
Full version is:
STDMETHODIMP Foo(/*[out]*/ BSTR* psValue)
{
_ATLTRY
{
ATLENSURE_THROW(psValue, E_POINTER); // Parameter validation
*psValue = NULL; // We're responsible to initialize this no matter what
CString sValue;
// Doing our stuff to get the string value into variable
*psValue = CComBSTR(sValue).Detach();
}
_ATLCATCH(Exception)
{
return Exception;
}
return S_OK;
}