[RESOLVED] [2.0] API declaration from VB6 to C#
I've got an API declared in VB6:
VB Code:
Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long
Its passed a RECT structure, that is declared like this:
VB Code:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Am I right thinking that this would be the proper translation?
C# Code:
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hwnd, RECT lpRect);
I dont see what I mightve done wrong, because everytime I try to call the function I get this error message:
A call to PInvoke function 'WindowHandles!WindowHandles.Form1::GetWindowRect' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Re: [2.0] API declaration from VB6 to C#
You need to add the InteropServices. This is VB but if you need C# I can convert it.
vb.net Code:
Imports System.Runtime.InteropServices
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="GetWindowRect")> _
Private Shared Function GetWindowRect (ByVal hwnd As Int32, ByRef lpRect As RECT) As Int32
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Int32
Public Top As Int32
Public Right As Int32
Public Bottom As Int32
End Structure
Re: [2.0] API declaration from VB6 to C#
and the converted C#. Not too much dif from yours.
C# Code:
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public Int32 Left;
public Int32 Top;
public Int32 Right;
public Int32 Bottom;
}
[DllImport("user32.dll", CharSet=CharSet.Auto, EntryPoint="GetWindowRect")]
private extern static Int32 GetWindowRect(Int32 hwnd, ref RECT lpRect);
Re: [2.0] API declaration from VB6 to C#
The API Viewer can give you C# declarations.
Code:
[DllImport("user32.dll")]
static extern int GetWindowRect (
int hwnd,
ref RECT lpRect);
Code:
[StructLayout(LayoutKind.Sequential)]
struct RECT{
internal int Left;
internal int Top;
internal int Right;
internal int Bottom;
}
Re: [2.0] API declaration from VB6 to C#
Other then internal vs public I dont see a difference between your code and mine?
Re: [2.0] API declaration from VB6 to C#
Quote:
Originally Posted by RobDog888
Other then internal vs public I dont see a difference between your code and mine?
I wasn't trying to say that it was better. That code was straight out of the API Viewer. I just posted it as an example of what the API Viewer can do. I've seen you recommend it many times and the fact that you didn't here made me think that perhaps you weren't aware it handled C# too.
Re: [2.0] API declaration from VB6 to C#
Actually since I hardly ever code in C# I forgot :D
I was looking to see what was the difference in the signature as the error message was stating but dont see anything out of the usual.
Re: [2.0] API declaration from VB6 to C#
The original problem presumably was caused by the fact that the second argument was not declare by reference, because that's the only difference I can see.
Re: [2.0] API declaration from VB6 to C#
Yup looks like the only dif and that dif would create the error.
Re: [2.0] API declaration from VB6 to C#
Thanks guys:D I declared the second argument by reference and its working perfectly.
Re: [RESOLVED] [2.0] API declaration from VB6 to C#
You should also e using the dllimport attributes too. ;)
Re: [RESOLVED] [2.0] API declaration from VB6 to C#
Quote:
Originally Posted by RobDog888
You should also e using the dllimport attributes too. ;)
Where should I use it you mean? If you look at my row 9 in the code in my original post, Ive got an dllimport statement just above the API declaration:)
Re: [RESOLVED] [2.0] API declaration from VB6 to C#
But not for the rect structure. See post #3 and #4 :D
Code:
[StructLayout(LayoutKind.Sequential)]