Namespace around API calls


D

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

Advertisements

M

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
 
G

Guest

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

Advertisements

D

David A

Mattias Sjögren said:
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,

Thank you. That works perfectly.
 

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