Click here to Skip to main content
15,886,963 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Weird problem using C++ dll from vb6 Pin
lenourien23-Aug-11 5:05
lenourien23-Aug-11 5:05 
AnswerRe: Weird problem using C++ dll from vb6 Pin
MicroVirus23-Aug-11 5:24
MicroVirus23-Aug-11 5:24 
GeneralRe: Weird problem using C++ dll from vb6 Pin
Richard MacCutchan23-Aug-11 5:51
mveRichard MacCutchan23-Aug-11 5:51 
AnswerRe: Weird problem using C++ dll from vb6 Pin
MicroVirus23-Aug-11 5:18
MicroVirus23-Aug-11 5:18 
GeneralRe: Weird problem using C++ dll from vb6 Pin
lenourien23-Aug-11 13:33
lenourien23-Aug-11 13:33 
QuestionRe: Weird problem using C++ dll from vb6 Pin
MicroVirus23-Aug-11 15:22
MicroVirus23-Aug-11 15:22 
AnswerRe: Weird problem using C++ dll from vb6 Pin
lenourien24-Aug-11 4:17
lenourien24-Aug-11 4:17 
AnswerRe: Weird problem using C++ dll from vb6 Pin
MicroVirus24-Aug-11 17:41
MicroVirus24-Aug-11 17:41 
This piece of code hasn't been sitting well with me since the beginning:

lenourien wrote:
C++
LPSAFEARRAY pSA;
pSA= SafeArrayCreate(VT_BSTR,1,aDim);
 
if (pSA != NULL) {
 
BSTR element = SysAllocString((BSTR)words[low]);
 
unsigned int length = SysStringByteLen(element);
BSTR wcElement = NULL;
 
wcElement = SysAllocStringByteLen(NULL, length*2); 
 
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,(LPCSTR)element
, -1, (LPWSTR)wcElement, length*2);
 
for(i = 0; i < wordcnt[low]; i++) {
 
	if (hr= SafeArrayPutElement (pSA, &arrIndex, wcElement)){
		SafeArrayDestroy(pSA); 
			}
	SysFreeString (wcElement); 
	SysFreeString (element);
	arrIndex++;
		
    }


The problem here is that a BSTR is different from a C-string in that a BSTR knows its own length. In Visual Basic, a string can contain null (0) characters. In C, that signifies the end of a string, but in Visual Basic it can be part of the string.

MultiByteToWideChar is a function that takes and outputs normal C strings. The SysAllocString-functions take a C-string/length pair and output a BSTR. A BSTR is in fact a 2-byte unsigned integer representing the size of the string, followed by the actual unicode (16 byte) character array. The catch here is that the address the BSTR references (BSTR is a wchar_t*) is to the actual character array. If you take the pointer (a BSTR) and go back 2 bytes, you can read the size field. So, in the code above: *((unsigned short*)(wcElement - 1)) returns the size of wcElement (the actual size! Not the C-size counted to the 0-char, but the actual allocated size, which is VB wise 'the' size). SysAllocStringByteLen is a weird function, in that it puts an ANSI (1 byte) string flatly into a unicode (2 bytes) string without doing a widening conversion.
Furthermore, SysAllocString expects to be given a unicode string, and not a char*, as words is. So, your code now greatly risks reading outside the buffer. It doesn't because words at some point has two 0-chars after each other, which the Sys function recognises as a unicode 0-char and stops on time. But actually, it's reading through words way too far.

So, knowing this, you can for one conclude that the code is wrong in almost every way, but also actually that you can simplify your code a little and make it right.
What you do is you loop through each words list and use strlen to find the length of the words entry. Then you use MultiByteToWideChar to convert to unicode. Then you use SafeArrayPutElement to store the converted string. And this you repeat for every entry of words.

C++
for (i = lowerBoundary; i <= upperBoundary; i++)
{
  size_t len = strlen(words[i]); // Get length of current word list
  BSTR element = SysAllocStringLen(NULL, len); // Allocate space for unicode version of words[i]
  MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, words[i], len, (LPWSTR)element, len);
  SafeArrayPutElement(pSA, &i, element);
  SysFreeString(element);
}


I think this code should work correctly, but you should check this and see how it fits into the rest yourself.

Finally, in the first piece where you receive StringArray you are taking into account the lower and upper boundary of the array, but then in the rest of the code you assume lower boundary is 0. This is a bug you'll need to fix as well.

At any rate, I'm fairly sure this is where your current problem is stemming from: from SysAllocString copying too many bytes from words[low].
GeneralRe: Weird problem using C++ dll from vb6 Pin
lenourien25-Aug-11 1:31
lenourien25-Aug-11 1:31 
GeneralRe: Weird problem using C++ dll from vb6 Pin
MicroVirus25-Aug-11 3:25
MicroVirus25-Aug-11 3:25 
GeneralRe: Weird problem using C++ dll from vb6 Pin
lenourien25-Aug-11 12:39
lenourien25-Aug-11 12:39 
QuestionWTSSendMessage fails as RPC Server is unavailable Pin
Arun Parthasarathy22-Aug-11 1:24
Arun Parthasarathy22-Aug-11 1:24 
AnswerRe: WTSSendMessage fails as RPC Server is unavailable Pin
Tony Richards25-Aug-11 13:05
Tony Richards25-Aug-11 13:05 
QuestionConverting ADO data types into C++ data types Pin
Bahram198420-Aug-11 0:47
Bahram198420-Aug-11 0:47 
AnswerRe: Converting ADO data types into C++ data types Pin
John Schroedl22-Aug-11 3:28
professionalJohn Schroedl22-Aug-11 3:28 
QuestionInheriting a template class Pin
Subin Mavunkal17-Aug-11 1:43
Subin Mavunkal17-Aug-11 1:43 
AnswerRe: Inheriting a template class Pin
George L. Jackson9-Sep-11 12:13
George L. Jackson9-Sep-11 12:13 
Questionc++ function name parameter problem Pin
ali_zdn14-Aug-11 3:11
ali_zdn14-Aug-11 3:11 
AnswerRe: c++ function name parameter problem Pin
Richard MacCutchan14-Aug-11 7:03
mveRichard MacCutchan14-Aug-11 7:03 
QuestionProject Converted From VS2003 to VS2008, get exception when call dialog.doModal Pin
Andraw Tang5-Aug-11 11:16
Andraw Tang5-Aug-11 11:16 
AnswerRe: Project Converted From VS2003 to VS2008, get exception when call dialog.doModal Pin
Richard MacCutchan5-Aug-11 22:32
mveRichard MacCutchan5-Aug-11 22:32 
GeneralRe: Project Converted From VS2003 to VS2008, get exception when call dialog.doModal Pin
Andraw Tang8-Aug-11 4:37
Andraw Tang8-Aug-11 4:37 
GeneralRe: Project Converted From VS2003 to VS2008, get exception when call dialog.doModal Pin
Richard MacCutchan8-Aug-11 7:02
mveRichard MacCutchan8-Aug-11 7:02 
GeneralRe: Project Converted From VS2003 to VS2008, get exception when call dialog.doModal Pin
Andraw Tang8-Aug-11 7:18
Andraw Tang8-Aug-11 7:18 
GeneralRe: Project Converted From VS2003 to VS2008, get exception when call dialog.doModal Pin
Richard MacCutchan8-Aug-11 7:38
mveRichard MacCutchan8-Aug-11 7:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.