|< C Run-Time 4 | Main | C & Win32 programming 2 >| Site Index | Download |
MODULE C
INTRO TO WIN32 AND C PROGRAMMING 1
My Training Period: hours
Note:
Program examples compiled using Visual C++ .Net (Visual studio .Net 2003). It is low-level programming and the .Net used is Unmanaged (/clr is not set: Project menu → your_project_name Properties… sub menu → Configuration Properties folder → General subfolder → Used Managed Extension setting set to No). All programs are in debug mode. The pre-requirement for this Module are structure, function, pointer and array.
▪ Be familiar and play around with the Win32 programming mainly their Application Programming Interfaces (APIs) and the environment.
▪ Able to understand and use Windows data type.
▪ Able to understand and use Windows header files properly.
▪ Able to understand and use the Windows system error codes through the GetLastError().
|
|
Some Basic Background Story of The Win32 APIs
- The Win32 application programming interface (API) provides building blocks used by applications written for the Microsoft Windows operating system family. - It defines the 32-bit members of the Windows family from the programmer's point of view and you will find out later, 64-bit members also included. Some members of the Windows family use the entire Win32 API, while others use subsets. - The Microsoft Foundation Class Library (MFC) encapsulates, or "wraps," most of the Win32 API but not all. - MFC versions 2.x and earlier encapsulated the 16-bit Windows API. MFC supplies classes representing key Windows objects, such as windows, dialog boxes, brushes, pens, and fonts. - The member functions of these classes wrap most of the important Win32 API functions associated with the encapsulated object. - The MFC class member function calls the Win32 API function, and might add functionality. - The Active Template Library (ATL) is a set of template-based C++ classes that let you create small, fast Component Object Model (COM) objects. ATL wraps Win32 and C run-time library APIs, but does not wrap Win32 to the extent that MFC does. - With Visual C++, you can program for Windows using either:
1. C or C++ and the Win32 API. 2. C++ and MFC or 3. C++ and ATL.
- For information about Win32 programming we normally refer to Win32 API and the documentation is available in the Platform SDK. - In the previous Module we have been introduced to some of the Microsoft implementation of the C using the C Run-time (CRT) libraries. In this Module we will explore the Win32 APIs with C. - This API is designed for use by C/C++ programmers but in this Module we still drop the Windows graphical user interface and message-driven related things, the MFC and ATL. Don’t worry; C/C++ codes are reusable in the MFC and ATL. - Whatever it is, from C/C++ view, these APIs are just functions. As usual, we are going to learn how to use those functions. - We will learn several categories of the available APIs. For others, please refer to the Platform SDK documentations. - First of all let start with the most basic information required to fully understand how to use the Win32 APIs: the Windows data types, header files issue and handles. - Other than the standard data types that you have found when doing the C/C++ programming, there is a lot more in Win32 programming. - Because of the update issue and the various Windows OS versions, using the same Windows header file doesn’t mean you will be provided with the same functionalities. This issue will be solved by using macros. - Later on we will concentrate on the file management functions that available in Win32 APIs by collecting the required and related information and then try building simple working programs. - These functions used to deal with volumes, disks, directories (including paths) and files management. The file system used in the discussion is NTFS. - The not so understandable stories about Windows file system and storage can be found Supplementary for Windows file system. - There is no new constructs you will find in this Module except functions, arrays, pointers and structures.
Windows Data Types
- The data types supported by Microsoft Windows are used to define function return values, function and message parameters, structure members and variables. They define the size and meaning of these elements. - The following Table contains types of character, integer, Boolean, pointer, and handle. The character, integer, and Boolean types are common to most C/C++ compilers. The types also include the 64 bits size. - If you have noticed, many types actually are derived from the basic types with new name using the typedef or #define and some are specific for the graphic functions. |
- Most of the pointer-type names begin with a prefix of P or LP (long pointer) and handle with H.
- Handles refer to a resource that has been loaded into memory and will be discussed in another Module.
- Well, we have so many data types here :o).
- These various data types defined in several header files such as windef.h, basetsd.h, ddeml.h, shellapi.h, winuser.h, winsvc.h and winnt.h.
|
Term |
Description |
|
ATOM |
Atom. This type is declared in windef.h as follows: typedef WORD ATOM; |
|
BOOL |
Boolean variable (should be TRUE or FALSE). This type is declared in windef.h as follows: typedef int BOOL; |
|
BOOLEAN |
Boolean variable (should be TRUE or FALSE). This type is declared in winnt.h as follows: typedef BYTE BOOLEAN; |
|
BYTE |
Byte (8 bits). This type is declared in windef.h as follows: typedef unsigned char BYTE; |
|
CALLBACK |
Calling convention for callback functions. This type is declared in windef.h as follows: #define CALLBACK __stdcall |
|
CHAR |
8-bit Windows (ANSI) character. This type is declared in winnt.h as follows: typedef char CHAR; |
|
COLORREF |
Red, green, blue (RGB) color value (32 bits). This type is declared in windef.h as follows: typedef DWORD COLORREF; |
|
CONST |
Variable whose value is to remain constant during execution. This type is declared in windef.h as follows: #define CONST const |
|
DWORD |
32-bit unsigned integer. This type is declared in windef.h as follows: typedef unsigned long DWORD; |
|
DWORDLONG |
64-bit unsigned integer. This type is declared in winnt.h as follows: typedef ULONGLONG DWORDLONG; |
|
DWORD_PTR |
Unsigned long type for pointer precision. Use when casting a pointer to a long type to perform pointer arithmetic. Also commonly used for general 32-bit parameters that have been extended to 64 bits in 64-bit Windows. This type is declared in basetsd.h as follows: typedef ULONG_PTR DWORD_PTR; |
|
DWORD32 |
32-bit unsigned integer. This type is declared in basetsd.h as follows: typedef unsigned int DWORD32; |
|
DWORD64 |
64-bit unsigned integer. This type is declared in basetsd.h as follows: typedef unsigned __int64 DWORD64; |
|
FLOAT |
Floating-point variable. This type is declared in windef.h as follows: typedef float FLOAT; |
|
HACCEL |
Handle to an accelerator table. This type is declared in windef.h as follows: typedef HANDLE HACCEL; |
|
HANDLE |
Handle to an object. This type is declared in winnt.h as follows: typedef PVOID HANDLE; |
|
HBITMAP |
Handle to a bitmap. This type is declared in windef.h as follows: typedef HANDLE HBITMAP; |
|
HBRUSH |
Handle to a brush. This type is declared in windef.h as follows: typedef HANDLE HBRUSH; |
|
HCOLORSPACE |
Handle to a color space. This type is declared in windef.h as follows: if(WINVER >= 0x0400) typedef HANDLE HCOLORSPACE; |
|
HCONV |
Handle to a dynamic data exchange (DDE) conversation. This type is declared in ddeml.h as follows: typedef HANDLE HCONV; |
|
HCONVLIST |
Handle to a DDE conversation list. This type is declared in ddeml.h as follows: typedef HANDLE HCONVLIST; |
|
HCURSOR |
Handle to a cursor. This type is declared in windef.h as follows: typedef HICON HCURSOR; |
|
HDC |
Handle to a device context (DC). This type is declared in windef.h as follows: typedef HANDLE HDC; |
|
HDDEDATA |
Handle to DDE data. This type is declared in ddeml.h as follows: typedef HANDLE HDDEDATA; |
|
HDESK |
Handle to a desktop. This type is declared in windef.h as follows: typedef HANDLE HDESK; |
|
HDROP |
Handle to an internal drop structure. This type is declared in shellapi.h as follows: typedef HANDLE HDROP; |
|
HDWP |
Handle to a deferred window position structure. This type is declared in winuser.h as follows: typedef HANDLE HDWP; |
|
HENHMETAFILE |
Handle to an enhanced metafile. This type is declared in windef.h as follows: typedef HANDLE HENHMETAFILE; |
|
HFILE |
Handle to a file opened by OpenFile(), not CreateFile(). This type is declared in windef.h as follows: typedef int HFILE; |
|
HFONT |
Handle to a font. This type is declared in windef.h as follows: typedef HANDLE HFONT; |
|
HGDIOBJ |
Handle to a GDI object. This type is declared in windef.h as follows: typedef HANDLE HGDIOBJ; |
|
HGLOBAL |
Handle to a global memory block. This type is declared in windef.h as follows: typedef HANDLE HGLOBAL; |
|
HHOOK |
Handle to a hook. This type is declared in windef.h as follows: typedef HANDLE HHOOK; |
|
HICON |
Handle to an icon. This type is declared in windef.h as follows: typedef HANDLE HICON; |
|
HINSTANCE |
Handle to an instance. This type is declared in windef.h as follows: typedef HANDLE HINSTANCE; |
|
HKEY |
Handle to a registry key. This type is declared in windef.h as follows: typedef HANDLE HKEY; |
|
HKL |
Input locale identifier. This type is declared in windef.h as follows: typedef HANDLE HKL; |
|
HLOCAL |
Handle to a local memory block. This type is declared in windef.h as follows: typedef HANDLE HLOCAL; |
|
HMENU |
Handle to a menu. This type is declared in windef.h as follows: typedef HANDLE HMENU; |
|
HMETAFILE |
Handle to a metafile. This type is declared in windef.h as follows: typedef HANDLE HMETAFILE; |
|
HMODULE |
Handle to a module. The value is the base address of the module. This type is declared in windef.h as follows: typedef HINSTANCE HMODULE; |
|
HMONITOR |
Handle to a display monitor. This type is declared in windef.h as follows: if(WINVER >= 0x0500) typedef HANDLE HMONITOR; |
|
HPALETTE |
Handle to a palette. This type is declared in windef.h as follows: typedef HANDLE HPALETTE; |
|
HPEN |
Handle to a pen. This type is declared in windef.h as follows: typedef HANDLE HPEN; |
|
HRESULT |
Return code used by interfaces. It is zero upon success and nonzero to represent an error code or status information. This type is declared in winnt.h as follows: typedef LONG HRESULT; |
|
HRGN |
Handle to a region. This type is declared in windef.h as follows: typedef HANDLE HRGN; |
|
HRSRC |
Handle to a resource. This type is declared in windef.h as follows: typedef HANDLE HRSRC; |
|
HSZ |
Handle to a DDE string. This type is declared in ddeml.h as follows: typedef HANDLE HSZ; |
|
HWINSTA |
Handle to a window station. This type is declared in windef.h as follows: typedef HANDLE WINSTA; |
|
HWND |
Handle to a window. This type is declared in windef.h as follows: typedef HANDLE HWND; |
|
INT |
32-bit signed integer. This type is declared in windef.h as follows: typedef int INT; |
|
INT_PTR |
Signed integral type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic. This type is declared in basetsd.h as follows: #if defined(_WIN64) typedef __int64 INT_PTR; #else typedef int INT_PTR; |
|
INT32 |
32-bit signed integer. This type is declared in basetsd.h as follows: typedef signed int INT32; |
|
INT64 |
64-bit signed integer. This type is declared in basetsd.h as follows: typedef signed __int64 INT64; |
|
LANGID |
Language identifier. This type is declared in winnt.h as follows: typedef WORD LANGID; |
|
LCID |
Locale identifier. This type is declared in winnt.h as follows: typedef DWORD LCID; |
|
LCTYPE |
Locale information type. This type is declared in winnls.h as follows: typedef DWORD LCTYPE; |
|
LGRPID |
Language group identifier. This type is declared in winnls.h as follows: typedef DWORD LGRPID; |
|
LONG |
32-bit signed integer. This type is declared in winnt.h as follows: typedef long LONG; |
|
LONGLONG |
64-bit signed integer. This type is declared in winnt.h as follows: typedef __int64 LONGLONG; #else typedef double LONGLONG; |
|
LONG_PTR |
Signed long type for pointer precision. Use when casting a pointer to a long to perform pointer arithmetic. This type is declared in basetsd.h as follows: #if defined(_WIN64) typedef __int64 LONG_PTR; #else typedef long LONG_PTR; |
|
LONG32 |
32-bit signed integer. This type is declared in basetsd.h as follows: typedef signed int LONG32; |
|
LONG64 |
64-bit signed integer. This type is declared in basetsd.h as follows: typedef __int64 LONG64; |
|
LPARAM |
Message parameter. This type is declared in windef.h as follows: typedef LONG_PTR LPARAM; |
|
LPBOOL |
Pointer to a bool. This type is declared in windef.h as follows: typedef BOOL *LPBOOL; |
|
LPBYTE |
Pointer to a BYTE. This type is declared in windef.h as follows: typedef BYTE *LPBYTE; |
|
LPCOLORREF |
Pointer to a COLORREF value. This type is declared in windef.h as follows: typedef DWORD *LPCOLORREF; |
|
LPCSTR |
Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. This type is declared in winnt.h as follows: typedef CONST CHAR *LPCSTR; |
|
LPCTSTR |
An LPCWSTR if UNICODE is defined, an LPCSTR otherwise. This type is declared in winnt.h as follows: #ifdef UNICODE typedef LPCWSTR LPCTSTR; #else typedef LPCSTR LPCTSTR; |
|
LPCVOID |
Pointer to a constant of any type. This type is declared in windef.h as follows: typedef CONST void *LPCVOID; |
|
LPCWSTR |
Pointer to a constant null-terminated string of 16-bit Unicode characters. This type is declared in winnt.h as follows: typedef CONST WCHAR *LPCWSTR; |
|
LPDWORD |
Pointer to a DWORD. This type is declared in windef.h as follows: typedef DWORD *LPDWORD; |
|
LPHANDLE |
Pointer to a HANDLE. This type is declared in windef.h as follows: typedef HANDLE *LPHANDLE; |
|
LPINT |
Pointer to an int. This type is declared in windef.h as follows: typedef int *LPINT; |
|
LPLONG |
Pointer to a LONG. This type is declared in windef.h as follows: typedef long *LPLONG; |
|
LPSTR |
Pointer to a null-terminated string of 8-bit Windows (ANSI) characters. This type is declared in winnt.h as follows: typedef CHAR *LPSTR; |
|
LPTSTR |
An LPWSTR if UNICODE is defined, an LPSTR otherwise. This type is declared in winnt.h as follows: #ifdef UNICODE typedef LPWSTR LPTSTR; #else typedef LPSTR LPTSTR; |
|
LPVOID |
Pointer to any type. This type is declared in windef.h as follows: typedef void *LPVOID; |
|
LPWORD |
Pointer to a WORD. This type is declared in windef.h as follows: typedef WORD *LPWORD; |
|
LPWSTR |
Pointer to a null-terminated string of 16-bit Unicode characters. This type is declared in winnt.h as follows: typedef WCHAR *LPWSTR; |
|
LRESULT |
Signed result of message processing. This type is declared in windef.h as follows: typedef LONG_PTR LRESULT; |
|
PBOOL |
Pointer to a bool. This type is declared in windef.h as follows: typedef BOOL *PBOOL; |
|
PBOOLEAN |
Pointer to a bool. This type is declared in winnt.h as follows: typedef BOOLEAN *PBOOLEAN; |
|
PBYTE |
Pointer to a BYTE. This type is declared in windef.h as follows: typedef BYTE *PBYTE; |
|
PCHAR |
Pointer to a CHAR. This type is declared in winnt.h as follows: typedef CHAR *PCHAR; |
|
PCSTR |
Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. This type is declared in winnt.h as follows: typedef CONST CHAR *PCSTR; |
|
PCTSTR |
A PCWSTR if UNICODE is defined, a PCSTR otherwise. This type is declared in winnt.h as follows: #ifdef UNICODE typedef LPCWSTR PCTSTR; #else typedef LPCSTR PCTSTR; |
|
PCWSTR |
Pointer to a constant null-terminated string of 16-bit Unicode characters. This type is declared in winnt.h as follows: typedef CONST WCHAR *PCWSTR; |
|
PDWORD |
Pointer to a DWORD. This type is declared in windef.h as follows: typedef DWORD *PDWORD; |
|
PDWORDLONG |
Pointer to a DWORDLONG. This type is declared in winnt.h as follows: typedef DWORDLONG *PDWORDLONG; |
|
PDWORD_PTR |
Pointer to a DWORD_PTR. This type is declared in basetsd.h as follows: typedef DWORD_PTR *PDWORD_PTR; |
|
PDWORD32 |
Pointer to a DWORD32. This type is declared in basetsd.h as follows: typedef DWORD32 *PDWORD32; |
|
PDWORD64 |
Pointer to a DWORD64. This type is declared in basetsd.h as follows: typedef DWORD64 *PDWORD64; |
|
PFLOAT |
Pointer to a FLOAT. This type is declared in windef.h as follows: typedef FLOAT *PFLOAT; |
|
PHANDLE |
Pointer to a HANDLE. This type is declared in winnt.h as follows: typedef HANDLE *PHANDLE; |
|
PHKEY |
Pointer to an HKEY. This type is declared in windef.h as follows: typedef HKEY *PHKEY; |
|
PINT |
Pointer to an int. This type is declared in windef.h as follows: typedef int *PINT; |
|
PINT_PTR |
Pointer to an INT_PTR. This type is declared in basetsd.h as follows: typedef INT_PTR *PINT_PTR; |
|
PINT32 |
Pointer to an INT32. This type is declared in basetsd.h as follows: typedef INT32 *PINT32; |
|
PINT64 |
Pointer to an INT64. This type is declared in basetsd.h as follows: typedef INT64 *PINT64; |
|
PLCID |
Pointer to an LCID. This type is declared in winnt.h as follows: typedef PDWORD PLCID; |
|
PLONG |
Pointer to a LONG. This type is declared in winnt.h as follows: typedef LONG *PLONG; |
|
PLONGLONG |
Pointer to a LONGLONG. This type is declared in winnt.h as follows: typedef LONGLONG *PLONGLONG; |
|
PLONG_PTR |
Pointer to a LONG_PTR. This type is declared in basetsd.h as follows: typedef LONG_PTR *PLONG_PTR; |
|
PLONG32 |
Pointer to a LONG32. This type is declared in basetsd.h as follows: typedef LONG32 *PLONG32; |
|
PLONG64 |
Pointer to a LONG64. This type is declared in basetsd.h as follows: typedef LONG64 *PLONG64; |
|
POINTER_32 |
32-bit pointer. On a 32-bit system, this is a native pointer. On a 64-bit system, this is a truncated 64-bit pointer. This type is declared in basetsd.h as follows: #if defined(_WIN64) #define POINTER_32 __ptr32 #else #define POINTER32 |
|
POINTER_64 |
64-bit pointer. On a 64-bit system, this is a native pointer. On a 32-bit system, this is a sign-extended 32-bit pointer. This type is declared in basetsd.h as follows: #define POINTER_64 __ptr64 |
|
PSHORT |
Pointer to a SHORT. This type is declared in winnt.h as follows: typedef SHORT *PSHORT; |
|
PSIZE_T |
Pointer to a SIZE_T. This type is declared in basetsd.h as follows: typedef SIZE_T *PSIZE_T; |
|
PSSIZE_T |
Pointer to a SSIZE_T. This type is declared in basetsd.h as follows: typedef SSIZE_T *PSSIZE_T; |
|
PSTR |
Pointer to a null-terminated string of 8-bit Windows (ANSI) characters. This type is declared in winnt.h as follows: typedef CHAR *PSTR; |
|
PTBYTE |
Pointer to a TBYTE. This type is declared in winnt.h as follows: typedef TBYTE *PTBYTE; |
|
PTCHAR |
Pointer to a TCHAR. This type is declared in winnt.h as follows: typedef TCHAR *PTCHAR; |
|
PTSTR |
A PWSTR if UNICODE is defined, a PSTR otherwise. This type is declared in winnt.h as follows: #ifdef UNICODE typedef LPWSTR PTSTR; #else typedef LPSTR PTSTR; |
|
PUCHAR |
Pointer to a UCHAR. This type is declared in windef.h as follows: typedef UCHAR *PUCHAR; |
|
PUINT |
Pointer to a UINT. This type is declared in windef.h as follows: typedef UINT *PUINT; |
|
PUINT_PTR |
Pointer to a UINT_PTR. This type is declared in basetsd.h as follows: typedef UINT_PTR *PUINT_PTR; |
|
PUINT32 |
Pointer to a UINT32. This type is declared in basetsd.h as follows: typedef UINT32 *PUINT32; |
|
PUINT64 |