|
-
Oct 23rd, 2007, 04:31 PM
#1
[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.
-
Oct 23rd, 2007, 06:01 PM
#2
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 23rd, 2007, 06:06 PM
#3
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);
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 23rd, 2007, 10:34 PM
#4
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;
}
-
Oct 24th, 2007, 01:55 AM
#5
Re: [2.0] API declaration from VB6 to C#
Other then internal vs public I dont see a difference between your code and mine?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 24th, 2007, 02:02 AM
#6
Re: [2.0] API declaration from VB6 to C#
 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.
-
Oct 24th, 2007, 02:13 AM
#7
Re: [2.0] API declaration from VB6 to C#
Actually since I hardly ever code in C# I forgot 
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 24th, 2007, 02:36 AM
#8
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.
-
Oct 24th, 2007, 02:49 AM
#9
Re: [2.0] API declaration from VB6 to C#
Yup looks like the only dif and that dif would create the error.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 24th, 2007, 05:44 AM
#10
Re: [2.0] API declaration from VB6 to C#
Thanks guys I declared the second argument by reference and its working perfectly.
-
Oct 24th, 2007, 12:15 PM
#11
Re: [RESOLVED] [2.0] API declaration from VB6 to C#
You should also e using the dllimport attributes too.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 24th, 2007, 12:26 PM
#12
Re: [RESOLVED] [2.0] API declaration from VB6 to C#
 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
-
Oct 24th, 2007, 12:30 PM
#13
Re: [RESOLVED] [2.0] API declaration from VB6 to C#
But not for the rect structure. See post #3 and #4 
Code:
[StructLayout(LayoutKind.Sequential)]
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|