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