> md C:\learncs > cd C:\learncs > C:Now you should create your first C# program, type "notepad hello.cs" and type (in the notepad)
using System; public class Hello { public static void Main() { Console.WriteLine("Hello C# World :-)"); } }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.
> md ..\learncs2 > cd ..\learncs2 > notepad hello.csand then type, in the notepad
using System; public class Echo { string myString; public Echo(string aString) { myString = aString; } public void Tell() { Console.WriteLine(myString); } } public class Hello { public static void Main() { Echo h = new Echo("Hello my 1st C# object !"); h.Tell(); } }Wouah, 25 lines! That's a program! Save it, compile it, run it...
using System; public class Hello { public static void Main() { HelloUtil.Echo h = new HelloUtil.Echo("Hello my 1st C# object !"); h.Tell(); } }echo.cs
using System; namespace HelloUtil { public class Echo { string myString; public Echo(string aString) { myString = aString; } public void Tell() { Console.WriteLine(myString); } } }Note in hello.cs I have used the syntax "HelloUtil.Echo" it's because Echo is in the namespace HelloUtil, you could have typed (at he start of the file) using HelloUtil and avoid HelloUtil., that's the way namespace work.
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!
You could also put your DLL in the global assembly cache (GAC), and any
program would be able to access it, even if the DLL is not in its directory!
to put it in the GAC, I sugest you read MS doc but here are the unexplained step:
using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyTitle("My Lib Title")] [assembly: AssemblyVersion("1.2.3.4")] [assembly: AssemblyKeyFile("myKeyName.snk")]
By the way, did I tell you ? 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 !!!
#include <stdio.h> #define DLLOBJECT __declspec(dllexport) DLLOBJECT void writeln(char* s) { printf("%s\n", s); }echo.cs
using System; using System.Runtime.InteropServices; namespace HelloUtil { public class Echo { [DllImport("echo.native.dll", CallingConvention=CallingConvention.Cdecl)] static extern void writeln(string s); string myString; public Echo(string aString) { myString = aString; } public void Tell() { writeln(myString); } } }hello.cs
using System; using HelloUtil; public class Hello { public static void Main() { Echo h = new Echo("Hello my 1st interop code !"); h.Tell(); } }Hehe, here you discover a completly new thing, Attribute.
And now let's compile this!
> csc /nologo /t:library /out:echo.dll echo.cs > csc /nologo /out:hello.exe /r:echo.dll hello.cs > > rem "if the following line don't work, read bellow.." > gcc -shared -o echo.native.dll echo.c > strip echo.native.dllthe 2 last line (the gcc & strip command) are for building the "C-DLL".
Now I should admit I didn't tell you all the truth. echo.dll and
echo.native.dll are not the same kind of DLL. It's not just the language
(C / C#) the C one is a plain executable full of, probably, x86 instruction, whereas
the C# one is what MS call a portable executable.. anyway they are different.
If you install echo.dll in the GAC it wont work because it won't find
echo.native.dll except if you put in into the PATH (like C:\Windows\System32).
In the same manner when you add the reference in VS.NET echo.native.dll is overlooked
and your program won't work....
So either put the native one in your path or copy it in the debug/release directory of VS.NET.
Or do everything by hand (makefile? build.bat?) and put all your dll in you build
directory, and everything work fine..
Now you are ready to understand, I could tell you all the truth:
csgl.dll is a C# assembly and csgl.native.dll is just
a dll compiled by GCC, upon which csgl.dll depends...
feedback: lloyd@galador.net