FREN

#FF00AA


6 jul. 2004

GarooRSS

Watch out for the technical point. It’s not even about the web, but Windows programming, so you can just skip it. Besides, considering my current sleeping cycles, I’m dead now, so this post will be particularly bad. And what I’m writing about isn’t even new anymore. That’s how interesting it is.

So, if you’re nosy, you can still click the icon and download the program, which isn’t of much use — it’s just a technical demo — but, well, I like to brag with my software. (Hoping it’ll work on other computers than mine. And, of course, since that’s the post’s topic, you need the .NET runtime.)

If you’re using Windows, and you like to play with new software once in a while, you certainly have already encountered some freeware thatrequired the .NET runtime. If you’ve got broadband, it isn’t much: you follow the link, click where you have to, or launch Windows Update, and it gets installed, and you trust it because it’s on the Microsoft website (mwahaha), and so there. But if you’re on dialup, and those still exists, it’s harder to considering downloading 20MB or Microsoft crap in order to get a 100KB shareware to load.

Because .NET has to be crap, hasn’t it? For starters, it has a ridiculous name, that makes you think it only has a few web connection and XML processing functions. And it’s very reminiscent of those Visual Basic DLLs that were way too big for their time (when many people were still on dialup) and existed in fifteen different versions. Basically, something for lazy, incompetent programmers, who can’t code for themselves. Right? Nope.

I got curious yesterday, or maybe I got bored, but I think both go together, and I finally downloaded the SDK, read the documentation, checked out what this thing has to bring, and I came to this unsettling conclusion: it’s good! (Oops, so much for suspense.) For context, I have to say that, when I went back to Windows programming two and a half years ago in order to make Ghrone, and I discovered Visual C++, I had been terribly disappointed: the MFCs (the object-oriented interface that allowed you to program for Windows in C++), now that was crap. Of course, maybe I expected too much from those, because two years earlier I had gotten into BeOS programming (object oriented from the start, incredibly simple to program for). But I’m just certain that at the time I was using Borland C++ (that’s in 1995-96, when I made a nice NeXTstep interface clone, at a time when cloning OSs wasn’t the big rage yet) classes were much better designed, code wasn’t as messy, and everything was much more simple and convenient.

I won’t get into (more) detail, because either you’re not interested or you already know about that: to synthesize, I’ll say that, between my past disappointment in Visual C++ and .NET’s ridiculous name, plus all the noise Microsoft had made around it for years before laying low with all commenters saying it was much ado about nothing, I really didn’t expect much.

Surprise! .NET is precisely everything the MFCs should have been for years: a real object-oriented layer, that finally masks all system calls and allows you to access the Windows system in a way that makes sense, and automatically manages all those little obvious things that required lots of silly code with the MFCs.

I decided to get into it when I found out (reading the MSDN blogs, and it’s a good thing I use an aggregator now, because otherwise they’re really not digest) that Microsoft offered a free Visual C++ compiler. In case you wanted to try it out, here’s a little spoiler: forget it. Programming for .NET in C++ is awful, because they’re visibly trying to respect the language’s standards and you have to type lots of macros and weird, heavy definitions, ending up with horrid code. Not to mention the fact that the documentation offers very scarce C++ samples — and that one is as much a cause as a consequence. Microsoft created C# specially for .NET (but Visual Basic also works, it’s just exactly the same language without the curly braces) and, contrary to what you may believe before you get into it, they didn’t just do it in order to own the place, it really has a point.

The big particularity of .NET as a programming environment, and the main reason why it’s unusable in C++, is managed memory: the .NET runtime includes a garbage collector (I think Java does too, right?). Now that I know how it works (I had never really looked into it, so it remained just some kind of magic to me), I’m not able to, not explain it to you, but confirm that it’s really important: you used to spend hours in the Windows documentation, trying to figure out when and how to allocate a string or a structure, and when and how to discard it. It was the same for everybody, not that I’m stupid, but just the APIs weren’t always quite coherent in this regard. The funny thing is, it seems to me that Borland C++, eight years ago, and without resorting to a garbage collector, managed to get rid of much of that trouble simply by shielding the programmer from the evil Windows APIs. But not Microsoft, no, they had to reinvent their OS and their programming language. Bah.

Anyway, this is quite nice, but it’s not the most spectacular. Being rid of pointers is a welcome rest, but the most impressive thing in .NET (and what’s most interesting to me) that GUI programming is now as simple as it can be. A few examples:

Button button1 = new Button();
button1.Click += new System.EventHandler(button1 Click);
MainWindow.Controls.Add(button1);

There you go: the button is created and attached to the window, and clicks are processed, all in three lines. No constants to define so you can create events, no classes to inherit — you can perfectly program a small application without even having to create a class for the main window!

ListBox listBox1 = new ListBox();
listBox1.Size = new System.Drawing.Size(120, 173);
listBox1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
listBox1.Items.All = new object[] {“Item One”, “Item Two”, “Item Three”, “Item Four”};

Besides the list being filled in just one line, there’s something amazing in this snippet: thanks to Anchor, this lists will move and resize automatically when the user changes the window’s size. It’s apalling that we had to wait until .NET for this, but I’m so glad I don’t feel like complaining.

ContextMenu button1ContextMenu = new ContextMenu();
button1.ContextMenu = button1ContextMenu;

And now you have a context menu attached to a button (or to the main window, or to anything) in just two lines. And, of course, I edited some stuff out, but you can fill it up and define the event handling functions in just a few lines too. Crystal clear.

It’s now time to conclude this publicity operation (it’s all for free — you don’t think I’d accept money from Microsoft, do you?) with my little program of mine : in just a few hours of programming, in just a few pages of code, a program that displays an image (uh, yeah… but, seriously, it was so complicated before — particularly as it’s a JPEG, and at the time I made Ghrone I had to find a third-party library in order to read something other than BMP), downloads the blog’s RSS feed, displays it in an embedded Explorer, and opens the browser when you double-click a post.

Total time: eight hours. Including two hours trying to get a C++ .NET file to compile (I told you, forget it), one hour looking for the C# compiler that is indeed included for free with .NET (don’t know why it’s in the Windows directory instead of with the SDK), another hour to figure out how to include a damn icon in the program’s resources (I always had lots of trouble with Windows resources, don’t know why, I really find it poorly designed — and now it’s obviously worse when you work in command-line mode), one hour to launch the system browser when you double-click the list (I don’t know if it’s .NET, Firefox, or the anti-monopoly Windows Service Pack, but now you can’t just execute the URL, you have to get the browser’s location yourself in the registry) and one hour for the ActiveX control to launch the default browser too when you click a link.

Which means, in just two hours I could create my very first .NET C# program, displaying a picture at the top, managing a listbox, integrating a mini-browser, and processing an RSS feed. Two hours, and I was just discovering .NET with a far from optimal documentation (and on that level too, it seems to me that Borland documentation was better at the time).

As a result, I just have to get back to programming. Have to. I talked about it recently, but now I know it’s really possible: I cant to make an Exposé + Dashboard + Dock clone. Can be done. I only have to stay motivated long enough.

Want to know when I post new content to my blog? It's a simple as registering for free to an RSS aggregator (Feedly, NewsBlur, Inoreader, …) and adding www.ff00aa.com to your feeds (or www.garoo.net if you want to subscribe to all my topics). We don't need newsletters, and we don't need Twitter; RSS still exists.

cossaw, 7 years ago:

L'orientation objet pour ls trucs "graphiques", c'et très bien, joli itou.
Mais dès que tu essayes d'optimiser des calculs sous windows (parce que ces imbéciles de clients (internes en plus) ne connaissent que windows, c'est là qu'on est bien embêté (euphémisme) parce qu'on ne sait pas comment le système gère la mémoire...
Nous faisons beaucoup de calculs (méthodes de Monté-Carlo, d'arborescence des EDP, schéma de différences et éléments finis), et très souvent vectoriel. Qu'est-ce qui est sous-jacent à une structure vectorielle quelconque ? les pointeurs, nos amis qu'on aurait bien voulu abandonner ; ils sont de retour, et ils vont faire mal. très, très mal.
En fait, on a porté suos windows nos codes qu'on a développé sous linux et compilé/optimisé sous compilateur g++. Et là, tuot se casse la gueule parce que le système de gestion est différent, on a des pertes, des permanances (l'inverse de la perte : un vecteur qu ne veut plus se détruire sou sprétexte qu'il est owned par un autre truc mais tu ne sais plus lequel)...
L'horreur.
Je ne dis pas que le problème viens de MS, mais c'est pas méga clair pour nous dans .Net (rien que les options de compilations qu'on ne comprenais plus depuis les versions antérieures de VC++).
Je crois que le problème, c'est que nous sommes des mathématiciens "appliqués" et pas des informaticiens chevonés.

Alors, si l'aspect "interface graphique" peut en soi être vite régler, tant mieux.. on pourra se consacrer aux choses importantes.

Ps : si tu as moins de 30 ans, la nationalité française ou d'un membre de l'union européenne ; si tu habites en région parisienne ; si tu aimes la programmation et les maths, si tu as de l'expérience et que tu n'es pas un opposant des marchés financiers alors j'ai un poste pour toi...

garoo, 7 years ago:

Euh, non, mais merci :)

Je n'ai pas regardé en détail parce que ça ne me concerne pas, mais il y a des pages et des pages de doc sur comment faire cohabiter du .Net et du code pas-managé.

Et puis .Net et le C# ne sont pas obligatoires :)

A. L., 7 years ago:

Bon je peux pas l'essayer, mais : bouuuh même pas les sources.

hugolin, 7 years ago:

Le "contrôle de l'interface utilisateur" aussi a été calqué sur Java. .Net n'a rien inventé de si génial que ça.

Ruxor, 7 years ago:

Je crois que beaucoup de gens sont d'accord que .NET n'a rien de fondamentalement nouveau par rapport à Java (enfin, la JVM) mais que c'est quand même mieux fait. Notamment, il est plus facile de compiler d'autres langages de programmation que C# en .NET que que d'autres langages que Java sur la JVM. Par exemple, il doit y avoir des implémentations de Haskell ou OCaml qui compilent en .NET, et ça c'est vraiment bien.

Il y a quelques tentatives pour produire des implémentations libres de .NET (ou d'extensions de .NET), par exemple dot-GNU qui a l'air rigolo. Ceci dit, quand je vois que depuis le temps on n'a toujours pas une seule JVM libre digne de ce nom je me demande ce que ça va donner pour .NET...

garoo, 7 years ago:

Mono 1.0 ?

(Je ne sais pas ce que ça vaut, je suis juste tombé dessus quand je cherchais un compilateur C#, avant de finalement trouver celui intégré dans le SDK .NET.)

Gérard, 7 years ago:

J'attend le mois de Juillet des Pédésblogueurs avec impatience et tu nous fais un blog de spécialistes informaticiens. JandB a une belle foufoune sur son nombril et il est très mignon, mais j'ai envie de changer de fond d'écran.

Legal information: This blog is hosted par OVH, 2 rue Kellermann, 59100 Roubaix, France, www.ovhcloud.com.

Personal data about this blog's readers are not used nor transmitted to third-parties. Comment authors can request their deletion by e-mail.

All contents © the author or quoted under fair use.