473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

DLLImports

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.
Nov 21 '05 #1
2 5721
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.


Nov 21 '05 #2
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" <no****@microsoft.com> wrote in message
news:13**********************@news.microsoft.com.. .
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.


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jennifereden.price | last post by:
Hi, I have the following function in a project that implements a shell extension. This function shows all the files that the users has selected and attempts to determine whether it is a...
2
by: Nuno Magalhaes | last post by:
Hello all, Below I have a function that captures a packet and sends that packet to a Receive function (wether it is sent or received). I'm asking what functions should I use (what imports...
6
by: Daren Hawes | last post by:
Hi, My web host has a low security setting on the shared .net server I use. I cannot use third party dll's or install assemblies in the GAC. All I need to do is verify that a FTP login is...
2
by: John Dann | last post by:
One detail about using DLLImports as part of the PInvoke process that I don't understand: I'm trying to access a function (eg MyFunction) in a 3rd party DLL so I create a class: Imports...
2
by: Robin Tucker | last post by:
I need to use OleSetClipboard in my VB.NET application. Unfortunately, when I do the result returned is -2147221008 (CoInitialise has not been called). As I am using OleSetClipboard from a thread...
0
by: Robin Tucker | last post by:
I'm going to kindof restart this thread, because its going up in the list (on my browser anyway). I'm still not able to call OleSetClipboard from within my thread in my VB application. As a...
0
by: lushdog | last post by:
Hi, i'm writing a shell extension in c# that will add two menu's to the right-click menu of explorer if any file is selected, i.e. it's registered in the * section of Classes in the registry. ...
0
by: lushdog | last post by:
Hi, i'm having a little problem with my context menu handler. Basically if i select multiple files and right-click OPEN or EDIT, it uses my context menu to open it. It is because i am missing this...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.