473,587 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Namespace around API calls

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.Unma nagedCode.MyFun ction". 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(vbNu llString, "Hello world", "MessageBox ", 0)
End Sub
End Module

Module Module2
Public Declare Function MessageBox Lib "user32" Alias "MessageBox A"
(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()
MyWin32Namespac e.MessageBox(vb NullString, "Hello world",
"MessageBox ", 0)
End Sub
End Module

Namespace MyWin32Namespac e
Module Module2
Public Declare Function MessageBox Lib "user32" Alias "MessageBox A"
(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 MyWin32Namespac e
Module Module1
Sub Main()
MessageBox(vbNu llString, "Hello world", "MessageBox ", 0)
End Sub
End Module

Module1.vb(1): Namespace or type 'MyWin32Namespa ce' for the Imports
'MyWin32Namespa ce' cannot be found.
Module1.vb(5): Name 'MessageBox' is not declared.
Jan 8 '06 #1
4 2520
>Module1.vb(1 ): Namespace or type 'MyWin32Namespa ce' for the Imports
'MyWin32Namesp ace' 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.MyWin 32Namespace
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 8 '06 #2
> 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, SafeNativeMetho ds, UnsafeNativeMet hods). They discuss some
rules about these classes. The consequence is that you would always have to
qualify the api call like NativeMethods.B itBlt(...), and that defeats the
purpose of your inquiry in the first place. Just an FYI about an Fxcop style
rule.

Jan 8 '06 #3
"AMercer" <AM*****@discus sions.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, SafeNativeMetho ds, UnsafeNativeMet hods). 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/>

Jan 8 '06 #4
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:eh******** ******@TK2MSFTN GP12.phx.gbl...
Module1.vb(1 ): Namespace or type 'MyWin32Namespa ce' for the Imports
'MyWin32Namesp ace' 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.MyWin 32Namespace
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.
Jan 8 '06 #5

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

Similar topics

88
5105
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from the days before PHP got object-oriented features. For instance we currently have:
3
5644
by: Razmig K | last post by:
Dear mates, This is a small survey for C++ programmers to speak their mind about the uses and/or potential advantages and disadvantages resulting from the use of the aforementioned C++ feature in ralistic code in various programming domains. Thank you. //rk
1
2717
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a namespace prefix, what is its namespace, if default namespaces do not apply? Are (either of) prefixed or non-prefixed attributes correct? For example, are the following equivalent: <html:br class="foo"...
20
3853
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in a named namespace namespace foo { class Foo
6
8335
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get the dreaded <elementname xmlns=''> was not expected exception. If I comment out the XmlTypeAttribute, it works just fine. Just so you know, when I instantiate an instance of an XmlSerializer, I pass a default namespace to the ctor. ...
5
2458
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
2
4079
by: Mike Fiedler | last post by:
On a client web services request, I have a SoapExtension that is adding a header to the message in the BeforeSerialize stage. If the Web Service request is for an operation with style="document"/use="literal", everything works fine - the header is added, message is serialized and all is ok. If the Web Service request is for an operation with style="rpc"/use="encoded":
2
18723
by: james.cssa | last post by:
I want to declare namespace prefix in the Envelope element (i.e. xmlns:xsi and xmlns:xsd) so that the document will be serialized to look like the following. However, I don't know the right way to do that in Java. <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:xsd='http://www.w3.org/1999/XMLSchema'>
3
7778
by: Frederick Gotham | last post by:
Back in the day, if you wanted a function to be self-contained within a translation unit, you defined the function as "static". If there were an external linkage function by the same name residing in a different translation unit, then the current translation unit was simply oblivious to it and had no way of accessing it. Any time the function name was mentioned in the current translation unit, it referred to the "static" one which...
0
7927
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7857
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2367
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.