PC Review


Reply
 
 
Howard Kaikow
Guest
Posts: n/a
 
      8th Mar 2005
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.
--
http://www.standards.com/; See Howard Kaikow's web site.


 
Reply With Quote
 
 
 
 
Imran Koradia
Guest
Posts: n/a
 
      8th Mar 2005
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.
>




 
Reply With Quote
 
Howard Kaikow
Guest
Posts: n/a
 
      8th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
> >

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrapping WinPCap with DLLImports Nuno Magalhaes Microsoft C# .NET 2 28th Oct 2005 02:25 PM
DLLImports Rein Petersen Microsoft C# .NET 1 4th Apr 2004 04:33 PM
DLLImports John Dann Microsoft VB .NET 2 12th Feb 2004 02:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:40 PM.