Home All Groups Group Topic Archive Search About

Namespace around API calls

Author
8 Jan 2006 8:43 AM
David A
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.

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.

Author
8 Jan 2006 9:47 AM
Mattias Sjögren
>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.
Author
8 Jan 2006 4:37 PM
David A
Show quote Hide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
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.

Mattias,

Thank you. That works perfectly.
Author
8 Jan 2006 12:31 PM
AMercer
> 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.  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.
Author
8 Jan 2006 12:40 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"AMercer" <AMer***@discussions.microsoft.com> schrieb:
>> 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.

ACK:

<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/>