api type


Hi I have a question.

i want use winapi. Winapi uses new types as HANDLE,DWORD...
Why api using their own types?
thx

sorry for my english
WinAPI uses their own types for the same reason they have all the 100million+ lines of their header all in "windows.h": namely, they want you dependent on them for your windows programs.

I have no idea why they would do that and then distribute the header free, but they do...

side note: when you use WinAPI, always #define WIN32_LEAN_AND_MEAN so that you only have 80 million or so lines in your header.
The data type 'HANDLE' is a void pointer and the data type 'DWORD' is an integer. There are two major reasons that Microsoft chooses to do this, one is to make it more clear to the programmer what that data represents and another is to make sure that only certain data is used with certain functions.

For example both 'HANDLE' and 'HWND' are void pointer data types, so all they are is an address. At the most basic level the functions that use and interact with them only need an address. How does the user know which pointer to use with which function? Easy, 'HWND' values generally refer to the addresses of windows where as 'HANDLE' values refer to the addresses of Threads, Processes, Files etc. So when the programmer sees that a function in the WinAPI wants the address to something in his process, and that data type is written as 'HWND' they know right away that the function is referring to the address of his window.

EDIT: Some things about what kaseron said:

- Something that tripped me up when I was first starting out is that #define WIN32_LEAN_AND_MEAN goes BEFORE #include<windows.h> . This seems obvious when you think about it but for me it didn't click for awhile.

- #define WIN32_LEAN_AND_MEAN is a pre-processor trick to tell then linker not to bother inserting certain lesser used parts of windows.h into your object file. %75-%90 of the time what kaseron said is right. But if you run into an issue where something is clearly supposed to be declared by including windows.h, but you are still getting errors about it not being declared then delete that line because the chances are that it filtered out the part of the file you needed. NOTE: declared is not the same as defined, if you get errors about something not being defined then it's probably because you didn't like to the lib or you didn't declare the correct versions of '_WIN32_WINNT' and\or 'WINVER'.
Last edited on
Thanks...

and this winapi: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx is designed only for c++?

Visual Basic uses a different WinAPI?
That ellipse after that "Thanks" makes me think some thing wasn't clear, feel free to ask for clarification.

There are not separate WinAPI's for each language, there are only different versions for each OS\Service Pack version.
thank you all

but there is no problem with data types?
example:
there http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx

WinAPI uses DWORD type (this is define inside header file c + +) but 'visual basic' does not include the header file.VisualBasic doesn't support DWORD. Libraries WinAPI DWORD use. How is this resolved? :)

sorry for my english
The native language of Windows was C. The Windows Platform SDK provides Windows.h as the entry point to all their programming interface. It provides a set of type definitions and functions that allow you to write Windows programs.

This is the same for Unix, BeOS, Mach, HURD or any other similar OS you'll use. C has inherited a lot of Unix definitions and their look/feel so other systems will look a little foreign. You should remember, C was developed on Unix to port Unix to different hardware.

You interface with C and C++ sub-systems by using the header files to get compiler definitions and link with the libraries bind to those definitions.

but there is no problem with data types?
What problem do see?

You get definitions for DWORD, WINAPI and Sleep by using Windows.h, not a C++ header file. The program links with Windows libraries.

Visual Basic is a program written by Microsoft for Windows. It's designed to interact with Windows and has it's own way of doing so. Don't be concerned about anything Visual Basic does or the way it does it. It's not portable (it runs only on Windows) and should not influence your use of C or C++.
Last edited on
Excellent thank you
There is another reason that Microsoft has it's own definition of DWORD and others. Over the years, Windows has run on a variety of platforms (8086, 80x86, MIPS, Alpha, X64, etc). Not all of these platforms have the same word size. The winapi header guarantees that DWORD is a 32 bit integral type regardless of which platform is being used, since C does not have a native 32 bit integral type. Each platform's C implementation is free to define short, int and long to be what ever size best suits the implementation.
Topic archived. No new replies allowed.