|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Namespace around API callsso I can have all the unmanaged calls to DLL functions in a separate module and then these can be called from any other module using the "Imports" statement. But I can't get it to work, at least not in the manner that I'd prefer. You can call the functions using the fully-qualified name, e.g. "MySpecial.UnmanagedCode.MyFunction". But I want to be able to work in a similar manner to the the third test below and call the functions without having to do this, i.e. just call the function MyFunction() directly in my main code. This approach works fine if you have a proper class, and Intellisense "sort of" seems to be seeing the namespace and functions names, but it won't compile. Any suggestions? Example:- FIRST TEST - TWO SIMPLE MODULES -- WORKS OK Module Module1 Sub Main() 'Do equivalent of MsgBox("Hello World") using Win32 API MessageBox(vbNullString, "Hello world", "MessageBox", 0) End Sub End Module Module Module2 Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hWnd As Integer, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Integer) As Integer End Module SECOND TEST - put win32 function in a namespace, call with full dotted name -- WORKS OK Module Module1 Sub Main() MyWin32Namespace.MessageBox(vbNullString, "Hello world", "MessageBox", 0) End Sub End Module Namespace MyWin32Namespace Module Module2 Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hWnd As Integer, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Integer) As Integer End Module End Namespace THIRD TEST - try to Import namespace to avoid having to use the fully-qualified name each time -- FAILS Imports MyWin32Namespace Module Module1 Sub Main() MessageBox(vbNullString, "Hello world", "MessageBox", 0) End Sub End Module Module1.vb(1): Namespace or type 'MyWin32Namespace' for the Imports 'MyWin32Namespace' cannot be found. Module1.vb(5): Name 'MessageBox' is not declared. >Module1.vb(1): Namespace or type 'MyWin32Namespace' for the Imports Check your project properties if you have a root namespace set.>'MyWin32Namespace' cannot be found. >Module1.vb(5): Name 'MessageBox' is not declared. Assuming you have a root namespace of MyProject, you have to change the imports statement to Imports MyProject.MyWin32Namespace Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Show quote
Hide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message Mattias,news:ehIYThDFGHA.2856@TK2MSFTNGP12.phx.gbl... > >Module1.vb(1): Namespace or type 'MyWin32Namespace' for the Imports > >'MyWin32Namespace' cannot be found. > >Module1.vb(5): Name 'MessageBox' is not declared. > > Check your project properties if you have a root namespace set. > Assuming you have a root namespace of MyProject, you have to change > the imports statement to > > Imports MyProject.MyWin32Namespace > > > Mattias > > -- > Mattias Sjögren [C# MVP] mattias @ mvps.org > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com > Please reply only to the newsgroup. Thank you. That works perfectly. > I'm trying to "isolate" some unmanaged calls to DLLs in a separate namespace What you are trying is ok by me, but FYI Fxcop disapproves. They want you > so I can have all the unmanaged calls to DLL functions in a separate module > and then these can be called from any other module using the "Imports" > statement. But I can't get it to work, at least not in the manner that I'd > prefer. to put all unmanaged declares in one of the native method classes (NativeMethods, SafeNativeMethods, UnsafeNativeMethods). They discuss some rules about these classes. The consequence is that you would always have to qualify the api call like NativeMethods.BitBlt(...), and that defeats the purpose of your inquiry in the first place. Just an FYI about an Fxcop style rule.
Show quote
Hide quote
"AMercer" <AMer***@discussions.microsoft.com> schrieb: ACK:>> I'm trying to "isolate" some unmanaged calls to DLLs in a separate >> namespace >> so I can have all the unmanaged calls to DLL functions in a separate >> module >> and then these can be called from any other module using the "Imports" >> statement. But I can't get it to work, at least not in the manner that >> I'd >> prefer. > > What you are trying is ok by me, but FYI Fxcop disapproves. They want you > to put all unmanaged declares in one of the native method classes > (NativeMethods, SafeNativeMethods, UnsafeNativeMethods). They discuss > some > rules about these classes. <URL:http://blogs.msdn.com/brada/articles/361363.aspx> -> "Naming Conventions" -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Summary of issues with VS 2005
2 keys pressed at once .Net 2.0 - Webbrowser control flickering on resize.... Loading UserControl on a ShowDialog Form sequence strange Virtual Serial Port Bluetooth System.IO.Ports rotate gdi object Project help Access SubQuery Help Needed..................... VS2005 combox question ListView vs. ListBox in my Program |
|||||||||||||||||||||||