DLLImports

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.
 
I

Imran Koradia

Define your function ShowWindow as 'Shared' since 'static' from C# converts
to 'Shared' in VB.NET:

<DllImport("user32.dll")> _
Public Shared Function ShowWindow(ByVal hwnd As Integer, _
ByVal nCmdShow As Integer) As Integer

End Function

hope that helps..
Imran.
 
H

Howard Kaikow

Thanx.

That fixed it.

But, I SWEAR that I had done that before.
That's what I get for swearing.
My fat fingers must have done something else wrong.

--
http://www.standards.com/; See Howard Kaikow's web site.
Imran Koradia said:
Define your function ShowWindow as 'Shared' since 'static' from C# converts
to 'Shared' in VB.NET:

<DllImport("user32.dll")> _
Public Shared Function ShowWindow(ByVal hwnd As Integer, _
ByVal nCmdShow As Integer) As Integer

End Function

hope that helps..
Imran.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top