Skip to content

String casting problem #462

@verh010m

Description

@verh010m

There's a simple function that reads some data from a structure. Ignore the unfamiliar calls starting with "_" - these are internal functions and they return the correct result. Everything works perfectly until I try to return a string from the function. I ALWAYS get an empty string. Initially, I used the ANSI version of the Win32 API. Then ChatGPT suggested using the wide version. Both gave an empty string as output. That is, in the debugger (I'm using QT Creator), I see that the structure is filled correctly, there is meaningful data, the memory editor shows everything correctly. But as soon as I try to cast the DeviceName field to a String type, I get an empty string as output. The variable in the debugger shows the following data:

data = 0x1
len = 6683552
size = 4282752

In the ANSI version, instead of

Dim As String str_result

I tried declaring

Dim As ZString*32 zstr_result

and then using memcpy to directly copy the data from the DeviceName field of the structure. And I got garbage content. ChatGPT thinks these are UTF-16 strings

Function _monitorName(ByVal index As UInteger) As String
	' get the monitor count
	' if monitor count is 0 (zero) then return empty string
	Dim As UInteger monitor_count = 0: monitor_count = _monitorCount()
	If monitor_count = 0 Then Return ""
	
	' check index to correct value
	' if index is greather than monitor count then return empty string
	' monitor_count based in 0 (zero) first position
	If monitor_count - 1 < index Then Return ""
	
	' struct to obtain information
	Dim As DISPLAY_DEVICEW dda
	Clear dda, , SizeOf(dda)
	dda.cb = SizeOf(dda)
	
	' get information
	' if failed then return empty string
	Dim As Const WINBOOL bln_ret = EnumDisplayDevicesW(NULL, index, @dda, 0)
	If Not bln_ret Then Return ""
	
	' ok. Get the string with device name
	Dim As String str_result = Str(*Cast(WString Ptr, @dda.DeviceName[0]))   <<<<<<<<<< ?????
	Return str_result
End Function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions