Can I import a dll for one class only? - vb.net

Good day,
I have used dll imports for "user32.dll" in the past.
However, I am trying to import a class library into my application which has some namespaces which come into conflict with namespaces which are already imported and referenced from other class libraries.
How can I reference this dll and only use the namespaces contained in it, or override the other namespaces imported from other class libraries in one class without it affecting the rest of the application.
I am still pretty new, this may not be possible.
Thank you.

To summarize the relevant issues:
In VB.NET one can use the Declare statement to call win32 functions in DLL's with typical EntryPoint constructs.
.NET Assemblies do not provide classical Win32 type EntryPoints (as such they cannot be 'declared')
If one needs to reference a .NET Assembly [or COM] you need to add a reference to the target library when compiling (usually done in the VS IDE or with the /r: switch)
In some cases Namespaces of such referenced Assemblies may collide with others. (i.E. referencing the same Assembly in different versions)
In that case one needs to import the required (conflicting) Namespaces with an Alias
For example:
Assuming you have an assembly with a root namespace Net that could collide with System.Net use:
Imports System
Imports ExtNet = SomeNetworkAssembly
Then in this case to access members of that assembly use ExtNet instead of Net
Note you can name the ExtNet part as you wish.
In C# one can do it via the using keyword instead.

Related

Cannot import `Imports System.Web.Script.Serialization`

When I try to Imports System.Web.Script.Serialization, I get an error in VB 2010 that says:
Warning: Namespace or type specified in the Imports System.Web.Script.Serialization doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
Not sure why I can't import it.
You might be missing an assembly reference to System.Web.Extensions.dll. Add this reference to your project, then try again.
Generally speaking, when you encounter this issue, go to the .NET API reference page of the type you want to use — for instance, JavaScriptConverter — and look out for the Namespace and Assembly hints (make sure you're looking at the page for the .NET framework version that you are using):
Namespace: tells you what to put in the Imports directive.
Assembly: tells you what assembly you need to reference in your project (e.g. go to Solution Explorer, locate References, and select Add Reference… from the context menu).
Also make sure to check the "Target Framework" in the project properties. If you are targeting a "Client Profile" framework then the assembly System.Web.Extensions.dll will not be available to add as a reference to your project.

Create C++ ATL COM nested namespaces like System namespace in .NET

I have several ATL COM services and would like each of them to have their own namespace, but be under a single base namespace, just like the System namespace in .NET.
For example if a C# project were to include these COM objects, all would be under the same base namespace:
using MyCompanyName.Terminator;
using MyCompanyName.Superman;
using MyCompanyName.Thor;
... instead, what I have currently is this:
using Terminator;
using Superman;
using Thor;
... which is what I do NOT want. I want to be able have a base namespace and sub-namespaces under that base. I don't know how to do this when creating an ATL service and what I need to modify to do this. Is it something I modify in the IDL file?
In case you are targeting managed clients it is possible to provide namespaces for them! However since COM is language independent you cannot provide namespaces using the interface description (type library). But whenever you are creating managed wrapper assemblies (that are actually referenced by the client), they can have namespaces to address the RCW objects. The keyword you are looking for is Primary Interop Assemblies. Those are assemblies that you as the vendor of the original library provide for clients to reference. To simplify this: You are doing the work, Visual Studio does for you when you are adding a reference to a COM library. You are creating the interop assembly and the customer does not reference the type library, but the assembly you generated. Using the tlbimp.exe tool it is possible to encapsulate the RCW types inside a namespace using the /namespace parameter.

a statically linked function is not visible in a dll dumpbin output

I have a dynamic library A.dll which consumes a static library B.lib containing a function "int foo()". When I use dumpbin (on win32, vs2010) to view the symbols in dll - foo is not visible - Is that expected? or is it Microsoft specific? I tested this on linux and that is not the case on linux.
Thanks very much for your patience to read this through.
Kapil.
Yes, this is expected. As far as the imported symbols are concerned, using dumpbin, you can see the so called Import Address Table and the Import Name Table which both (typically) exist as soon as at least one function is imported by an application (in your case A.dll). Since your application imports one function from a STATIC library (in your case B.lib), NO entry exist in the imports tables mentioned above for the functions used from B.lib. Once a library is STATICALLY linked to an application, its body (code) is part of the application. As well as the functions of your application are not visible using dumpbin, the functions of the static library are not visible to dumpbin!

Working of "imports namespace"

I knew from here that we have to do the explicit imports for child namespace because imports will not include the child namespace.
But my question is that if I do "imports System" whether it will include all the classes/methods inside that namespace in the IL/native code or only referred ( used inside the application) will be included in the IL/native code.
Importing a namespace doesn't mean that anything is included in the code. It only means that the compiler recognises identifiers from that namespace.
The references in your project are what really decides which libraries the application is using. Still, the libraries are loaded when needed, they are not included in your executable file.

vb.net creating and using namespace

I've googled for creation of namespaces and found some very useful examples, what these examples didn't have is how do I compile and implement my created namespace on my system so I can include it from my various applications.
So for example, if I create a namespace to load a config file from my application path and insert it to an array, Do i need to include the namespace on any project I use or is there a way to make it part of my environment?
You're thinking of Class Library (DLL) projects.
When you start up a new Visual Studio project, select Class Library rather than Windows Form project. This will compile your namespaces as a DLL (exposing your public classes), which can be referenced in other projects.
If you want to include a namespace that you created you have to add a reference to your project first. If you have compiled your code into a .dll file, then simply add the reference to the .dll file to your project and then at the top of your classes put the "Imports [Namespace]". If you haven't compiled your namespace, add the project (with the namespace that you created) to your solution, add the reference to it (under the Projects tab), and then use the Imports statement.
You are confusing the concept of a namespace with the concept of a project, especially of a class library project.
A class exists within a namespace. If no namespace is defined, then the class still exists within the global namespace (the one with no name).
In any case, it's classes that do the work. Namespaces are only so that you can have a class named Book, and I can have a class named Book, and so that TriDat.Book can exist at the same time as JohnSaunders.Book.