Length of VARIANT V_BSTR
-
Hi, I am dealing with OLE automation. Now: I have to convert a string to V_BSTR to use it with OLE. This string is really long. About 500 chars and more! If I convert it to V_BSTR, then it cuts after some hundred bytes (about 230). How can I avoid it? I use SysAllocStringLen to convert it, with this, normally, it should be allocated enough space for it. But it seems it doesnt care! VARIANT v1; V_VT(&v1) = VT_BSTR; V_BSTR(&v1) = SysAllocStringLen(strToWc(selStr), 1500); strToWc() is a method by me, which converts a string to widechar. This one works. I checked, and the string is complete! DKT
-
Hi, I am dealing with OLE automation. Now: I have to convert a string to V_BSTR to use it with OLE. This string is really long. About 500 chars and more! If I convert it to V_BSTR, then it cuts after some hundred bytes (about 230). How can I avoid it? I use SysAllocStringLen to convert it, with this, normally, it should be allocated enough space for it. But it seems it doesnt care! VARIANT v1; V_VT(&v1) = VT_BSTR; V_BSTR(&v1) = SysAllocStringLen(strToWc(selStr), 1500); strToWc() is a method by me, which converts a string to widechar. This one works. I checked, and the string is complete! DKT
If my memory serves me right then the "B" in BSTR stands for Byte - the byte that is used to hold the length of the string. This is the Pascal-type string representation. As a consequence, a BSTR cannot hold more then 255 chars. No way around it. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
-
If my memory serves me right then the "B" in BSTR stands for Byte - the byte that is used to hold the length of the string. This is the Pascal-type string representation. As a consequence, a BSTR cannot hold more then 255 chars. No way around it. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
Hmmm thats strange... If I use a direct string: V_BSTR(&v) = SysAllocString(OLESTR("blabla")); then, it works. Even if the "blabla" string is more than 500 chars long! If I use it the way I mentioned before, then it behaves strange: The first time it cuts some bytes off, the second time and so on, it works! If I put the same command twice, then it never works... It seems as if the memory management is totally shit! DKT
-
Hmmm thats strange... If I use a direct string: V_BSTR(&v) = SysAllocString(OLESTR("blabla")); then, it works. Even if the "blabla" string is more than 500 chars long! If I use it the way I mentioned before, then it behaves strange: The first time it cuts some bytes off, the second time and so on, it works! If I put the same command twice, then it never works... It seems as if the memory management is totally shit! DKT
I'm sorry, my memory certainly didn't serve me well. I think I confused it with the ANSI version of BSTR (I think it's called BSTRT). Just to be sure, you say your function converts it to a widechar, you mean unicode, right? A BSTR is a pointer to a location, where the first four bytes are the length part and the rest is the unicode string terminated by a double-zero. Could this in any way be the cause of your problem (e.g. a premature terminating double-zero?) Otherwise, I don't have any good ideas. Perhaps you could post more code? Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
-
I'm sorry, my memory certainly didn't serve me well. I think I confused it with the ANSI version of BSTR (I think it's called BSTRT). Just to be sure, you say your function converts it to a widechar, you mean unicode, right? A BSTR is a pointer to a location, where the first four bytes are the length part and the rest is the unicode string terminated by a double-zero. Could this in any way be the cause of your problem (e.g. a premature terminating double-zero?) Otherwise, I don't have any good ideas. Perhaps you could post more code? Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
-
I checked the double zero, but it didnt make it better... Here some code: Function to convert String to widechar: OLECHAR* ViaExcelConnector::strToWc(const string &cnvrtData) const { OLECHAR cnvrt[500]; int i = 0; char cnvrtChr[500]; for(i=0; i
Your code got f***ed up because you didn't check the "do not treat <'s as HTML tags" box. Anyway, you only allocate 500 chars to do the conversion, so it's no wonder that it wont' work for more than 500 chars. And you have a buffer overrun when cnvrtData is more than 500 chars since you don't check for max 500 chars in your for loop. There's no saying what will happen, but you will definitely get your memory screwed up. Furhtermore, you return a pointer to cnvrt which is a stack variable that will go out of scope (and be overwritten) when the function returns. Why do you move the content of cnvrtData into cnvrtChr? Can't you do the LPCSTR cast directly on cnvrtData? Besides, you should call MultiByteToWideChar with cchWideChar set to zero firt to get the length of the widechar, then allocate it and then convert it:
int iLength = MultiByteToWideChar(CP_ACP,0,(LPCSTR)cnvrtData,-1,NULL,0); OLECHAR* cnvrt = new OLECHAR[iLength]; // perhaps iLength+1; MSDN is not clear on wether the terminating NULL is included in the length returned by MultiByteToWideChar MultiByteToWideChar(CP_ACP,0,(LPCSTR)cnvrtData,-1,cnvrt,iLength; // or iLength+1 return cnvrt
but then you will have to remember to delete cnvrt (the return value from strToWc) withdelete[]
or you'll leak memory. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music" -
I checked the double zero, but it didnt make it better... Here some code: Function to convert String to widechar: OLECHAR* ViaExcelConnector::strToWc(const string &cnvrtData) const { OLECHAR cnvrt[500]; int i = 0; char cnvrtChr[500]; for(i=0; i
Oh, I forgot, you should check that the return value from MultiByteToWideChar(....,NULL,0)is non-zero. If it is zero, call GetLastError to get info on why it failed. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
-
Oh, I forgot, you should check that the return value from MultiByteToWideChar(....,NULL,0)is non-zero. If it is zero, call GetLastError to get info on why it failed. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
Oh perfect. It works now! BUT: While testing, I used a string that was 442 chars long, so not longer than those 500! And no, I couldnt easily convert the string to LPCSTR, cause the string is an own written class (not by me), but I found out that there is a c_str method in this class. And now, it works! Amazing. Thanks so much, this was a very clear answer, and I can learn alot from it, on how to code better! DKT
-
Oh perfect. It works now! BUT: While testing, I used a string that was 442 chars long, so not longer than those 500! And no, I couldnt easily convert the string to LPCSTR, cause the string is an own written class (not by me), but I found out that there is a c_str method in this class. And now, it works! Amazing. Thanks so much, this was a very clear answer, and I can learn alot from it, on how to code better! DKT
Glad to help. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"