How to set up smartphones and PCs. Informational portal

What is working with api. Here is a small list of standard Windows DLLs

Sandbox

burly man November 26, 2012 at 01:59 pm

What is API

  • Lumber room *

Greetings!
In this article, we will look at what an API is, where, how and for what it is used. We will also look at how the API can be used in your web development and how it can make life easier for a web programmer.

So let's start with a definition. API (Application Programming Interface) is a programming interface, an interface for creating applications. In a more understandable language, the API is ready-made code to simplify the life of a programmer. The API was created so that the programmer could really facilitate the task of writing this or that application by using ready-made code (for example, functions). The well-known jQuery written in JavaScript is also a kind of API. If we consider this particular example, then jQuery makes it much easier to write code. What could be done in 30 lines with the usual JavaScript tools, is written through jQuery in 5-6. If we consider the API in general, then you can find a lot of services that represent development solutions. The most famous today is the code.google.com service, which provides about fifty different APIs! This is an interface for creating Android applications, and various APIs for working with AJAX, and various application APIs that can be easily customized.

After all, does it make sense to write code with your own hands? Why work on what has already been created? Does it make sense to refuse free solutions (and in fact, free help) in web development? If you answered “NO” to all these questions, then consider that you understand the essence of the API.

But I also want to clarify. Novice developers should NOT use semi-finished solutions, as they will not cope with the real task in the future. Therefore, if you are a beginner web programmer, then do not use semi-finished products! Learn to think with your head, build various algorithms to understand the essence of programming. I also say, already addressing everyone, that the API is not a ready-made solution, it is an environment, an interface for creating your own projects. You don't eat frozen meatballs from the store, do you? You fry them first, right? This analogy captures the essence of the API very clearly.

In general, I told what the API is, where and how it is used, most importantly, what for. I wish you a pleasant study of web programming and comprehension of its greater depths!

No tags

This article is not subject to comments, since its author is not yet a full member of the community. You will be able to contact the author only after he receives

By definition from Wikipedia, API is a set of ready-made classes, procedures, functions, structures and constants provided by an application (library, service) for use in external software products. Used by programmers to write all sorts of applications.

But since a lot of things on Wikipedia are not understandable for many people, I will try to explain on my fingers what an API is and what they are usually made for, and how they are used.

APIs are completely different, but for example, I chose a situation where we have a network of stores and only one common database. Imagine that you own an affiliate program. The affiliate program works according to the following principle: a person registers in the affiliate program and receives a store engine. Then he can put this store on his hosting and start working. But all the data on this store is taken from our database, that is, we need to give each partner access to our precious database. Can you imagine how dangerous it is? After all, we need to open access to the database from outside, so that all partner stores can work with it. But what happens if the access data falls into the hands of intruders?

This is where the API will help us. Instead of giving access to the database, we will simply make an API through which partner stores will receive information. Thus, only our API script will work with the database, and stores will work with this script.

How it works?
For example, a store sends a request to our API
http://ourapi.com/get_books?limit=20
and our API understands that it needs to return a list of books consisting of 20 copies, because we passed the limit parameter equal to 20. Our script (API) makes a request to the database, receives a list of books and returns them to the store (in fact, it simply displays ) in a specific format. The format in which the API returns information can be absolutely anything, the main thing is that our stores understand it. It can be JSON, a serialized array, or XML. It doesn't matter anymore, the main thing is that you understand the principle.

The set of commands that the API understands is up to you. For example, in our case, these could be commands such as getting a list of books, getting a list of categories, getting popular books, getting new books, etc. Thus, even if an attacker were to be able to access our API, all he could do was get a list of books, and this does not pose any threat to our database.

I hope I was able to explain what an API is with a simple example. If you have any questions, ask them in the comments or on the forum and we will be happy to help you solve them.

Let's start with the basics: what is an API? The abbreviation stands for Application Programming Interface, or an interface for programming applications. The name seems to speak for itself, but it is better to consider a more detailed explanation.

As already mentioned, the API is, first of all, an interface. An interface that allows developers to use ready-made blocks to build an application. In the case of developing mobile applications, a library for working with a "smart home" can act as an API - all the nuances are implemented in the library and you only refer to this API in your code.

In the case of web applications, the API can return data in a format other than standard HTML, which makes it convenient to use when writing your own applications. Third-party public APIs most often return data in one of two formats: XML or JSON. In case you decide to make an API for your application, remember that JSON is much more concise and easier to read than XML, and services that provide access to data in XML format are phasing out the latter.

API in web applications by examples

An application - for example, Github - has its own API that other developers can use. How they will use it depends on the possibilities that the API provides and how well the imagination of the developers works. The Github API allows, for example, to get information about the user, his avatar, readers, repositories, and many other useful and interesting information.

Similarly, you can send a request in any language, including Ruby. The response to the request will be something like this:

( "login" : "Freika" , "id" : 3738638, "avatar_url" : "https://avatars.githubusercontent.com/u/3738638?v=3", "gravatar_id" : "" , "url" : "https://api.github.com/users/Freika", "html_url" : "https://github.com/Freika" , "followers_url" : "https://api.github.com/users/Freika/followers", "following_url" : "https://api.github.com/users/Freika/following(/other_user)", "gists_url" : "https://api.github.com/users/Freika/gists(/gist_id)", "starred_url" : "https://api.github.com/users/Freika/starred(/owner)(/repo)", "subscriptions_url" : "https://api.github.com/users/Freika/subscriptions", "organizations_url" : "https://api.github.com/users/Freika/orgs", "repos_url" : "https://api.github.com/users/Freika/repos", "events_url" : "https://api.github.com/users/Freika/events(/privacy)", "received_events_url" : "https://api.github.com/users/Freika/received_events", "type" : "User" , "site_admin" : false , "name" : "Evgeniy" , "company" : "" , "blog" : "http://frey.su/" , "location" : " Barnaul" , "email" : "" , "hireable" : true , "bio" : null, "public_repos" : 39, "public_gists" : 13, "followers" : 15, "following" : 21, "created_at" : "2013-03-01T13:48:52Z" , "updated_at" : "2014-12-15T13:55:03Z" )

As you can see from the block above, the response contains a login, an avatar, a link to the profile on the site and in the API, user status, the number of public repositories, and other useful and interesting information.

One API is not enough

Creating a full-fledged API for your application is only half the battle. How are you going to access the API? How will your users access it?

The first thing that comes to mind is the usual series of HTTP requests in order to get the desired information, and this is the wrong answer. The most obvious way in this case is not the most convenient and simple. It would be much more reasonable to create a special library for working with the interface, which will describe all the necessary methods for receiving and sending information using the API.

Let's use Github once again to give an example: to work with the API of this excellent service (and its interface provides extensive possibilities), several libraries have been created in various languages, for example, the Octokit gem. In the documentation for such libraries (and the gem given as an example), any interested developer will be able to find all the necessary ways to receive information from Github and send it back through the service API.

Thus, if you are creating your own API, think about creating libraries for working with it in the most common languages ​​as well. And be prepared that at a certain level of demand for your application, someone else can create their own library to work with your API. This is fine.

useful links

In subsequent articles, we will talk about how to correctly create an API, ensure its security and restrict access to some information.

It's time to talk about such a concept as Function APIs in Visual Basic. Why are these functions important in Visual Basic? Let's see, firstly, when you start programming in Visual Basic, you will immediately understand that using these functions you can write very serious programs, and secondly, you simply cannot do a lot without using API functions, for example, almost everything related to using the Windows operating system ( for example: find out the username, hard disk space and so on).

But in order to start putting these features into practice, you need to know what they are, let's talk about that first.

Theory on Visual Basic API Functions

API is an abbreviation for Application Programming Interface ( application programming interface). In other words, an API is a set of functions that the operating system itself provides to the programmer for use in their programs. Using the API, programmers can easily put their ideas into practice.

API functions can be called from Visual Basic to perform tasks for which standard Visual Basic code is insufficient. For example, the standard Visual Basic tools do not allow you to turn off your computer or restart it. However, all this can be easily done by calling the appropriate API function.

Almost all the functions that are located in Windows are located inside the DLL ( Dynamic Link Library). All dll files are located in the Windows system folder. There are over 1,000 API functions that fall roughly into four main categories.

  • Working with applications - launching and closing applications, processing menu commands, moving and resizing windows;
  • Graphics - creating images;
  • System information - determination of the current disk, memory size, current user name, etc.;
  • Working with the registry - manipulations with the Windows registry.

Application of API functions in practice

Now let's move on to using these functions in practice. API functions called in Visual Basic must first be declared. The declaration specifies the name of the DLL and function, as well as information about the number and type of arguments.

DLL procedures are declared with the Declare command in the General Declarations section of a form module or a standard module:

Declare Function name Lib "library" [([ _ variable [,ByVal,] variable ]...])] As type

DLL procedures found in standard modules are public by default and can be called from anywhere in the application. Procedures declared in a form module are private and must be preceded by the Private keyword.

The Lib section tells you where to look for the DLL file containing the function. When referencing core Windows libraries (user32.dll, kernel32.dll, gdi.dll), the extension is optional.

If the path to the file is not specified, then directory browsing starts in the following sequence

  1. Directory containing the program's EXE file;
  2. current directory;
  3. Windows system directory;
  4. Windows directory;
  5. Path environment variable.

Here is a small list of standard Windows DLLs:

  • advapi.dll - NT protection, registry and other advanced services;
  • comdlg.dll - standard dialog boxes;
  • gdi.dll - graphics device interface;
  • kernel32.dll - memory, disks, processes, resources;
  • mpr.dll - networks;
  • netapi32.dll - network API library;
  • shell32.dll - Windows shell, drag-and-drop, icon selection;
  • user32.dll - user interface - windows, menus, lines, messages;
  • version.dll - version control;
  • winmm.dll - multimedia, sound, joysticks, timers;
  • winspoll.dll - print manager.

Most DLL procedures are documented using C and C++ programming language notation. To successfully convert an API function to a Declare command, you need to understand the argument passing mechanism. Arguments are usually passed by value. In this case, the ByVal keyword is placed before the argument name. If the argument is passed by reference, ByRef is set.

Here is the smallest list of functions that are used in Visual Basic:

  • BitBlt - copies an image from one hDC to another;
  • FindWindow - finds the Handle of the window with the specified Caption;
  • GetCursorPos - returns the current position of the mouse cursor (globally);
  • GetDriveType - returns the drive type;
  • GetForegroundWindow - returns the Handle of the active window;
  • SetCursorPos - sets a new mouse cursor position;
  • SetVolumeLabel - changes the disk label;
  • SetWindowPos - sets the position of the window with a certain Handle (you can also use this function to make the window be on TOP of all;
  • ShellExecute - runs the application associated with the extension that has the specified file (may also just run the specified application);
  • ShowCursor - hides or shows the mouse cursor on the screen.

Registry Examples

Also, when programming in Visual Basic, and in other languages, there is a problem when you need to use the Windows registry, but with the help of api functions this is easy to do, for example:

To read a string parameter, use the GetRegString function

Example:

Dim test As String test = GetRegString(HKEY_LOCAL_MACHINE, _ "Software\Microsoft\DirectX", "Version") Form1.Caption = test

To read a GetRegDWord DWORD parameter

Example:

Dim test As Long test = GetRegDWord(HKEY_LOCAL_MACHINE, _ "Software\Microsoft\Internet Explorer\AboutURLs", "Home") Form1.Caption = test

SetRegString is used to write a string parameter

Example:

SetRegString HKEY_LOCAL_MACHINE, _ "Software\test", "MyProgram", "Options"

If the key or parameter has not been created, then they will be created, if the key or parameter already exists, then the new values ​​will overwrite the old ones.

To write a DWORD parameter, use SetRegDWord

Example:

SetRegDWord HKEY_LOCAL_MACHINE, _ "Software\test", "MyProgram", "555"

To delete a key from the registry, use the DeleteRegKey function.

Example:

DeleteRegKey HKEY_LOCAL_MACHINE, "Software", "test", True

Second parameter - Parent key
The third parameter is True if confirmation is required for deletion or False if confirmation is not required.

To delete a parameter from the registry, use the DeleteRegValue function

Example:

DeleteRegValue HKEY_LOCAL_MACHINE,_ "Software\Microsoft\Internet Explorer\AboutURLs", "mozilla", False

In fact, the possibilities of API functions in the Windows operating system are almost limitless. Let's finish this review of this Visual Basic feature and finally write a small program using API functions.

This program simply displays a message on the screen, paste the following code:

Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function PathToRegion Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long Private Sub Form_Load() Const TEXT = "You used the API" Dim hRgn As Long Font.Name = "Times New Roman" Font.Bold = True Font.Size = 60 Width = TextWidth(TEXT) Height = TextHeight(TEXT) BeginPath hdc Print TEXT EndPath hdc hRgn = PathToRegion(hdc) SetWindowRgn hWnd, hRgn, False Move (Screen.Width - Width) / 2 , (Screen.Height - Height) / 2 End Sub

We used 4 functions here (BeginPath, EndPath, PathToRegion, SetWindowRgn), which are located in two dll libraries (gdi32, user32).

That's all for now!

The first version of Android appeared in October 2008 - only 4 years ago, which is not much for an operating system. During this time, more than two dozen updates have already been released. The main part of the updates included new elements software interface ( API) that applications could use for their needs. In order for this or that version of Android to know whether it can satisfy the program requests of a particular application, numbering of programming interfaces was introduced. The number characterizing the version of the interface was embedded in the application itself, and the system used it to determine the compatibility of this application with itself. This number was called "API level" (API Level). In total, 17 levels have already been accumulated, each subsequent of which included all the functions of the previous one and added new ones.

To write an application, along with a programming system, an SDK is installed on a computer ( software development kit) is a set of development tools, the main part of which is the class library of the corresponding API level. Each API level has its own library, the functions of which are used by the application. If we take a library of the first API level to develop our application, then we will not get at our disposal the functions that appeared later. If we take the last level library, we run the risk of making our application incompatible with older versions of Android if we suddenly use a function that was not supported before. So how do you choose an API layer?

In our application, we must install two API levels (there is also a third, but it has actually died out as unnecessary). The first is the minimum API level at which the application will be able to run, and the second is the target API level, that is, the API level at which the application is compiled. When publishing an application, this level should be as accessible as possible at that time, so that in new versions of Android it does not work in compatibility mode, but uses all the little things of the new version of the operating system (not dependent on the author of the program itself). At the time of writing the program, it should be compiled to the lowest possible API level so as not to use new functions.

As a result, we get the following application development methodology:

  1. We study the target audience - which version of Android is installed on their smartphones. If we are writing for everyone, then it will be useful to get acquainted with constantly updated schedules for using one or another version of the system on mobile devices: http://developer.android.com/intl/ru/about/dashboards/index.html . Today we see that the bulk is already sitting at API level 10, but there are also rather big pieces sitting at levels 7 and 8. You can see the differences in levels here (http://developer.android.com/intl/ru/guide /topics/manifest/uses-sdk-element.html) by clicking on the corresponding API number in the table.
  2. If we do not want to lose about 10% of the audience, we choose the minimum API level 7. Otherwise, we can choose the API level 10, which is used by almost half of the users today. Install the SDK corresponding to the selected minimum level.
  3. We set the target level equal to the minimum, write and compile the program for it. We launch and test it on a smartphone emulator with the minimum level API installed.
  4. After writing and debugging the program, we increase the target level by one, compile the program under the SDK of the new target level and test it in a new smartphone emulator with the appropriate level. Next, we continue to increase the target API level, compile and test the program until we reach the highest API level.
  5. Everything, now you can publish the program. When a new version of Android with a new API level is released, our program will be included in compatibility mode in it and will work as before. In order for the program to work directly, and not in compatibility mode, we take our project, download the new SDK of the latest API level, and compile the application under it. Now the application will start on the new system as usual, and may look a little different, in the tradition of the new version of the operating system. At the same time, on older versions of the system, the application remains the same as it was.
As a result, on reflection, for myself, I stopped at API level 7. In addition, when using some of the new functions that appeared in much later APIs (for example, fragments), the development environment itself suggested that I use a special compatibility package, and added it to the project . As a result, I was able to use some of the new features from the new APIs in the old one. However, this is another topic.

Top Related Articles