Gå til innhold

Hvordan bruke denne API`n HASTER


Anbefalte innlegg

Hei...

 

DET HASTER

 

Jeg må vite hvordan i helvete denne funker:

Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean

 

Kan noen lage et fungerende eksempel som fyller opp en listbox med handlerene til alle windownw... har prøvd og prøvd.. sett på eksempler.. googla.. og alt ja..

 

Så der etter bruke alle handlerene til å finne caption til alle åpne form... men det er en annen sak igen.. et skritt om gangen.. jeg må vite hvordan dette funker?

 

EVIG TUSEN TUSEN TAKK TIL DEN SOM KLARER OPP I DETTE FOR MEG :innocent:

Lenke til kommentar
Videoannonse
Annonse

Den er enkel, det eneste viktige er at du MA deklarere en public EnumProc funksjon i en egen Modul:

 

Form1:

Private Sub Form_Load()
EnumWindows AddressOf EnumProc, 5
End Sub

 

Modul1:

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 EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean

Public Function EnumProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Form1.List1.AddItem hWnd
EnumProc = True'For og fortsette
End Function

'For og liste alle Tittlene:
Public Function GetCaption(hwnd As Long) As String
Dim strBuffer As String
strBuffer = String(255, Chr$(0))
GetWindowText hwnd, strBuffer, 255
GetCaption = Left(strBuffer, InStr(1, strBuffer, Chr(0)) - 1)
End Function

 

[EDIT] For og gjore det litt enklere til neste gang, fa tak i API-Guide fra AllAPI.org, denne lister en god del av de mest brukte API'ene og eksempler til alle i sammen...

Enum_Windows.zip

Endret av Richard87
Lenke til kommentar

Takker og bukker :D:w00t:

 

*smile og anstrenge meg selv fra å hoppe opp å ned :p *

 

EDIT:

 

Men du eg fora koden din inn i VS.NET gjennom sånn upgrade engine...

Det eneste den stussa på hva adressof ordet.. dette stod over ordet

'UPGRADE_WARNING: Add a delegate for AddressOf EnumProc

 

Ka i steike banan er en delegate... ? :dontgetit:

Endret av chills
Lenke til kommentar
A callback function is code within a managed application that helps an unmanaged DLL function complete a task. Calls to a callback function pass indirectly from a managed application, through a DLL function, and back to the managed implementation. Some of the many DLL functions called with platform invoke require a callback function in managed code to run properly. This topic describes the elements of a callback function, and how to implement and call one from managed code.

Callback Function Basics

 

To call most DLL functions from managed code, you create a managed definition of the function and then call it. The process is straightforward.

 

Using a DLL function that requires a callback function has some additional steps. First, you must determine whether the function requires a callback by looking at the documentation for the function. Next, you have to create the callback function in your managed application. Finally, you call the DLL function, passing a pointer to the callback function as an argument. The following illustration summarizes these steps.

 

Callback function and implementation

 

Callback functions are ideal for use in situations in which a task is performed repeatedly. Another common usage is with enumeration functions, such as EnumFontFamilies, EnumPrinters, and EnumWindows in the Win32 API. As the example in the next section demonstrates, the EnumWindows function enumerates through all existing windows on your computer, calling the callback function to perform a task on each window.

Implementing a Callback Function

 

The following process demonstrates how a managed application, using platform invoke, can print the handle value for each window on the local computer. Specifically, the example uses the EnumWindows function to step through the list of windows and a managed callback function (named CallBack) to print the value of the window handle.

 

To implement a Callback function

 

  1. Look at the signature for the EnumWindows function before going further with the implementation. EnumWindows has the following signature:

 

BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)

 

      One clue that this function requires a callback is the presence of the lpEnumFunc argument. It is common to see the lp (long pointer) prefix combined with the Func suffix in the name of arguments that take a pointer to a callback function. For documentation about Win32 functions, see the Microsoft Platform SDK.

  2. Create the managed callback function. The example declares a delegate type, called CallBack, which takes two arguments (hwnd and lparam). The first argument is a handle to the window; the second argument is application-defined. In this release, both arguments must be integers.

 

      Callback functions generally return nonzero values to indicate success and zero to indicate failure. This example explicitly sets the return value to true to continue the enumeration.

  3. Create a delegate and pass it as an argument to the EnumWindows function. Platform invoke converts the delegate to a familiar callback format automatically.

  4. Ensure that the garbage collector does not reclaim the delegate before the callback function completes its work. When you pass a delegate as a parameter, or pass a delegate contained as a field in a structure, it remains uncollected for the duration of the call. So, as is the case in the following enumeration example, the callback function completes its work before the call returns and requires no additional action by the managed caller.

 

      If, however, the callback function can be invoked after the call returns, the managed caller must take steps to ensure that the delegate remains uncollected until the callback function finishes. For detailed information about preventing garbage collection, see Interop Marshaling with Platform Invoke.

Imports System
Imports System.Runtime.InteropServices

Public Delegate Function CallBack( _
hwnd As Integer, lParam As Integer) As Boolean

Public Class EnumReportApp

   Declare Function EnumWindows Lib "user32" ( _
      x As CallBack, y As Integer) As Integer

   Public Shared Sub Main()
       EnumWindows(AddressOf EnumReportApp.Report, 0)
   End Sub 'Main

   Public Shared Function Report(hwnd As Integer, lParam As Integer) _
   As Boolean
       Console.Write("Window handle is ")
       Console.WriteLine(hwnd)
       Return True
   End Function 'Report
End Class 'EnumReportApp

MSDN

 

Haper dette hjelper

Lenke til kommentar

Takker for hjelpen... :) så LANGT :innocent: hehehehe :p

 

Her er koden i formet så langt:

Public Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
   ' Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
   Declare Function EnumWindows Lib "user32" ( _
   ByVal x As CallBack, ByVal y As Integer) As Integer
   Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       EnumWindows(AddressOf Form1.Report, 0)
   End Sub
   Public Shared Function Report(ByVal hwnd As Integer, ByVal lParam As Integer) _
  As Boolean
       Dim strBuffer As String
       strBuffer = New String(Chr(0), 255)
       GetWindowText(hwnd, strBuffer, 255)
       Debug.WriteLine(hwnd)
       Debug.WriteLine(strBuffer)
       Return True
   End Function

 

Gettext greien funker isje.. og viss eg prøver å fylle en listbox direkte fra report funksjonene så får eg feil.. HALLO koffor får eg feil der... ?

 

listbox1.items.add(hwnd)

Hmmm liker isje den.. får opp dette.. :

thihi.JPG

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...