PDA

View Full Version : Visual Basic Problems


sobored
29th June 2002, 17:36
I'm running Visual Basic .Net, and trying to make a remote control for :winamp:.

I've been having alot of problems, and I've traced them back to my FindWindow function call.
So I wrote a routine to find the window and return the title, and it returns nothing. What am I doing wrong?

I declared GetWindowText, GetWindowTextLength and FindWindow:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

And this is the routine:
Public Function GetWinampTitle() as String
Dim sBuffer as String
Dim hwndWinamp as Long = FindWindow("Winamp v1.x", vbNullString)
Dim lTitle as Long = GetWindowTextLength(hwndWinamp)
GetWindowText(hwndWinamp, sBuffer, lTitle)
Return sBuffer
End Function
(Note: this is VB.NET so the Return statement is just like in C/C++ and variables can be initalized)

Gourou
30th June 2002, 02:58
is FindWindow returning the window handle?

deep_burn
2nd July 2002, 23:40
Ah, VB.
I'm having a similar problem with Visual Basic 6. Now, I know that I'm getting the correct window handle for WinAmp, I checked it with Spy++, and I can send messages to it and control it with (ironically) a WinAmp remote control that I have written. My problem now is that I want the window title of WinAmp, but I can't get it. It appears as if I'm getting the correct length of string, but it's all whitespace.

sWindowText = Space$(255)
r = GetWindowText(hwndWinamp, sWindowText, 255)
sWindowText = Left(sWindowText, r)

"What's wrong with this?", I suppose is the better question.

sobored
3rd July 2002, 22:27
:igor: That's interesting. Try getting rid of the Left statement, so it looks like this:
sWindowText = Space$(255)
Call GetWindowText(hwndWinamp, sWindowText, 255)
Debug.Write(sWindowText) 'use something like this to see what sWindowText is


Maybe GetWindowText isn't returning the right length, in that case try:
GetWindowTextLength(hwnd as Long) as Long
to get the length

In reply to Gourou, I'm not exactly sure, it just returns a bunch on #'s, but i'll check.

deep_burn
4th July 2002, 00:37
After some gratuoitous cutting and pasting, I got it to return what I was more interested in, which was the currently playing song. What I mainly cut up was Gourou's Wondourous VB solution (http://forums.winamp.com/showthread.php?postid=610772). In its entirety, it has a pretty slick interface for getting the hwnd, the current song and playlist, and the current file and file-list.
It also has good examples of how to communicate with WinAmp in there, too.
What I ended up doing was replacing all of my function def's with his, and rewriting my surrounding code to fit. It's pretty slick now.
Oh, and soberd! The hwnd is supposed to be a bunch of numbers. It's how Windows identifies each window that it has open. I don't know if .NET has the same handy tools as VB6 does, but if it do, look for Spy++. It shows you all the different windows running around, and their classIDs and hwnds. It's a good way to make sure that you have the right one.
Be wary, however, because Spy++ returns hwnds in Hex, and the functions return them in Decimal. (due to the fact that they are limited to the Long type)

sobored
4th July 2002, 01:26
Sorry if i mislead you deep_burn, I know what an hwnd is I just meant that I haven't checked it yet, all I knew was that it was returning numbers (i.e. not zero).

deep_burn
4th July 2002, 01:31
Right, cool.

You wouldn't want to take a look at my sources, would you? I have a great (especially because it works) MiniServer for WinAmp that allows control via TCP/IP. If you think that'd help you any, I'll put it here or email it to you, whatever.

sobored
4th July 2002, 01:33
Sure, that'd be great. Send them to super_happy_fun_sauce@hotmail.com

sobored
4th July 2002, 02:08
The return value has gotta be an error.

FindWindow returns 429249343779242904 (Hex:5F5000100080398)

Spy++ (I also have VB6) gives 525208 (Hex:80398) as the Window Handle for the same instance of Winamp. The class name I'm using in FindWindow and the class name Spy++ gives are the same, so what am I doing wrong?!?!?!?! :cry:

I'm reviewing your code now deep_burn, if the answers in there I'll say.

sobored
4th July 2002, 02:18
deep_burn, your FindWindow call is exactly like mine, but mine doesn't work...WHY?!?!?!?!??!?!?! (sorry for that outburst). I know for sure its an error because GetLastError returns the exact same number. I just gotta find out what error it is.

Anyone got any ideas (see first post for the code)

Gourou
4th July 2002, 02:42
mmm, gimme a bit to work on dis

oopsie, dis was edited, I thought of one thing, then another, but the offer still stands.

sobored
4th July 2002, 02:46
I think I figured it out. I'm gonna test this code and if it doesn't work I'll send it to you.

Thx for the offer

sobored
4th July 2002, 03:07
This is my main form. I've converted it to a text file. It's VB.Net but it should be easy to understand.

sobored
4th July 2002, 03:53
IT WORKS!!!!!!!!!!!!!!:D :D :D :D

I was declaring all the API's as with the Long data type, as specified in the API Text Viewer, and as you would use in VB6, but there was one problem!

In VB.Net the Short data type is the same as the VB6 Integer data type, and the Integer data type is the same as the VB6 Long, and the Long data type is just...really big.

So my Handles were too big and were causing Overflow errors! I changed my function definitions to use Integer instead of Long and it worked perfectly!

Thanks for your help anyway

Sobored:D :D :D :D :D

Note to all VB.Net'ers using Win32 API's, don't use Long, use Integer!

deep_burn
4th July 2002, 16:10
You know, score one against Microsoft on that one. They should always keep the same keywords for backwards compatability!
It really should have been something like Integer, Long, and DamnLong in my opinion.

sobored
4th July 2002, 16:52
That's what I was thinking, the only thing that the've done that's even close to that is create Int16 (Short), Int32 (Integer), and Int64 (Long) data types, but then you have to know exactly which one.

Oh well, at least it works now.