using api in vb.net

Hi,
I am migrating an application from VB6 to VB.NET at the moment. There is a Windows API call in one of the subroutine which will not run through, see below (words in red are added by VB.NET):
Private Sub hook()
'UPGRADE_WARNING: Add a delegate for AddressOf WindowProc Click for more:...
lpPrevWndProc = SetWindowLong(gWH, GWL_WNDPROC, AddressOf WindowProc)
End Sub
my question is how can i add a delegate for windowProc how does a delegate work
Thanks


Answer this question

using api in vb.net

  • MarkOlbert

    I think just adding

    Sub WindowProc

    End Sub

    would work, hmm.

    Interesting code there, does it subclass another window Can you subclass windows in this way

    Ahmedilyas



  • twolfkg

    by putting:
    "System.Threading.Thread.Sleep(1)"
    into the do loop before the "System.Windows.Forms.Application.DoEvents()", the program will run just fine.
    But will try the process class approach later.



  • lachlanj

    that might be a solution, i will try it, thanks.

  • ignitionflip

    You can override the windows messaging procedure as follows

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    MyBase.WndProc(m)

    End Sub

    Not sure if you actually need the delegate but here is the info....

    TO use a delegate:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vblr7/html/vastmdelegate.asp

    Delegate Sub MyWinProc(ByRef m As System.Windows.Forms.Message)



  • Philly10407

    actually "Address of " is from the VB6, it is not inserted by VB.NET, and now i think the delegate is not a problem as the program can go pass it...sorry, my mistake.. But another related problem occured..

    what happened is that this project is tring to launch a Fortran (executable) program at the back stage, then receive results from it, show them in the main form (say in a listbox). The project needs to use some windows APIs to achieve this task.

    The prosedure is outlined below:

    firstly, do the hooking:
    lpPrevWndProc = SetWindowLong(gWH, GWL_WNDPROC, AddressOf WindowProc)

    then, run the Fortran program:<Fortran program has been described in ShellExInfo structure>

    ShellExecuteEx(ShellExInfo)

    then use a do loop to 'catch' the messages from Fortran program, process it in WindowProc:
    dim wait as boolean
    If wait Then
    do
    System.Windows.Forms.Application.DoEvents()
    Call GetExitCodeProcess(ShellExInfo.hProcess, dwExitCode)
    Loop Until dwExitCode <> STILL_ACTIVE
    End If

    the problem is i have to put a break point at "System.Windows.Forms.Application.DoEvents()" to capture all the results from the program, if i remove that, only a couple lines of results will be returned, and then Visual studio will stop at that line and issue a warning: "A callback was made on a garbage collected delegate of type 'Project1!Project1.MsgReceive+SubClassProcDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

    How should i do now


    Note: Part of the declaration is:

    Declare Function SetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer

    Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Declare Function SetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal attr As Integer, ByVal lVal As SubClassProcDelegate) As Integer

  • Pockey

    You DEFINATELY need to use the System.Diagnostics.Process class! Learn about it and it will make your life much easier, please!

  • Andy Mil

    ShellExecuteEx(ShellExInfo)

    Have you tried upgrading the ShellExecuteEx method to the Process class...The process class allows io redirection and has a waitfor exit method to ensure that the process has completed before continuing in your code



  • using api in vb.net