Mixing VB.net code with c# code - c#

I have a vb.net solution and I want to add there a new dll files written in c# and use the functionality from the dll, in the code written in vb.net.
I made several uses of it and it seems working all right,
but is it a smart thing to do messing vb.net code with c# like I want to do .
And what a dangers of what I am doing ?
Thank a lot for help .

Your DLL is not a C# DLL, it's a .NET DLL. Once compiled, all you have is IL - doesn't matter what language it came from. Should be no problem, unless you encounter one of the odd edge cases where the DLL's interface includes something that is not supported by Visual Basic. But this would be very much an edge case.
The Common Language Specification, or CLS, defines the subset of .NET features that must be supported by a .NET language, and if your DLL is CLS compliant, then you can use it with no problems. If you are confused about the difference between the CLS, CTS, CLR etc, then I found the coverage of it in this book very helpful, though it is primarily a C# book.

Mark your code as CLS compliant, and then the C# compiler will warn you if you do anything that might cause problems when your DLL is called from another .Net language.
Some quotes from MSDN
To fully interact with other objects
regardless of the language they were
implemented in, objects must expose to
callers only those features that are
common to all the languages they must
interoperate with. For this reason,
the Common Language Specification
(CLS), which is a set of basic
language features needed by many
applications, has been defined.
You can mark assemblies, modules,
types, and members as CLS-compliant using the CLSCompliantAttribute.
Some CLS-compliant language compilers,
such as the C# compiler, enable you to
specify that you intend your code to
be CLS-compliant. These compilers can
check for CLS compliance and let you
know when your code uses functionality
that is not supported by the CLS.
Also, your organisation will now need C# skills as well as Vb.Net skills. You should probably convince yourself that this is OK, and then convince key decision makers.

You can mix VB and C# code in the same project - I have worked on several projects that have mixed them and have no issues.
You language mix seems to be much more isolated - one solution with multiple C# DLLs and vb project(s).
I don't see many issues with that.

One solution was found here:
However, it is possible to use
different languages in a single
project. You may need to write command
line build file to build the project.
In .NET framework SDK, there is one
sample on it. You could access it in
C:\Program Files\Microsoft Visual
Studio
.NET\FrameworkSDK\Samples\Technologies\CrossDevLan
guage.
This sample demonstrates the use
different development languages in a
single project. This sample creates
two assemblies. The first is a library
or DLL assembly that defines a simple
base class written in managed
extensions for C++. The second
assembly is an executable assembly
that defines three derived classes
written in C#, VB, and IL
(Intermediate Language). These types
derive from each other and ultimately
from the base class written in managed
C++. Finally, the executable creates
instances of each of the derived types
and calls a virtual method for each.
The .NET Framework is an environment
where various developers can work
together seamlessly while developing
in their language of choice.
But you can use both VB.NET and C# code inside asp.net application.
You need to create two folders (ex. vbFolder and csFolder) in App_Code folder and write this code in web.config:
<system.web>
<compilation>
<CODESUBDIRECTORIES>
<ADD directoryName="vbFolder" />
<ADD directoryName="csFolder" />
</CODESUBDIRECTORIES>
</compilation>
</system.web>
Good explanation is here.

I think biggest danger is to have a developer to know both languages; while C# and VB.NET are similar because they're bound to .NET framework, they have some peculiarities.
You'll find many good C# programmers and many good VB.NET programmers, but can be a little harder to find a good programmer for both languages
Also, take a look into this article: A Manager’s Retrospective on the C# versus VB.NET decision as it talks about other items to keep in mind, as developer preferences, language features and recruiting.

Both VB.NET & C# are compiled to MSIL (Microsoft Intermediate Language) not native code and this to complete the full compilation to native (machine) on the end user machine via the exist .NET frame work which is on the end user machine so if it was .NET for operating system x your program should work fine for operating system x and if it was operating system y your application should work fine with OS y, and this is the solution which .NET technology comes with to let the .NET applications operating system in-Dependant.
also there is a COM Marshaler service to support old component (controls) to work with .NET applications so for example you can invoke vb6 control (*.ocx) in C# windows application.
and this is great integration between Microsoft technologies and techniques.
and no need to have developer good in both VB.NET and C#, but any way if you need one, I am here :)
but the question is why I am in both?
this just because I deliver training, So I thought to expand my abilities and I was surprised that they both very near except the syntax.

Related

Can C# compiler compile a VB.Net code?

I found following on a ASP.NET book. I am learning and curious about following content.
The second major advantage of an IL architecture is that it enables the Framework to be language neutral. To a large degree, language choice is no longer dictated by the capabilities of none language over another but rather by their preferences of the developer or the tam. You can even mix language in a single applications. A class written in C# can be derived from a VB2005 class, and an exception thrown in a C# method van be caught in a VB#005 method.
My question is , does ASP.NET use same compiler to compile VB.net and C#?
Update: (Another query)
Can C# compiler compile a VB.Net code ?
They have separate compilers (csc.exe for C# and vbc.exe for VB.Net) but they both get compiled into IL and then at run-time JIT compiles it into machine code.
Question 1 : can C# compiler compile VB.net code?
Answer 1: No it can't and you will see that in the below example. It gives an error if you try to do that as it looks for C# syntax.
Question 2: I think that's what they say in the book. Since it is two compilers, I feel it is not compatible
Answer 2: It doesn't say that you can compile VB code using C# but it says that you can mix languages in a single application like I did in the example below and still able to compile C# and VB (using their compilers).
See below example to understand how it works. I created a solution with a C# project with a C# class and a VB project with a VB class. You should not mix C# and VB classes in same project as it will ignore the vb file if its a C# project during build.
Content of ClassCSharp.cs:
namespace ClassLibraryCSharp
{
public abstract class ClassCSharp
{
public int MyProperty { get; set; }
protected abstract void Test();
}
}
Content of ClassVBInCSharp.vb in C# ClassLibrary. See how I can inherit from a C# class and also access its properties and override the method.
Namespace ClassLibraryVB
Public Class ClassVBInCSharp
Inherits ClassCSharp
Property Test2 As Integer
Protected Overrides Sub Test()
Test2 = MyBase.MyProperty
End Sub
End Class
End Namespace
See below commands I ran:
vbc.exe /reference:"ClassLibraryCSharp.dll" /target:library /out:"ClassLibraryCSharpVbVersion.dll" "ClassVBInCSharp.vb"
Microsoft (R) Visual Basic Compiler version 12.0.20806.33440
Copyright (c) Microsoft Corporation. All rights reserved.
See above VB Compiler is used to compile vb class.
csc.exe /reference:"ClassLibraryCSharp.dll" /target:library /out:"ClassLibraryCSharpVersion.dll" "ClassVBInCSharp.vb"
Microsoft (R) Visual C# Compiler version 4.0.30319.33440 for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
ClassLibrary1\ClassVBInCSharp.vb(1,1): error CS
0116: A namespace cannot directly contain members such as fields or methods
See above if I try to use C# Compiler to compile vb class it throws an error as its looking for C# syntax.
ASP.Net is a server-side Web application framework designed for Web development to produce dynamic Web pages.
What you are confused is about the compilation of languages, now, C# and VB.Net has it's own compilers. ASP.Net is a framework that could be achieved using languages like C# and all.
So do not get confused with language compilers. Each language will have their respective compilers that will convert the source code to a Intermediate language (IL) code that the Common Language Runtime (CLR) can understand. So a web framework like ASP.net is a framework that provides a mechanism to process the HTTPRequest and HTTPResponse. Now the language compilers will do their part of work of compiling respective programming language to IL code.
From MSDN:
ASP.NET is a unified Web development model that includes the services
necessary for you to build enterprise-class Web applications with a
minimum of coding. ASP.NET is part of the .NET Framework, and when
coding ASP.NET applications you have access to classes in the .NET
Framework. You can code your applications in any language compatible
with the common language runtime (CLR), including Microsoft Visual
Basic and C#. These languages enable you to develop ASP.NET
applications that benefit from the common language runtime, type
safety, inheritance, and so on.
Update:
Now answering the second doubt that you posted in the commenting section of your question. (Can C# compiler compile VB code)
Actually you cannot. Let us consider C# compiler, what any compiler like C# or VB know's is that you have certain code written in front of you and you need to understand the code and provide an equivalent IL code in this case. This means, a C# compiler has to work with code that has syntax of C# and a VB compiler has to work with code that has syntax of VB. You see a compiler basically needs to know what it is compiling (processing). Now consider some language X has a X compiler, now the compiler will be written in order to just crack and decode the language X.
Now, this does not stop you from writing VB code that talks to C# code or vice versa. That interoperability. See more.
C# and VB.Net are different languages. They have different programs (executables) which are the actual compilers. These compilers compile the high-level code to IL which is what the CLR understands and executes.
The compiler program can either generate an exe or a dll. For asp.net applications the program makes a dll always.
The below link would help you get familiar with the c# compiler command line program:
http://msdn.microsoft.com/en-us/library/2fdbz5xd.aspx
Update:
Q. Can a C# compiler compile vb code?
The answer is: No.
However, for an asp.net website there is a possiblity of having both c# and vb code in a single project. Take a look at the following link to know how that is done:
http://timheuer.com/blog/archive/2007/02/28/14002.aspx
Update:
why it says "multiple code files only works inherently (along with codeSubDirectories)
with the web site model" ?
That was the intent of the website model projects: http://msdn.microsoft.com/en-us/library/dd547590(v=vs.110).aspx#scenarios
There is a compiler for C# (CSC.Exe) and one for VB.NET (Vbc.exe). These compilers do not generate machine code but compile the respective language to intermediate language (IL). When executing the program (or running the website) on a computer, IL is interpreted and converted into machine code that is executed on the processor.
The compilers that ASP.NET uses are the same that are also used for other .NET programs, e.g. Windows applications.

Decompling .net assembly

Hey i have done a few of decompiling in .net as i am learning c# so it helps me to see codes as it helps a lot. But lately i have come acrossed few program that i know are .net but in reflector show up as non .net assemblies. Here is the example of program named: Proxy Multiply.
I am not trying to do any illegal stuff or something. Just trying to learn. I have tried to google this but i was not able to achieve any good result.
Thanks
here is the link to image.
There are many .Net code protection alternative, that obfuscate the IL codes so that they are not that much exposed to IL disassembler application.
.Net Reactor
Themida
SmartAssembly
the list is huge . . .
many of the protector modify the Exe (PE Header info), .Net exe contains some extra MetaData that helps disassembler to identify it.
Download this little application it may tell you a little more about the exe.
Download PEiD 0.95
PEiD is an intuitive application that relies on its user-friendly
interface to detect packers, cryptors and compilers found in PE
executable files – its detection rate is higher than that of other
similar tools since the app packs more than 600 different signatures
in PE files.
PEiD comes with three different scanning methods, each suitable for a
distinct purpose. The Normal one scans the user-specified PE file at
its Entry Point for all its included signatures. The so-called Deep
Mode comes with increased detection ratio since it scans the file's
Entry Point containing section, whereas the Hardcore mode scans the
entire file for all the documented signatures.
My best guess the assembly you are looking for is Protected by .Net Reactor or Themida
I have same problem with dot net reflector before,
try JetBrains dotPeek version 1.0 Decompling(this application will show code that obfuscated)
Decompiling .NET 1.0-4.5 assemblies to C#
Support for .dll, .exe, .zip, .vsix, .nupkg, and .winmd files
Quick jump to a type, assembly, symbol, or type member
Effortless navigation to symbol declarations,
implementations, derived and base symbols, and more
Accurate search for symbol usages
with advanced presentation of search results
Overview of inheritance chains
Support for downloading code from source servers
Syntax highlighting
Complete keyboard support
dotPeek is free!
Just because it is .NET doesn't mean that you can just decompile it like that. They probably used ILMerge. That's not to say it's impossible but it will require more work.
See Is it possible to “decompile” a Windows .exe? Or at least view the Assembly?

C# (MSIL) into native X86 code?

I would first like to say my goal is to convert MSIL into native X86 code. I am fine with my assembly's still needing the .net framework installed. NGEN is not what I want as you still need the original assembly's.
I came across ilasm, and what I am wondering is this what I want, will this make pure assembly code?
I have looked at other projects like mono (which does not support some of the key features my app uses) and .net linkers but they simple just make a single EXE with the .net framework which is not what I am looking for.
So far any research has come up with...you can't do it. I am really no sure as to why as the JIT does it when it loads the MSIL assembly. I have my own reasons for wanting this, so I guess my question(s) come down to this.
Is the link I posted helpful in anyway?
Is there anything out there that can turn MSIL into x86 assembly?
There are various third-party code-protection packages available that hide the IL by encrypting it and packing it with a special bootloader that only unpacks it during runtime. This might be an option if you're concerned about disassembly of your code, though most of these third-party packages are also already cracked (somewhat unavoidable, unfortunately.) Simple obfuscation may ultimately be just as effective, assuming this is your underlying goal.
One the major challenges associated with 'pre-jitting' the IL is that you end up including fixed address references in the native code. These in turn will need to be 're-based' when the native code is loaded for execution under the CLR. This means you need more than just the logic that gets compiled; you also need all of the reference context information necessary to rebase the fixed references when the code is loaded. It's a lot more than just caching code.
As with most things, the first question should be why instead of how. I assume you have a specific goal in mind, if you want to generate native code yourself (also, why x86? Why not x64 too?). This is the job of the JIT compiler - to compile an optimized instruction set on a particular platform only when needed, and execute it later.
The best source I can recommend to try and understand how the CLR works and how JIT works is taking a look at SSCLI - an implementation of the CLR based on the ECMA-335 spec.
Have you considered not using C#? Given that the output of the C# compiler is MSIL, it would make sense to develop on a different platform if that is not what you want.
Alternatively it sounds like NGEN does the operation you are wanting, it just doesn't handle putting the entire thing into an executable. You could analyze the resultant NGEN image to determine what needs to be done to accomplish that (note that NGENed images are PE files per the documentation)
Here is a link on NGEN that contains information on where the images are stored: C:\windows\assembly\NativeImages_CLR_Bit for instance C:\windows\assembly\NativeImages_v2.0.50727_86. Note that .NET 3.0 and 3.5 are both part of 2.0.

How is it that the Mono sources are mostly C#?

I just looked at the source of Mono for the first time and I thought I would find a bunch of C or C++ code, instead I found 26,192 .cs files and 7 .cpp files.
I am not totally shocked but it made me think of a quesiton I've always had in the back of my mind:
How does a project end up being written in "itself" like this?
Was an older version of mono more c/c++? Or was there initial effort to create some kind of machine coded compiler...
What's the "trick" here?
Mono's compiler is written in C#. You may want to read about compiler bootstrapping.
You should be looking for .c files, instead of .cpp files: the mono runtime is written in C, not C++.
I think it is also important to remember that mono is both a virtual machine runtime (the JIT compiler, garbage collector, etc.) as well as a collection of class libraries that run on this framework (the System.Linq namespace, the XML parsers, etc.).
The majority of the .cs files you see are part of the class libraries. These are basically C# code that run like your own C# code (with some exceptions, but basically it doesn't make sense for everyone to reinvent and re-distribute the wheel over and over, so these are the C# "base" class libraries). This is why you can download complex mono programs as such small file sizes if mono is already installed on the machine.
For mono, the JIT, runtime and garbage collector are largely written in C/C++ as you would expect. If you ever get a low level error, you will often see GNU debug tool dumps as you would in C, just with lots more useful information. The Mono framework is very good at taking any C# code and converting it to CIL code that can run anywhere, and they use whatever toolset is best suited to ensure the code does run anywhere (which in this case meant a C compiler runtime on linux).

Can you mix .net languages within a single project?

Can you mix .net languages within a single project? So pre-compiled, I would like to call classes and methods of other source files.
For both web and apps?
In particular I'd be interested in F# and C#.
You can mix languages in a single assembly with ILMerge and MSBuild.
Here is a very good example about it.
Yes, you can, but Visual Studio does not support it directly. What you will do is compile code to netmodules, then combine them into a single assembly. The compilers support the "/target:module" option which generates these netmodules.
You can then use the compilers to reference other netmodules when building, or use Assembly Linker (Al.exe). There's even an msbuild task for it: AL (Assembly Linker) Task.
A full overview is provided on MSDN: How to: Build a Multifile Assembly
CMS mentions an interesting approach, but in reality I would suggest you keep things simple and have different assemblies (projects) for the C# and F# code. There are well documented communication points between C# and F# (such as here and here) - I'd recommend them instead. The differences between C# and F# (especially with F# having a different runtime!) are quite large...
you can specify the language in each assembly project (library DLL) and use several of these in the same solution, but i don't think you can mix languages within the same assembly
You can do it in a web site project versus a compile-first project: http://www.aspnetlibrary.com/articledetails.aspx?article=Use-C-Sharp-and-VB.NET-in-the-same-project. A web site's BuildProvider resolves language elements on-the-fly.
.NET's BuilderProvider still isn't available for non-web site projects so you're out of luck for standard app mixed Intellisense.

Categories