API call

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, im trying to get the caption text from the active window but i cant get it working in vb.net. Can somebody please help me. Im new to using API

Public Class Form
Inherits System.Windows.Forms.For

#Region " Windows Form Designer generated code

' Return Caption.ToString(
Public Declare Function GetActiveWindow Lib "user32" () As System.IntPt
Public Declare Auto Function GetWindowText Lib "user32"
(ByVal hWnd As System.IntPtr,
ByVal lpString As System.Text.StringBuilder,
ByVal cch As Integer) As Intege

' Create a buffer of 256 character
Dim Caption As New System.Text.StringBuilder(256
Dim hWnd As IntPtr = GetActiveWindow(
Dim t As Strin

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
Dim t As Strin

t = GetWindowText(hWnd, Caption, Caption.Capacity) 'put the caption text in strin
End Su
End Class
 
sneffe said:
Hi, im trying to get the caption text from the active window but i cant get it
working in vb.net. Can somebody please help me. Im new to using API.
[...]
Dim t As String

t = GetWindowText(hWnd, Caption, Caption.Capacity) 'put the
caption text in string

What's the exact problem? Can't the code be compiled (congrats, then you
correctly enabled option strict) or do you get a runtime error? Do you need
a description of the API functions? The MSDN library contains them: Put the
cursor on the function and press <F1>.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Your GetActiveWindow() isn't initialized to any value. And also,
GetWindowText doesn't return a string. It returns a Integer. Use your
stringbuilder variable to get the Caption of the window.

Put this your button click event


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim hWnd As IntPtr = GetActiveWindow()
GetWindowText(hWnd, Caption, Caption.Capacity)
Debug.WriteLine(Caption)
End Sub
 
Please use the MSDN to correct your GetWindowText declaration and use
Namely what does it return and the meaning of each parameter
 
Back
Top