VC++ DLL - ? assign value to VARIANT type variable
-
Hi EveryOne, I have added a class which contains a method that accepts a VARIANT type variable in an ActivexDll written in VC++. In one of sample code I found the a value is assigned to the VARIANT type variable using a SAFEARRAY. My question is how can a string value be assigned to VARIANT type variable(type casting in strcpy does't work). Can we achieve it only by using SAFEARRAY? if yes why is it so? Is there any other way to obtain the same? kindly explain. Thank you. Regards, LG.
lgatcodeproject
-
Hi EveryOne, I have added a class which contains a method that accepts a VARIANT type variable in an ActivexDll written in VC++. In one of sample code I found the a value is assigned to the VARIANT type variable using a SAFEARRAY. My question is how can a string value be assigned to VARIANT type variable(type casting in strcpy does't work). Can we achieve it only by using SAFEARRAY? if yes why is it so? Is there any other way to obtain the same? kindly explain. Thank you. Regards, LG.
lgatcodeproject
Probably you should use a
BSTR
insted. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Hi EveryOne, I have added a class which contains a method that accepts a VARIANT type variable in an ActivexDll written in VC++. In one of sample code I found the a value is assigned to the VARIANT type variable using a SAFEARRAY. My question is how can a string value be assigned to VARIANT type variable(type casting in strcpy does't work). Can we achieve it only by using SAFEARRAY? if yes why is it so? Is there any other way to obtain the same? kindly explain. Thank you. Regards, LG.
lgatcodeproject
Hi, Even if I use the BSTR type while copying a string constant using strcpy with a type cast the application crashes. How to copy a string constant by-passing this problem? Please find the below snippet.
STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here **strcpy((char *)output, "connected");** return S_OK; }
Regards, LG.lgatcodeproject
-
Hi, Even if I use the BSTR type while copying a string constant using strcpy with a type cast the application crashes. How to copy a string constant by-passing this problem? Please find the below snippet.
STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here **strcpy((char *)output, "connected");** return S_OK; }
Regards, LG.lgatcodeproject
lgatcodeproject wrote:
STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here strcpy((char *)output, "connected"); return S_OK; }
Change to
STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR * pOutput)
{
*pOutput = SysAllocString(L"connected");
return S_OK;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
lgatcodeproject wrote:
STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here strcpy((char *)output, "connected"); return S_OK; }
Change to
STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR * pOutput)
{
*pOutput = SysAllocString(L"connected");
return S_OK;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeHi, Thanks its working now but then I have a doubt, what is that L do inside SysAllocString()function. Regards, LG.
lgatcodeproject
-
Hi, Thanks its working now but then I have a doubt, what is that L do inside SysAllocString()function. Regards, LG.
lgatcodeproject
A
BSTR
is substantially a wide char string (technically it is aOLECHAR
one). theL
prefix creates a wide char literal. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke