Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is WinAPI so much different from "normal" C?

Tags:

winapi

I wonder why the WinAPI is so much different from "normal" C programming?

I mean, in school I learned that every C programm has a main() function (WinAPI uses WinMain with some special parameters), some variable types like int, long, char etc. (WinAPI uses things like LPCSTR, BOOL, etc.) so why did Microsoft decide to go such a different way with their OS API?

When I saw my first WinAPI program I it looks more like a new language to me... ;)

like image 797
Inno Avatar asked Dec 02 '09 15:12

Inno


People also ask

What is WinAPI in C?

The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems.

Is Win32 API C or C++?

The company that I referred gave me advise to learn about Win32. But when I begin to learn about it, I encountered that Win32 is C++ based.

What is WinAPI used for?

The Windows API (application programming interface) allows user-written programs to interact with Windows, for example to display things on screen and get input from mouse and keyboard. All Windows programs except console programs must interact with the Windows API regardless of the language.

What language is the Windows API written in?

A native desktop client application is a C or C++ windowed application that uses the original native Windows C APIs or Component Object Model (COM) APIs to access the operating system. Those APIs are themselves written mostly in C.


3 Answers

The original Windows API was designed in the 1984-85 time frame, over 25 years ago. Hungarian Notation was all the rage, so putting the type of a variable into the declaration was the thing to do. For example, in pure C, there is no way to indicate a 'far' pointer, which is what the LP in LPCSTR indicates, but in 1985, it was very important to distinguish between regular pointers and far pointers. (That importance went by the wayside when 32-bit windows took over in the mid-90s, but the syntax persists...)

Also, C doesn't really distinguish between just a pointer to a char and a pointer to a static string. Thus the lpsz types.

In the end, it's about bringing a stronger, consistent typing to the parameters than plain C allowed in 1984. As for the WinMain, it's because a Windows program is pretty fundamentally different from a command line program. If you look in the library, you'd probably find a main() function that sets up the parameters and then calls into an extern WinMain function (i.e. yours).

like image 132
Aric TenEyck Avatar answered Jan 03 '23 14:01

Aric TenEyck


There are two major reasons:

  • Complexity. The C language is minimal, providing the building blocks on which more complex architectures can be constructed. LPCSTR, BOOL and other types that you find in Win32 are typedefs or structs built on top of C.
  • Event orientation. C is usually taught assuming that your program is proactive and in control of things. In an event-oriented environment, such as Windows (or any other GUI-based OS), your program is called by the OS, so it usually sits there in a loop waiting for messages to arrive.

The APIs of other GUI-based OSs may feel different to Win32, because there is no single solution, but the problem they are solving is the same one.

like image 41
CesarGon Avatar answered Jan 03 '23 15:01

CesarGon


Microsoft's Raymand Chen writes in his blog:

Although the function WinMain is documented in the Platform SDK, it's not really part of the platform. Rather, WinMain is the conventional name for the user-provided entry point to a Windows program.

The real entry point is in the C runtime library, which initializes the runtime, runs global constructors, and then calls your WinMain function (or wWinMain if you prefer a Unicode entry point).

like image 41
Stefan Schultze Avatar answered Jan 03 '23 15:01

Stefan Schultze