API Calls from .net

jimmay
07-16-2007, 10:53 AM
Hello, trying to do some api calls from .net, but I am receiving some errors. Here is the code:


Imports System.runtime.interopservices
Public Class Form1


Private Function fg_hwnd() As Long
fg_hwnd = w32.GetForeGroundWindow()
End Function

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

w32.SetForegroundWindow(197574)

'Dim ctr As Integer

'For ctr = 0 To 5
' TextBox1.Text &= fg_hwnd() & vbCrLf
' System.Threading.Thread.Sleep(1000)
'Next
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

Public Class w32
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function GetForegroundWindow() As Long
End Function


<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function SetForegroundWindow(ByVal hwnd As Long) As Long
End Function


End Class



So, the crash occurs when I call SetForegroundWindow. Yes, it does set the window to that handle, but i get an MDA error - "PInvokeStackImbalance". MSDN states the following:

" This mismatch can be caused by the managed signature not declaring the correct number of parameters or not specifying the appropriate size for the parameters. The MDA can also activate because the calling convention, possibly specified by the DllImportAttribute attribute, does not match the unmanaged calling convention. "

"Review the managed platform invoke signature and calling convention to confirm it matches the signature and calling convention of the native target. Try explicitly specifying the calling convention on both the managed and unmanaged sides. "

Does anyone know how I can make the two match up? Is there a common way to make sure my declare matches up with how the function is set in the dll?

Also, I noticed that the long integer returned by the getforegroundwindow is much longer than in previous visual basic versions....

AtmaWeapon
07-16-2007, 11:18 AM
Also, I noticed that the long integer returned by the getforegroundwindow is much longer than in previous visual basic versions....This was your hint as to why the stack imbalance existed.

VB6 used 16-bit integers for some weird reason; in VB .NET Integer is an alias for the CLR type Int32. Long is an alias for the CLR type Int64, which is much larger than the value the API function expects.

The problem with just using Integer in this case is that one day, VB .NET may standardize on 64-bit integers or some other type.

There is an IntPtr type that was specifically created for working with handles and pointers; on 32-bit systems it is always a 32-bit value and on 64-bit systems it is a 64-bit value.

There is a very helpful site, http://www.pinvoke.net, that shows the proper P/Invoke signature for most (if not all) API functions. The proper declaration for the functions you are using is:<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function

<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(hWnd as IntPtr) As Boolean
End FunctionI believe if you use these declarations and IntPtr types then things should work properly.

jimmay
07-16-2007, 11:25 AM
Yes, I thought it was something like a conversion problem....what i tried later was...


Public Class w32



Declare Auto Function GetForegroundWindow Lib "User32.dll" () As Integer
Declare Auto Function SetForegroundWindow Lib "User32.dll" (ByVal hwnd As Integer) As Integer

End Class



This works great.

Just wondering, is there managed code versions native to .net to achieve the above api calls?

MKoslof
07-16-2007, 11:43 AM
Well, remember any managed code in this realm is most likely just a wrapper around the unmanaged code you need to implement in the first place.

If you are interested in look into some built wrappers for the Windows API in .Net. You can start here:

http://mwinapi.sourceforge.net/

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum