Home » Community » Newbie corner » [Resolved] char* with "\0" between each char (Char* returned from OLE VARIANT is weird)
[Resolved] char* with "\0" between each char [message #50876] |
Fri, 11 January 2019 13:56 |
|
Xemuth
Messages: 387 Registered: August 2018 Location: France
|
Senior Member |
|
|
Hello there !
First of all, I wish you all a happy new year !
Today I'm trying to take control of Excel via OLE I did this code :
char* MyExcel::TestingNameOfSheet(){
VARIANT AppResult={0};
VARIANT AtmSheet={0};
VariantInit(&AppResult);
VariantInit(&AtmSheet);
VariantClear(&lgVariant);
lgVariant.vt = VT_I4;
lgVariant.lVal = 2;
AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &AppResult,xlApp.pdispVal, L"Worksheets", 1, lgVariant);
AutoWrap(DISPATCH_PROPERTYGET, &AtmSheet, AppResult.pdispVal, L"Name", 0);
return AtmSheet.pcVal;
}
Somewhere else in my code I did this :
...
test = excel.TestingNameOfSheet();
Cout() << "------------Begin-------------" << "\n";
if(test !=0){
do{
Cout() << (int)*test << "\n";
}while(++test);
}
Cout() << "\n------------Leave-------------" << "\n";
...
this code put into test the name of sheet2 from excel. in my case ("Feuil2").
problem is, when I read it with my "do while" I only get "F" because test is equal to "F\0e\0u\0i\0l\02\0\0\0\0\0 ...".
Can someone explain me why test looks like that ? is it normal ? Or did I make something like classic OLE error ?
Thanks in advance
Best Regard
[Updated on: Mon, 21 January 2019 14:51] Report message to a moderator
|
|
|
|
|
Re: char* with "\0" between each char [message #50985 is a reply to message #50876] |
Wed, 16 January 2019 09:17 |
|
Xemuth
Messages: 387 Registered: August 2018 Location: France
|
Senior Member |
|
|
Hello Again,
I have resolved my problem, the char * I tried to read was a bstr.
To make it readable as normal char * it need to be cast to ASCII char * via this kind of func :
void BSTRtoASC (BSTR str,char * resultat){
if ( str != NULL ) {
unsigned long length = WideCharToMultiByte (CP_ACP,0, str,SysStringLen(str), NULL, 0,NULL, NULL );
length = WideCharToMultiByte (CP_ACP,0, str,SysStringLen(str), resultat, length,NULL, NULL );
resultat[length] = '\0';
}
}
Have a good day
[Updated on: Wed, 16 January 2019 11:04] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Fri Apr 19 04:59:46 CEST 2024
Total time taken to generate the page: 0.03205 seconds
|