Classes in a DLL - c#

I was using a DLL in C# and called a method which returned me a C# object of the DBConnection class. My question is, if the DLL don’t know which language it will be used on, how can it return an object of a C# class?

Not all DLLs are created equal. Some are COM-specific. Some are not. Some are .Net IL assemblies. Some are not. This DLL is a .Net assembly. The class objects it provides for your are not strictly C# objects. They are .Net objects.
This works out for you because C# is itself built for .Net, and uses .Net objects. If you were using VB.Net, F#, IronPython, C++CLR, or other platform that uses .Net, you'd also be able to use the DLL. But C, Java, VBA etc would have a much harder time.

The .NET assembly is a standard independent of language.
If you write a code in c# it will generate the same "assembly" .NET than VB .NET or F#.
The DLL does not return a "C# Object". The DLL return a .NET object.

Related

Can IronPython call .net dll directly?

I am new to .net programming. I heard all .net supported languages can call .net dll even written by another .net supported languages. My question is that: Can IronPython call .net dll written by c# directly or easily under its command interpretation window?
Yes, you should be able to call any managed dll.
An example of how you would do that is detailed in https://stackoverflow.com/a/14210917/413672

Using C# dll in C++ code

I need to integrate this C# dll
in my C++ code. I want to call some functions written in C# from dll and the rest of code write in C++. What is the easiest and quickest way to do it? The program will be executed only on Windows.
There are basically two cases to call a .NET DLL from unmanaged code:
The .NET DLL exposes a COM interface. In this case, you can use COM from your C++ code.
The .NET DLL does not expose a COM interface. In this case, you have two possibilities (to make it simple):
2.a. host the CLR as described here: Loading the Common Language Runtime into a Process
2.b. write a piece of managed C++ code (another DLL - written in C++/CLI) to wrap the .NET DLL and expose 'old way' DLL exports to unmanaged clients.
I don't specifically know the sharpbox system, but it looks like it's pure .NET and does not expose COM interfaces, so 2.b might be the best way to do it (not so easy...). Maybe it has a REST/Web easier API you could use.
PS: you can also add exports to a .NET DLL. This is described here: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.

.NET wrapper for Webkit

I've created an browser in Visual Studio 2011 with the WebKit .NET wrapper. But since I'm new to C# I maybe have a strange question...
Why can't I just use: http://www.webkit.org/ for my browser? And if that's impossible, how hard would it be to create an .NET wrapper for WebKit?? And how...
Because Webkit was written in C++, not in C#. A translation layer is needed to marshal between the managed code execution environment of C# and the unmanaged code in Webkit. That's not particularly difficult for Webkit, it supports a COM automation interface. Something that .NET supports well.
The necessary starting point is the type library for Webkit. That's the COM version of assembly metadata, it describes the unmanaged COM interface types in a language neutral manner. The .NET Tlbimp.exe tool translates the type library into a .NET interop library. Easy to do in Visual Studio, you use Project + Add Reference, Browse tab and select the Webkit.tlb file. That automatically generates the Webkit.Interop.dll assembly, the .NET version of the COM interface.
As you might suspect, that interface is not particularly small. From there, you could write friendly .NET wrapper classes that hide the interface complexity, the tack taken by this SourceForge project. Studying it to see how it uses the interface should be enlightening. The .NET WebBrowser control and HtmlDocument and HtmlElement classes work the exact same way, but for IE.

Call a .net dll from VB

whether .net framework required to run a .tlb file generated from c#?
You have to:
make sure the classes that you want to use in your VB app are ComVisible
make sure the classes that you want to use in your VB app have a Guid assigned to it
it is best practice to create an interface for those classes
create a tlb file from the assembly using regasm
The .NET framework (runtime) needs to be installed. It is not that regasm will 'compile' your assembly into another language or something that does not need the .NET runtime. A runtime callable wrapper is created, so that you can invoke the .NET assembly as from VB6, which means the .NET runtime is still required.
In VB.Net you can use it normally. In previous version use Runtime Callable Wrapper. More information is here in this KB article.

How to create native DLL in Visual Studio from C# code?

I have the source code of a C# program. I want to create a DLL out of it which I want to use in C++.
Is it possible to create a native DLL in Visual Studio 2008 which can be used in C++?
native <-> .Net interop is one of my pet disciplines, which is why I needed this as straightforward and reliable as possible.
The result was that I made me an MSBuild task which I just need to drag into a project to allow me to export static methods from pretty much any .Net language. And since the whole marshalling infrastructure works for exports as well, you can do pretty much anything with it that you want (like passing native/managed objects as IUnknown).
The resulting assembly will look to the consuming process like a real DLL, which means you can't have it to scale up to 64bit automatically anymore.
However, since you do have native bits in your application, you already have this issue anyways. ;-)
When you do not specifiy the CPU target in your C# project, my task will emit a warning that it created a folder for all targets (x86,x64 and Itanium), and there you'll have *.dll and *.pdb for each platform.
If you want the program to be native, and not managed, you'll need to port it to C++, instead of using C#.
That being said, you can compile it in C# into a library, and use it from C++ by using C++/CLI. This just requires that you compile the files that use the C# library with the /clr flag. This provides C++ access to the .NET framework, and lets you use libraries made in C# directly from C++.
Alternatively, you can use .NET's COM interop to expose the C# class(es) as COM objects, and then use those from native C++.
It is possible in Visual Studio 2008, but you're not going to be able to write it using C#.
To create a native DLL, you'll have to write your code using one of the unmanaged C++ project types.
You can expose the DLL to COM. Have a look here for some examples.
yes you can.
you need to create second project.
the project must be unmanaged (like "visual c++"->class library).
the name of this procedure is "calling from unmanaged code to managed code".
good to read unmanaged to managed (codeproject)
you must be aware, that any computer that using your dll must have preinstalled DotNet and Visual C++ Redistributable Package

Categories