tags:

views:

1991

answers:

22

Does it make sense, having all of the C#-managed-bliss, to go back to Petzold's Programming Windows and try to produce code w/ pure WinAPI?

What can be learn from it? Isn't it just too outdated to be useful?

+7  A: 

Absolutely. When nobody knows the low level, who will update and write the high level languages? Also, when you understand the low level stuff, you can write more efficient code in a higher level language, and also debug more efficiently.

Ed
+2  A: 

Learning C or a lower level language can definitely be useful. However, I don't see any obvious advantage in using the unmanaged WinAPI.

Greg
+1  A: 

I've seen low level Windows API code... it ain't pretty... I wish I could unlearn it. I think it benefits to learn low level as in C, as you gain a better understanding of the hardware architecture and how all that stuff works. Learning old Windows API... I think that stuff can be left to the people at Microsoft who may need to learn it to build higher level languages and API... they built it, let them suffer with it ;-)

However, if you happen to find a situation where you feel you just can't do what you need to do in a higher level language (few and far between), then perhaps start the dangerous dive into that world.

Mike Stone
+9  A: 

The native APIs are the "real" operating system APIs. The .NET library is (with few exceptions) nothing more than a fancy wrapper around them. So yes, I'd say that anybody who can understand .NET with all its complexity, can understand relatively mundane things like talking to the API without the benefit of a middle-man.

Just try to do DLL Injection from managed code. It can't be done. You will be forced to write native code for this, for windowing tweaks, for real subclassing, and a dozen other things.

So yes: you should (must) know both.

Edit: even if you plan to use P/Invoke.

James D
+37  A: 

This question is bordering on religious :) But I'll give my thoughts anyway.

I do see value in learing the Win32 API. Most, if not all, GUI libraries (managed or unmanaged) result in calls to the Win32 API. Even the most thorough libraries don't cover 100% of the API, and hence there are always gaps which need to be plugged by direct API calls or P/invoking. Some of the names of the wrappers around the API calls have similar names to the underlying API calls, but those names aren't exactly self-documenting. So understanding the underlying API, and the terminology used therein, will aid in understanding the wrapper APIs and what they actually do.

Plus, if you understand the nature of the underlying APIs that are used by frameworks, then you will make better choices with regards to which library functionality you should use in a given scenario.

Cheers!

OJ
+4  A: 

Analogy: If you build cars for a living (programming), then its very pertinent to know how the engine works (Win32).

Joel Lucsy
+5  A: 

Simple answer, YES.

Stephen Cox
+2  A: 

Learning a new programming language or technology is for one of three reasons:
1. Need: you're starting a project for building a web application and you don't know anything about ASP.NET
2. Enthusiasm: you're very excited about ASP.NET MVC. why not try that?
3. Free time: but who has that anyway.

The best reason to learn something new is Need. If you need to do something that the .NET framework can't do (like performance for example) then WinAPI is your solution. Until then we keep ourself busy with learning about .NET

Amr
A: 

It's important to know what is available with the Windows API. I don't think you need to crank out code with it, but you should know how it works. The .NET Framework contains a lot of functionality, but it doesn't provide managed code equivalents for the entire Windows API. Sometimes you have to get a bit closer to the metal, and knowing what's down there and how it behaves will give you a better understanding of how to use it.

Chris Miller
+1  A: 

Ok I am convinced that it is important to learn low level WinAPI programming. How about some links to WinAPI references, maybe some good tutorials???

marked
+2  A: 

For most needs on the desktop you wont need to know the Win32, however there is a LOT of Win32 not in .NET, but it is in the outlaying stuff that may end up being less than 1% of your application.

USB support, HID support, Windows Media Foundation just off the top of my head. There are many cool Vista API's only available from Win32.

You will do yourself a large favor by learning how to do interop with a Win32 API, if you do desktop programing, because when you do need to call Win32, and you will, you won't spend weeks scratching your head.

Brian Leahy
+7  A: 

On the assumption that you're building apps targeted at Windows:

  • it can sure be informative to understand lower levels of the system - how they work, how your code interacts with them (even if only indirectly), and where you have additional options that aren't available in the higher-level abstractions
  • there are times when your code might not be as efficient, high-performance or precise enough for your requirements
  • However, in more and more cases, folks like us (who never learned "unmanaged coding") will be able to pull off the programming we're trying to do without "learning" Win32.
  • Further, there's plenty of sites that provide working samples, code fragments and even fully-functional source code that you can "leverage" (borrow, plagiarize - but check that you're complying with any re-use license or copyright!) to fill in any gaps that aren't handled by the .NET framework class libraries (or the libraries that you can download or license).
  • If you can pull off the feats you need without messing around in Win32, and you're doing a good job of developing well-formed, readable managed code, then I'd say mastering .NET would be a better choice than spreading yourself thin over two very different environments.
  • If you frequently need to leverage those features of Windows that haven't received good Framework class library coverage, then by all means, learn the skills you need.
  • I've personally spent far too much time worrying about the "other areas" of coding that I'm supposed to understand to produce "good programs", but there's plenty of masochists out there that think everyone's needs and desires are like their own. Misery loves company. :)

On the assumption that you're building apps for the "Web 2.0" world, or that would be just as useful/beneficial to *NIX & MacOS users:

  • Stick with languages and compilers that target as many cross-platform environments as possible.
  • pure .NET in Visual Studio is better than Win32 obviously, but developing against the MONO libraries, perhaps using the Sharp Develop IDE, is probably an even better approach.
  • you could also spend your time learning Java, and those skills would transfer very well to C# programming (plus the Java code would theoretically run on any platform with the matching JRE). I've heard it said that Java is more like "write once, debug everywhere", but that's probably as true as (or even moreso than) C#.
ParanoidMike
+2  A: 

Personally I don't really like the Win32 API but there's value in learning it as the API will allow more control and efficiency using the GUI than a language like Visual Basic, and I believe that if you're going to make a living writing software you should know the API even if you don't use it directly. This is for reasons similar to the reasons it's good to learn C, like how a strcpy takes more time than copying an integer, or why you should use pointers to arrays as function parameters instead of arrays by value.

da_code_monkey
A: 

This is really the same as the question, should I learn a low level language like C (or even assembler).

Coding in it is certainly slower (though of course the result is much faster), but its true advantage is you gain an insight into what is happening at close to the system level, rather than than just understanding someone else's metaphor for what is going on.

It can also be better when things won't work well, or fast enough or with the sort of granularity that you need. (And do at least some subclassing and superclassing.)

David L Morris
+7  A: 

I kept to standard C/C++ for years before learning Win32 API, and to be quite blunt, the "learning Win32 API" part is not the best technical experience of my life.

In one hand Win32 API is quite cool. It's like an extension of the C standard API (who needs fopen when you can have CreateFile. But I guess UNIX/Linux/WhateverOS have the same gizmo functions. Anyway, in Unix/Linux, they have the "Everything is a file". In Windows, they have the "Everything is a... Window" (no kidding! See CreateWindow!).

In the other hand, this is a legacy API. You will be dealing with raw C, and raw C madness.

  • Like telling one's structure its own size to pass through a void * pointer to some Win32 function.
  • Messaging can be quite confusing, too: Mixing C++ objects with Win32 windows lead to very interesting examples of Chicken or Egg problem (funny moments when you write a kind of delete this ; is a class method).
  • Having to subclass a WinProc when you're more familiar with object inheritance is head-splitting and less than optimal.
  • And of course, there is the joy of "Why in this fracking world they did this thing this way ??" moments when you strike your keyboard with your head once too many and get back home with keys engraved in your forehead, just because someone though it more logical to write to API to enable the changing of the color of a "Window", not by changing one of its properties, but by asking it to its parent window.
  • etc.

In the last hand (three hands ???), consider that some people working with legacy APIs are themselves using legacy code styling. The moment you hear "const is for dummies" or "I don't use namespaces because they decrease the runtime speed", or the even better "Hey, who needs C++? I code In object-oriented C" (No kidding... and this was quite a sight...), you'll feel the kind of dread only condemned feel in front of the guillotine.

So... All in all, it's an interesting experience.

Edit

After re-reading this post, I see it could be seen as overly negative. It is not.

It is sometimes interesting (as well as frustrating) to know how the things work under the hood. You'll understand that, despite enormous (impossible?) constraints, the Win32 API team did wonderful work to be sure everything, from you "olde Win16 program" to your "last Win64 over-the-top application", can work together, in the past, now, and in the future.

The question is: Do you really want to?

Because spending weeks to do things that could be done (and done better) in other more high-level and/or object-oriented API can be quite de-motivational (real life experience: 3 weeks for Win API, against 4 hours in three other languages and/or libraries).

Anyway, you'll find Raymond Chen's Blog very interesting because of his insider's view on both Win API and its evolution through the years:

http://blogs.msdn.com/oldnewthing

paercebal
You don't need to use C++ to use win32api. You can use Python or something that doesn't require 15 lines of memory management crap before you can do a simple task.
jle
@jle : Yes, but using the Win32 API through Python is like using the Win32 API through Java or C#: Someone already wrote the wrappers for you. Furthermore, I hope you're not coding GUI with Win32, because even with python, this is a pain. Last, but not least, the question has no mention of Python (or whatever script language), nor the "python" tag, so...
paercebal
A: 

I'll put it this way. I don't like programming to the Win32 API. It can be a pain compared to managed code. BUT, I'm glad I know it because I can write programs that otherwise I wouldn't be able to. I can write programs that other people can't. Plus it gives you more insight into what your managed code is doing behind the scenes.

Slapout
A: 

The amount of value you get out of learning the Win32 API, (aside from the sorts of general insights you get from learning about how the nuts and bolts of the machine fit together) depends on what you're trying to achieve. A lot of the Win32 API has been wrapped nicely in .NET library classes, but not all of it. If for instance you're looking to do some serious audio programming, that portion of the Win32 API would be an excellent subject of study because only the most basic of operations are available from .NET classes. Last I checked even the managed DirectX DirectSound library was awful.


At the risk of shameless self-promotion....

I just came across a situation where the Win32 API was my only option. I want to have different tooltips on each item in a listbox. I wrote up how I did it on this question.

Michael Lang
A: 

yes. take a look at uTorrent, an amazing piece of software efficiency. Half of it's small size is due to the fact that much of it's core components were re-written to not use gargatuian libraries.

Much of this couldn't be done without understanding how these libraries interface with the lower level API's

Ape-inago
A: 

Even in very very high level languages you still make use of the API. Why? Well not every aspect of the API has been replicated by the various libraries, frameworks, etc. You need to learn the API for as long as you will need the API to accomplish what you are trying to do. (And no longer.)

Oorang
A: 

If you planning to develop a cross platform application, If you use win32, then your application could easily run on linux through WINE. This results in a highly maintainable application. This is one of the advantages of learning win32.

Manohar
If you intend to develop a cross platform application and consider Win32 you are not only shooting yourself in your foot. You are blowing your entire leg off. WINE is most of the time excellent at running Windows Applications but that does not make Win32 a good choice for cross platform development.Also, since there is Mono I'd suspect C#/.NET would actually be a much better choice for cross platform issues.
kigurai
How do you debug a .net app in linux? If you use win32, you can use tools such as "ddd" under linux.
Manohar
Yes I did not know about mono, monodevelop seems to be far superior than ddd. I do not know about responsiveness of the UI running on wine and mono BTW.
Manohar
+1  A: 

Yes, for a few reasons:

1) .net wraps Win32 code. .net is usually a superior system to code against, but having some knowledge of the underlying Win32 layer (oops, WinAPI now that there is 64-bit code too) bolsters your knowledge of what is really happening.

2) in this economy, it is better to have some advantages over the other guy when you are looking for a job. Some WinAPI experience may provide this for you.

3) some system aspects are not available through the .net framework yet, and if you want to access those features you will need to use p/invoke (see http://www.pinvoke.net for some help there). Having at least a smattering of WinAPI experience will make your p/invoke development effort a lot more efficient.

Jay
+2  A: 

This is the answer to any question that is like.. "does it make sense to learn a low level language/api X even when a higher level language/api Y is there"

YES

You are able to boot up your Windows PC (or any other OS) and ask this question in SO because a couple of guys in Microsoft wrote 16-bit assembly code that loads your OS.

Your browser works because someone wrote an OS kernel in C that serves all your browser's requests.

It goes all the way up to scripting languages.

Big or small, there is always a market and opportunity to write something in any level of abstraction. You just have to like it and fit in the right job.

No api/language at any level of abstraction is irrelevent unless there is a better one competing at the same level.

Another way of looking at it: A good example from one of Michael Abrash's book: A C programmer was given the task of writing a function to clear the screen. Since C was a better (higher level) abstraction over assembly and all, the programmer only knew C and knew it well. He did his best - he moved the cursor to each location on the screen and cleared the character there. He optimized the loop and made sure it ran as fast as it could. But still it was slow... until some guy came in and said there was some BIOS/VGA instruction or something that could clear the screen instantly.

It always helps to know what you are walking on.

Senthil