WString crashes without warning

Hello,

I am trying new API2, Xojo 3.1, and the declaration of WNDCLASSEX WString works with CStr, but seems to crash without warning on API2? Is there another way of displaying a WString, or just keep wrapping it in a CStr() command?

Here is some example code in an action event:

[code]Sub Action() Handles Action
//Create Declares for C++ code
Declare Function GetClassInfoEx Lib “user32” Alias “GetClassInfoW” (hInstance As Integer, _
lpClassName As Ptr, _
ByRef lpWndClass As WNDCLASSEX) As Boolean

//Create a variable for Structure
Dim WCEx As WNDCLASSEX
//Create a variable for the response
Dim IsSuccessful as Boolean
//Call the function GetClassInfo

//Button is a Wstring and requires a null terminating string
Dim MyClassName as New MemoryBlock(256)
MyClassName.WString(0) = “BUTTON” + Chr(0)

IsSuccessful = GetClassInfoEx(PushButton1.Handle, MyClassName, WCEx)

Label1.Value = "IsSuccessful = " + Cstr(IsSuccessful) + EndOfLine +_
"cbSize = " + WCEx.cbSize.ToString + EndOfLine +_
"style = " + WCEx.style.ToString + EndOfLine +_
"lpfnWndProc = " + WCEx.lpfnWndProc.ToString + EndOfLine +_
"cbClsExtra = " + WCEx.cbClsExtra.ToString + EndOfLine +_
"cbWndExtra = " + WCEx.cbWndExtra.ToString + EndOfLine +_
"hInstance = " + WCEx.hInstance.ToString + EndOfLine +_
"hIcon = " + WCEx.hIcon.ToString + EndOfLine +_
"hCursor = " + WCEx.hCursor.ToString + EndOfLine +_
"hbrBackground = " + WCEx.hbrBackground.ToString + EndOfLine +_
"lpszMenuName = " + WCEx.lpszMenuName + EndOfLine +_ //Error Here
"lpszClassName = " + WCEx.lpszClassName + EndOfLine +_ //Error Here
"hIconSm = " + WCEx.hIconSm.ToString
End Sub
[/code]

Here is the structure:

Structure WNDCLASSEX cbSize as Uint32 style as UInt32 lpfnWndProc as Integer cbClsExtra as Int32 cbWndExtra as Int32 hInstance as Integer hIcon as Integer hCursor as Integer hbrBackground as Integer lpszMenuName as WString lpszClassName as WString hIconSm as Integer End Structure

Here is some code that works with wrapping a CStr:

[code]Sub Action() Handles Action
//Create Declares for C++ code
Declare Function GetClassInfoEx Lib “user32” Alias “GetClassInfoW” (hInstance As Integer, _
lpClassName As Ptr, _
ByRef lpWndClass As WNDCLASSEX) As Boolean

//Create a variable for Structure
Dim WCEx As WNDCLASSEX
//Create a variable for the response
Dim IsSuccessful as Boolean
//Call the function GetClassInfo

//Button is a Wstring and requires a null terminating string
Dim MyClassName as New MemoryBlock(256)
MyClassName.WString(0) = “BUTTON” + Chr(0)

IsSuccessful = GetClassInfoEx(PushButton1.Handle, MyClassName, WCEx)

Label1.Value = "IsSuccessful = " + Cstr(IsSuccessful) + EndOfLine +_
"cbSize = " + WCEx.cbSize.ToString + EndOfLine +_
"style = " + WCEx.style.ToString + EndOfLine +_
"lpfnWndProc = " + WCEx.lpfnWndProc.ToString + EndOfLine +_
"cbClsExtra = " + WCEx.cbClsExtra.ToString + EndOfLine +_
"cbWndExtra = " + WCEx.cbWndExtra.ToString + EndOfLine +_
"hInstance = " + WCEx.hInstance.ToString + EndOfLine +_
"hIcon = " + WCEx.hIcon.ToString + EndOfLine +_
"hCursor = " + WCEx.hCursor.ToString + EndOfLine +_
"hbrBackground = " + WCEx.hbrBackground.ToString + EndOfLine +_
"lpszMenuName = " + CStr(WCEx.lpszMenuName) + EndOfLine +_ //<- works
"lpszClassName = " + CStr(WCEx.lpszClassName) + EndOfLine +_ // <- works
"hIconSm = " + WCEx.hIconSm.ToString
End Sub
[/code]

Is this ‘as designed’ or a bug?

Thanks :slight_smile:

Here is a download link without the CStr() wrapper which causes Xojo 3.1 to crash without warning on Windows 10:

Example01-16-API2.zip

You’re trying to convert a Nil pointer to a string which is going to break in any version of xojo, not just API2 :slight_smile: It looks like CStr has an internal check for this.

You could do it one of four ways that I can think of, either cast it to a variant which can cope with Nil WString’s:

"lpszClassName = " + CType(WCEx.lpszClassName, Variant) + EndOfLine + _

or you could change the WString’s to Ptr’s in the structure and use a Nil check:

"lpszMenuName = " + If(WCEx.lpszMenuName = Nil, "", WCEx.lpszMenuName.WString) + EndOfLine + _

or you could add a ToString for WSting and forget it every happened:

"lpszClassName = " + WCEx.lpszClassName.ToString + EndOfLine + _

Public Function ToString(Extends s As WString) as String Return CType(s, Variant) End Function

or you could leave it and use CStr

But yes, it would be nice if WString just returned an empty string rather than crashing.

[quote=486117:@]
But yes, it would be nice if WString just returned an empty string rather than crashing.[/quote]
Sounds like a bug report or feature request is in order

<https://xojo.com/issue/60056> (to be made public asap due to feedback bug)

Thanks again for the good work-arounds Julian. I tried the usual “.Wstring(0)” offset of a pointer, and a few other attempts which didn’t work for a Nil pointer. I’ll remember your code and add them to my toolbox of workarounds for Xojo.

Thank you.

:frowning:
shouldnt be necessary but low level bugs like this dont seem to get priority to make the platform more reliable