Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get text from Byte Array #156

Open
kcvinker opened this issue Nov 23, 2016 · 4 comments
Open

How to get text from Byte Array #156

kcvinker opened this issue Nov 23, 2016 · 4 comments

Comments

@kcvinker
Copy link

Hi all,
I am making a class in Boo to automate some tasks. I did it in VB.Net earlier. This is my VB.Net code.
`Public Function EnumWindowProc(ByVal Hwnd As IntPtr, ByVal LParam As IntPtr) As Boolean

    If IsWindowVisible(Hwnd) Then
        Dim Length As Integer = GetWindowTextLength(Hwnd)
        Dim Ret(Length * 2) As Byte
        GetWindowText(Hwnd, Ret, Length + 1)
        Dim Text As String = ""
        For i As Integer = 0 To (Length - 1) * 2
            If Ret(i) <> 0 Then
                Text &= Chr(Ret(i))
            End If
        Next
        If Text <> "" Then    ' if window has a title
            Output.Add(Text)
        Else                    ' if window has no titles
            Output.Add((Hwnd))

        End If


    End If
    Return True

End Function`

And this is my try in Boo.
`private def EnumWindowProc(Hwnd as IntPtr, LParam as IntPtr) as bool:

	if IsWindowVisible(Hwnd):
		Length as int = GetWindowTextLength(Hwnd)
		Ret as (byte) = array(byte, ((Length * 2) + 1))
		GetWindowText(Hwnd, Ret, (Length + 1))
		Text as string = ""
		for i in range(0, (((Length - 1) * 2) + 1)):
			if Ret[i] != 0:
				Text += String.Chr(Ret[i])
		if not string.IsNullOrEmpty(Text):
			// if window has a title
			Output.Add(Text)
		else:
			// if window has no titles
			Output.Add(Hwnd)		
		
	return true		

`
I am having an error in "String.Char(Ret[i]) ". Please help.

@kcvinker
Copy link
Author

edited

@masonwheeler
Copy link
Contributor

What error are you encountering? Can you paste it in here?

@masonwheeler
Copy link
Contributor

Looking at this, it appears that you're starting with UTF-16 characters and trying to lower them to ANSI by discarding the 0s. This will break and give you weird mojibake if you have non-ANSI characters in there anywhere. So I'd start by reinterpreting the buffer as an array of 16-bit elements:

	var Ret = array(byte, (Length * 2) + 1)
	var chars = array(UInt16, Length + 1)
	GetWindowText(Hwnd, Ret, Length + 1)
	Buffer.BlockCopy(Ret, 0, chars, 0, Ret.Length)

If you want an equivalent of VB.NET's Chr() function for 16-bit characters, you'd use Microsoft.VisualBasic.Strings.ChrW. Getting a 16-bit 0 here would mean the end of the string, so you could do it like this. First, make sure to import System.Linq.Enumerable at the top of your file. Then:

	Text = string(chars.TakeWhile({c | c > 0}).Select(Microsoft.VisualBasic.Strings.ChrW).ToArray())

Having said all that, why are you using a GetWindowText that marshals the value back to you as a byte array rather than as a StringBuilder?

@kcvinker
Copy link
Author

kcvinker commented Nov 24, 2016

@masonwheeler ,
Thanks for the reply. To be frank, I was so frustated when i saw the result of my boo program. After posting here, today i started a new project. In this project, i compiled all this vb.net unmanaged code as a class library. Then i am trying to add it in my boo project. Anyways, now i see your reply. Let me try your suggestions.
Frankly speaking, i don't know about how to deal with this with StringBuilder. I am hobby programmer. Anyways, i will check it for sure. I am going to check it in PInvoke.Net. They sure have some samples with stringbuilder.
The code you added in your reply (Last piece of code) is not supporting in my SD. I mean i am not getting any intellisense help. So i am assuming that i am doing something wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants