H
Howard Kaikow
In a C# program, I find
[DllImport("user32")]
public static extern bool ShowWindow(int hwnd, int nCmdShow);
which I converted to
<DllImport("user32.dll")> _
Public Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer)
As Integer
End Function
and, the C#
private enum SW
{
HIDE = 0,
SHOWNORMAL = 1,
NORMAL = 1,
SHOWMINIMIZED = 2,
SHOWMAXIMIZED = 3,
MAXIMIZE = 3,
SHOWNOACTIVATE = 4,
SHOW = 5,
MINIMIZE = 6,
SHOWMINNOACTIVE = 7,
SHOWNA = 8,
RESTORE = 9,
SHOWDEFAULT = 10,
FORCEMINIMIZE = 11,
MAX = 11
}
was converted to
Private Enum SW
HIDE = 0
SHOWNORMAL = 1
NORMAL = 1
SHOWMINIMIZED = 2
SHOWMAXIMIZED = 3
MAXIMIZE = 3
SHOWNOACTIVATE = 4
SHOW = 5
MINIMIZE = 6
SHOWMINNOACTIVE = 7
SHOWNA = 8
RESTORE = 9
SHOWDEFAULT = 10
FORCEMINIMIZE = 11
MAX = 11
End Enum
and, the C#
bool wasVisible = ShowWindow(xl.Hwnd, (int)SW.SHOWNA);
was converted to
Dim wasVisible As Boolean = CBool(ShowWindow(xl.Hwnd, CType(SW.SHOWNA,
Integer)))
Why the bool type was used, I know not, as ShowWindow returns an Integer
type
The code runs correctly if, instead of the Dllimports, I use:
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer,
ByVal nCmdShow As Integer) As Integer
If I use the DLLImports, I get the following error on the use of ShowWindow:
"An unhandled exception type 'System.InvalidProgramException' occurred
my.exe.
Additional Information: Error: PInvoke item(field, method) must be Static."
What do I need to do to get this to work using the DLLImports?
P.S. Both Option Explicit and Option Strict are On.
The original C# code is from pages 69-74 of Andrew Whitechapels's book
Microsoft .NET Development for Microsoft Office. Bye thee waye, the book is
very useful, tho it is painful to convert the C# to VB .NET. I expect to
learn a lot about .C#/VB NET doing the comnersion manually.
[DllImport("user32")]
public static extern bool ShowWindow(int hwnd, int nCmdShow);
which I converted to
<DllImport("user32.dll")> _
Public Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer)
As Integer
End Function
and, the C#
private enum SW
{
HIDE = 0,
SHOWNORMAL = 1,
NORMAL = 1,
SHOWMINIMIZED = 2,
SHOWMAXIMIZED = 3,
MAXIMIZE = 3,
SHOWNOACTIVATE = 4,
SHOW = 5,
MINIMIZE = 6,
SHOWMINNOACTIVE = 7,
SHOWNA = 8,
RESTORE = 9,
SHOWDEFAULT = 10,
FORCEMINIMIZE = 11,
MAX = 11
}
was converted to
Private Enum SW
HIDE = 0
SHOWNORMAL = 1
NORMAL = 1
SHOWMINIMIZED = 2
SHOWMAXIMIZED = 3
MAXIMIZE = 3
SHOWNOACTIVATE = 4
SHOW = 5
MINIMIZE = 6
SHOWMINNOACTIVE = 7
SHOWNA = 8
RESTORE = 9
SHOWDEFAULT = 10
FORCEMINIMIZE = 11
MAX = 11
End Enum
and, the C#
bool wasVisible = ShowWindow(xl.Hwnd, (int)SW.SHOWNA);
was converted to
Dim wasVisible As Boolean = CBool(ShowWindow(xl.Hwnd, CType(SW.SHOWNA,
Integer)))
Why the bool type was used, I know not, as ShowWindow returns an Integer
type
The code runs correctly if, instead of the Dllimports, I use:
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer,
ByVal nCmdShow As Integer) As Integer
If I use the DLLImports, I get the following error on the use of ShowWindow:
"An unhandled exception type 'System.InvalidProgramException' occurred
my.exe.
Additional Information: Error: PInvoke item(field, method) must be Static."
What do I need to do to get this to work using the DLLImports?
P.S. Both Option Explicit and Option Strict are On.
The original C# code is from pages 69-74 of Andrew Whitechapels's book
Microsoft .NET Development for Microsoft Office. Bye thee waye, the book is
very useful, tho it is painful to convert the C# to VB .NET. I expect to
learn a lot about .C#/VB NET doing the comnersion manually.