tags:

views:

43

answers:

2

I am new to vb.net (framework 4) and visual studio is giving me warnings on the following declarations.

Private Declare Auto Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

Private Declare Auto Function GetWindowText Lib "user32" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As IntPtr) As IntPtr

Private Declare Auto Function keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) As Boolean

Private Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Private Declare Auto Function SetForegroundWindow Lib "user32" (ByVal hWnd As IntPtr) As Boolean

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)

and more importantly how do I find out the correct declaration in future?

+2  A: 

It looks like you are converting a vb6 app. All these functions have .net Equivalents, use them.

David
I don't mean to be rude, but where might I find such equivalents?
Jigs
for the sleep function useSystem.Threading.Thread.Sleep(no_of_milliseconds)If you create a form most of the other functions are properties of the form
David
+2  A: 

I cannot imagine you getting a warning from these declarations. But yes, EnumChildWindows is wrong and impossible to use in a VB.NET program. Fix:

Private Delegate Function EnumChildCallback(ByVal hWnd As IntPtr, ByVal lparam As IntPtr) As Boolean

Private Declare Auto Function EnumChildWindows Lib "user32" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumChildCallback, ByVal lParam As IntPtr) As Boolean

The cch argument of GetWindowText is wrong, it is Integer. The rest are good.

Pinvoke.net is a reasonable decent resource for these declarations. Or you could use the P/Invoke Interop Assistant tool. Neither of them are perfect, only SO is.

Hans Passant