Sponsored Ad

Friday, November 27, 2009

C# First tutorial

First open a DOS command shell.

You should begin to work in an empty directory for this. let call it "C:\learncs". Type in the shell:

> md C:\learncs

> cd C:\learncs

> C:

Now you should create your first C# program, type "notepad hello.cs" and type (in the notepad)

Code Snippet
  1. using System;  
  2.     public class Hello
  3.     {
  4.         public static void Main()
  5.         {
  6.             Console.WriteLine("Hello C# World :-)");
  7.         }
  8.     }

the using keyword just let you write Console at line 7, instead of System.Console. It's very usefull shortcut when you use a lot of "class" define in System.

Save the file.

Now you could compile. Type in the DOS Shell again and type:

csc /nologo /out:hello.exe hello.cs

Second tutorial

Now you become to be pretty confident, I guess, so we could start using multiple file, and even a dll ? go into an other directory (or stay in this one, I won't mind) and create 2 file:

hello.cs

Code Snippet
  1.  
  2.     using System;    
  3.     public class Hello
  4.     {
  5.         public static void Main()
  6.         {
  7.             HelloUtil.Echo h = new HelloUtil.Echo("Hello my 1st C# object !");
  8.             h.Tell();
  9.         }
  10.     }   
  11. echo.cs
  12.     using System;    
  13.     namespace HelloUtil
  14.     {
  15.         public class Echo
  16.         {
  17.             string myString;            
  18.             public Echo(string aString)
  19.             {
  20.                 myString = aString;
  21.             }            
  22.             public void Tell()
  23.             {
  24.                 Console.WriteLine(myString);
  25.             }
  26.         }
  27.     }

Note in the syntax I used hello.cs "HelloUtil.Echo" is because it is in the Echo HelloUtil namespace, could have been written (in which the start of the file) using HelloUtil and avoid HelloUtil., This is the workspace thus.

Now you could compile both in one .exe with

> csc /nologo /out:hello.exe *.cs

But it's not my intention, no.

Well.

(Have you tried?)

Let's go building a DLL:

> csc /nologo /t:library /out:echo.dll echo.cs

that's it (dir will confirm).

Now we could use it ...

> csc /out:hello.exe /r:echo.dll hello.cs

if you typed "hello" it will worked as usual..., but if you delete "echo.dll" the program will now crash: it use the DLL. You could also change Echo.cs, rebuild the DLL and see... that's the advantage of DLL!

Alternatively, put the DLL in the global assembly cache (GAC), and any program would be able to access it, even if the DLL is not in your directory!

to put it in the GAC, I suggest you read doc of MS, but here are the step without explanation:

# Create a key Assembly to create once and use for each version. created by:

sn-k myKeyName.snk

the. snk must be in the build directory (in which the execution CSC)

# Make a title added in any asssembly strong. Cs source file the following directive at the highest level:

Code Snippet
  1. using System.Reflection;    
  2.     using System.Runtime.CompilerServices;
  3.     [assembly: AssemblyTitle("My Lib Title")]
  4.     [assembly: AssemblyVersion("1.2.3.4")]
  5.     [assembly: AssemblyKeyFile("myKeyName.snk")]
  6. # now add it to the GAC:
  7.   > gacutil.exe /if myLib.dll

when I referenced the hello.dll while compiling, remember? csc /out:hello.exe /r:echo.dll hello.cs, it could have been any assembly, even a .exe !!!

Fourth tutorial

Greeting soon to be able to hack CsGL but there is one last step you should understand: interoperability (with code C).

You will need a C compiler, I advise for Windows called MinGW gcc is free, is good is GCC?

We will create 3 files:

Code Snippet
  1. echo.c
  2.     #include <stdio.h>    
  3.     #define DLLOBJECT __declspec(dllexport)    
  4.     DLLOBJECT void writeln(char* s)
  5.     {
  6.         printf("%s\n", s);
  7.     }

 

 

Code Snippet
  1. echo.cs
  2.     using System;
  3.     using System.Runtime.InteropServices;    
  4.     namespace HelloUtil
  5.     {
  6.         public class Echo
  7.         {
  8.             [DllImport("echo.native.dll", CallingConvention=CallingConvention.Cdecl)]
  9.             static extern void writeln(string s);            
  10.             string myString;            
  11.             public Echo(string aString)
  12.             {
  13.                 myString = aString;
  14.             }
  15.             
  16.             public void Tell()
  17.             {
  18.                 writeln(myString);
  19.             }
  20.         }
  21.     }
  22.     hello.cs
  23.     using System;
  24.     using HelloUtil;
  25.     
  26.     public class Hello
  27.     {
  28.         public static void Main()
  29.         {
  30.             Echo h = new Echo("Hello my 1st interop code !");
  31.             h.Tell();
  32.         }
  33.     }

 

Here you find something completely new attributes.

[DllImport (.. "is an attribute.

You could label any method / field / class with any number of attributes.

Generate additional information that could be used by anyone who could understand.

The DllImport attribute is understood by the compiler and said that the function below is actually the name of a DLL, which is "echo.native.dll. I add a parameter of the calling convention as the default. NET calling convention is __stdcall that in C, is __cdecl.

By the way, if you look in documentation DllImport, DllImportAttribute search, because it removes the "attribute" of attribute class name to use, it does.

And now let's compile this!

 

Code Snippet
  1. > csc /nologo /t:library /out:echo.dll echo.cs
  2.     > csc /nologo /out:hello.exe /r:echo.dll hello.cs
  3.     >
  4.     > rem "if the following line don't work, read bellow.."
  5.     > gcc -shared -o echo.native.dll echo.c
  6.     > strip echo.native.dll

the 2 last line (the gcc & strip command) are for building the "C-DLL".

If they don't work maybe gcc is not in a directory listed in your path environment variable ? check with:

%lt; echo %PATH%

Well it's probably not,anyway, so type, assumin mingc is in C:\MinGW:

set PATH=C:\MinGW;%PATH%

And try again... you sure it's not a syntax error ?

If it compile test it now: hello Great isn't it ?

Now I must admit I did not tell the whole truth. echo.dll and echo.native.dll are not the same type of DLL. It's not just the language (C / C #), C is a single executable filled probably x86 instruction, while the C # one is what MS call a portable executable .. are different anyway.

If you install in the GAC is echo.dll wont work because it is not echo.native.dll except if put in the path (like C: \ Windows \ System32).

Likewise when you add the reference in VS.NET echo.native.dll overlooked and your program does not work .

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers