What's An API (Part 2)
Posted by Charlie Recksieck on 2019-10-17
Operating Systems APIs
Author’s Note: This series is intended for a more general audience to understand broad concepts, not a how-to for developers. Although hopefully it is of use to developers to contemplate the nature of these terms too.
* * *
In Part 1 we tried to generally educate you on what an API is. We landed on this definition: An API is an interface that software code can use to execute a command or retrieve a set of data..
So here in this article, we’ll focus on the most basic of APIs: ones provided by your operating system (either when it’s installed on your computer or after an update).
My Igloo Analogy
Sometimes an analogy can help folks conceptualize things (if it’s a good analogy). In Part 1 we featured a great one about ATM machines to analogize all APIs. Here’s what I’ll throw at you for operating system APIs:
People have a tendency to talk about "building blocks" so let me use the specifics of an igloo as our premise. If we think of the igloo as an operating system, then each operating-system-related ice block would be an API. You can’t build an operating system without those APIs (you can’t build an igloo without the blocks). But if you needed to cool something inside your igloo, such as chilling a beer, you rest it against the ice block (not the whole igloo). The ice block (or program) can act as an API both for the structure itself but also for the things within the structure, just as a third-party program that runs within the operating system can also use the API.
(How did I do with that analogy?)
What’s A Library
A library is a compiled group of functions installed locally. Have you heard of ".DLLs" ("Dynamic Link Libraries")? They, along with some other library files, are groups of functions; operating systems use these foundational DLLs to do just about everything.
If a library is written so it can be used by other code outside of its own project, then that's when I would start calling it an "API".
Libraries vs. Drivers
Likewise, you've heard of "drivers". What’s the difference between a "driver" and a "library" (or even an "API")? Drivers are programs or libraries that control how the computer interacts with a connected device or a peripheral (a printer, for example).
When you install a printer, you also install drivers including .DLLs that can take a "Print" request from Microsoft Word, or your browser, or other programs that have defined print functions. Those print .DLLs translate the info they receive from MS-Word into a format that the printer can use. When these same printer .DLLs can be used in a program that you or I write, then these are APIs - since our code can use these as an interface to get to the printer.
When Is An Operating System Library An "API" & Who Can Use It?
For starters, let’s reiterate that some libraries are APIs (meaning that can be accessed from something else, like an app or even a webpage). Think of the operating system APIs as a entrance to the library/code.
When you buy a Windows computer, it already has these libraries installed and sitting as .DLL, .TLB and other files on your hard drive. Windows uses these for all of its core functions. The available APIs (often also with the .DLL extension) are also installed.
But not only can Windows functions call other functions in APIs, whole other applications can call these APIs. The the list of most important groups of Windows APIs would look something like this: File System, User Interface, Security, Data Access, User Input, Graphics, to name a few.
If you’re not a programmer, just make sure you remember that Windows has a lot of APIs that any program can leverage. In fact, these functions are basic building blocks and used in coding every day a developer is writing code.
What You Can Get From Windows APIs
There are so many essential Windows libraries and APIs that it does seem a little silly come up with short lists of them here. But I’ll give just a few examples to illustrate not only how powerful they are, but how much a developer needs to use these. I, or any programmer, writing a local application can access this kind of data (and can also be done from the web). Here’s a list of benign things we can access:
* Monitor/Screen size
* Windows logon name
* Can launch a form, take user input
It’s futile to try to make a complete list. I would say about 40% of all code lines in a program are calling a Windows API in some way or another. But here’s a couple of problematic things we could access:
* Can have the user browse their hard drives to look for files, or the code can do this by itself - and with full delete or edit access.
* Anything in the Windows registry can be accessed. This includes personal information, your most recent websites visited, your last 10 Amazon purchases, your Windows Address Book, etc.
We don’t mean to alarm you, but this is a good reminder that you need to take your security seriously, especially when installing a new application.
If we wanted to do something as basic as changing a word from lower case to all caps, we are using a Windows API. Some of these commands are so integral to writing code, that it’s easy for developers that they aren’t just preternaturally there. Code commands have to be created somewhere and Windows APIs are the place to get it.
For our change-to-upper example, we hit the Windows API that has a bunch of great functions for manipulating strings (words) then all you have to do is call the string-handling API, give it the word "lower" with an argument of "lower" using the "ToUpper" command and it changes "lower" to "LOWER" for you.
For non-developers, I don’t think you need to be thinking about Windows libraries and APIs very much. They are so foundational to writing code, that they really can’t be avoided (no matter if in Windows, iOS, Linux or anywhere else). Just know that they’re there, and there are a lot of built-in functions that your developers and IT departments can take advantage of.
Continue reading Part 3 - Applications APIs)