WString is not the same as WString?

Hello,

Should a memoryblock of WString have the same binary length as a variable WString?

A WString is a null terminated, 2 bytes-per-character value.

On Windows, when I create a WString variable, here is the Binary that has a length of 8 bytes:

Dim c as WString = "Time" Binary: 5400 6900 6D00 6500

When I attempt to create this type with bytes in a memory block, I need to add 2 bytes to terminate the WString to work, for a total of 10 bytes:

m.Byte(0) = &h54 // ASCII 'T' m.Byte(1) = &h00 m.Byte(2) = &h69 //ASCII 'i' m.Byte(3) = &h00 m.Byte(4) = &h6D //ASCII 'm' m.Byte(5) = &h00 m.Byte(6) = &h65 //ASCII 'e' m.Byte(7) = &h00 m.Byte(8) = &h00 //null termination m.Byte(9) = &h00 Binary: 5400 6900 6D00 6500 0000

To show WString by MemoryBlock or by variable, shouldn’t the WString length be the same? Or is there a case where WString is not the same as Wstring? :slight_smile: Just curious to know what I am missing. Thanks!

Here is the complete code:

[code]Sub Action() Handles Action
//x86 and x64 compatible
//Show a null terminated Wstring
//Create a new MemoryBlock of 8 or 10 bytes?
Dim c as WString = “Time”
Dim m as new MemoryBlock(10)

//Add data in byte location #0
m.Byte(0) = &h54 // ASCII ‘T’
m.Byte(1) = &h00
m.Byte(2) = &h69 //ASCII ‘i’
m.Byte(3) = &h00
m.Byte(4) = &h6D //ASCII ‘m’
m.Byte(5) = &h00
m.Byte(6) = &h65 //ASCII ‘e’
m.Byte(7) = &h00
m.Byte(8) = &h00 //null termination
m.Byte(9) = &h00

//Show data in location #0
LblData.text = "The value of WString is: " + m.WString(0)
LblData1.text = "The value of WString is: " + c
break
End Sub[/code]

There is no scenario in which you should ever need to DIM a variable of type WString. Treat WString (and CString) as a mechanism to convert a String to the format required by some API call.

That said, the terminating Null bytes don’t count as part of the string length. If you use WString as a data type, it provides the extra bytes for you, but if you allocate the storage yourself via a MemoryBlock, you have to account for the extra bytes needed by a CString or WString.

Thanks Tim. This explains a couple portions of code which don’t work - blush. Memoryblocks are my friend. :slight_smile:

Edit: Spelling