Leo1906

String cant be connected to other string

4 posts in this topic

I need your help on this one ..

I have a function (not written by myself) which gives me the username back (differs from @username, when called with admin rights)
 

MsgBox(0, 0, "C:\Users\"& _GetUsername() & "\blabla")

Func _GetUsername()
    Local $sUsername = ""
    Local $aResult = DllCall("Wtsapi32.dll", "int", "WTSQuerySessionInformationW", "int", 0, "dword", -1, "int", 5, "dword*", 0, "dword*", 0)
    If @error Or $aResult[0] = 0 Then Return SetError(1, 0, 0)
    Local $sUsername = BinaryToString(DllStructGetData(DllStructCreate("byte[" & $aResult[5] & "]", $aResult[4]), 1), 2)
    DllCall("Wtsapi32.dll", "int", "WTSFreeMemory", "ptr", $aResult[4])
    Return $sUsername
EndFunc   ;==>_GetUsername

It seems that the returned String can't work together with other strings. It can't be connected to other strings. And even when made into an array it won't work. You can't add data behind the returned string.
In the first example the "\blabla" won't get added to the string.

The second example with using arrays won't work either. There are more objects in the array than those who get displayed:

#include <Array.au3>

Global $dest = @LocalAppDataDir&"\OGR_Contextmenue"
$a = StringSplit($dest, "\")
$a[3] = _GetUsername()
;~ Local $string = ""
;~ For $i = 1 to $a[0]
;~  $String &= $a[$i] & "\"
;~ Next
$string2 = _ArrayToString($a)
MsgBox(0, 0, $string2)

;~ MsgBox(0, 0, $dest)

Func _GetUsername()
    Local $sUsername = ""
    Local $aResult = DllCall("Wtsapi32.dll", "int", "WTSQuerySessionInformationW", "int", 0, "dword", -1, "int", 5, "dword*", 0, "dword*", 0)
    If @error Or $aResult[0] = 0 Then Return SetError(1, 0, 0)
    Local $sUsername = BinaryToString(DllStructGetData(DllStructCreate("byte[" & $aResult[5] & "]", $aResult[4]), 1), 2)
    DllCall("Wtsapi32.dll", "int", "WTSFreeMemory", "ptr", $aResult[4])
    Return $sUsername
EndFunc   ;==>_GetUsername

So .. whats wrong with this returned String? Even converting it to string wont help: String(_GetUsername())

Any suggestions?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Apparently the last character of the returned string is a null and that screws with the string building (well, it terminates the string). So try stripping the last character of the retrieved username (my commented line), that should do it.

#include <Array.au3>

$un = _GetUsername()
;~ $un = StringLeft($un, stringlen($un) - 1)           ; <-- The solution

For $i = 1 To StringLen($un)
    $char = StringMid($un, $i, 1)
    ConsoleWrite(Asc($char) & " : >" & $char & "<" & @CRLF)
Next

$thing = StringLen($un) & " - 123" & String($un) & "456"    

ConsoleWrite(@CRLF & $thing & @CRLF)

Func _GetUsername()
    Local $sUsername = ""
    Local $aResult = DllCall("Wtsapi32.dll", "int", "WTSQuerySessionInformationW", "int", 0, "dword", -1, "int", 5, "dword*", 0, "dword*", 0)
    If @error Or $aResult[0] = 0 Then Return SetError(1, 0, 0)
    Local $sUsername = BinaryToString(DllStructGetData(DllStructCreate("byte[" & $aResult[5] & "]", $aResult[4]), 1), 2)
    DllCall("Wtsapi32.dll", "int", "WTSFreeMemory", "ptr", $aResult[4])
    Return $sUsername
EndFunc   ;==>_GetUsername

 

Edited by SadBunny
typo
1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Ah okay thank you. First time i got in touch with a null terminated string :D

I added the line to the function before it returns the username:

$sUsername = StringLeft($sUsername, stringlen($sUsername) - 1)

and now it works just fine :)

Thanks for the help :)

Share this post


Link to post
Share on other sites

No problem, have a nice weekend :cheer:


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now