Discussion:
How to extract char from VT_BSTR?
(too old to reply)
Atiz
2008-01-22 10:47:25 UTC
Permalink
Hi, I have an OPC client.
I used a server to send value to the OPC client whereby it detects the
value.

The OPC client is able to detect the value as a VT_BSTR.
The function which detects the value as a VT_BSTR has VARIANT* pValue
as a parameter.
When this pValue is a VT_BSTR type, I have trouble extracting the
correct value out.

Currently my method does this:

BSTR bstr = V_BSTR(pValue);
int a = SysStringLen(bstr)+1;
char *ansistr = new char[a];
result = ::WideCharToMultiBYte(CP_ACP, 0, bstr, -1, ansistr, a, NULL,
NULL);

I tried to print out

LOG4CPLUS_DEBUG(logPacket, *V_BSTR(pValue));
LOG4CPLUS_DEBUG(logPacket, bstr);
but the value is always different and is not the value that is sent
over to the OPC client.

How can I get the correct value out from pValue?

TIA,
Atiz
Brian Muth
2008-01-22 16:46:35 UTC
Permalink
Post by Atiz
Hi, I have an OPC client.
I used a server to send value to the OPC client whereby it detects the
value.
The OPC client is able to detect the value as a VT_BSTR.
The function which detects the value as a VT_BSTR has VARIANT* pValue
as a parameter.
When this pValue is a VT_BSTR type, I have trouble extracting the
correct value out.
BSTR bstr = V_BSTR(pValue);
int a = SysStringLen(bstr)+1;
char *ansistr = new char[a];
result = ::WideCharToMultiBYte(CP_ACP, 0, bstr, -1, ansistr, a, NULL,
NULL);
This coding is correct, assuming pValue is a VARIANT* containing a BSTR as you describe. I'm wondering if there is an issue with the
way you are trying to print it out with LOG4CPLUS_DEBUG, of which I know nothing about.

HTH

Brian
Atiz
2008-01-23 06:48:57 UTC
Permalink
Post by Brian Muth
Post by Atiz
Hi, I have an OPC client.
I used a server to send value to the OPC client whereby it detects the
value.
The OPC client is able to detect the value as a VT_BSTR.
The function which detects the value as a VT_BSTR has VARIANT* pValue
as a parameter.
When this pValue is a VT_BSTR type, I have trouble extracting the
correct value out.
BSTR bstr = V_BSTR(pValue);
int a = SysStringLen(bstr)+1;
char *ansistr = new char[a];
result = ::WideCharToMultiBYte(CP_ACP, 0, bstr, -1, ansistr, a, NULL,
NULL);
This coding is correct, assuming pValue is a VARIANT* containing a BSTR as you describe. I'm wondering if there is an issue with the
way you are trying to print it out with LOG4CPLUS_DEBUG, of which I know nothing about.
HTH
Brian
Hi Brian,

I printed out the values so I can see that I am passing the correct
value to another server. I tried using Factorysoft OPC client and it
was able to get the correct value from the server.
Let's say I send a string "1" over to the client. I can't get the
value "1" from bstr. On debugging, I can only see that pValue is a
BSTR with some address pointing to some weird values which is what I
also saw in my debug statements, so I can safely say there is no issue
with LOG4CPLUS_DEBUG.

Atiz

Loading...