Windows APIs Some odds and ends Copyright © 1997 – 2016 Curt Hill.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Advertisements

Win32 Programming Lesson 5: Error Codes. Before We Begin  Much of the time in this class we’ll be calling Win32 Functions  However, sometimes they’re.
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill.
Batch Files More flow of control Copyright © by Curt Hill.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 02. Starting Windows Socket. IT COOKBOOK  Goal Error handling routine for Winsock function error Winsock startup and cleanup Socket creation.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
Window Threads Chapter 7 Windows Thread Management.
Windows Communication Foundation and Web Services
Chapter 11: Testing, Testing…1, 2, 3
Microsoft Foundation Classes MFC
Getting and displaying
User-Written Functions
Chapter 7: User-Defined Functions II
Phil Tayco Slide version 1.0 Created Sep 18, 2017
COMP 170 – Introduction to Object Oriented Programming
Introduction to Operating Systems
More important details More fun Part 3
Console and GUI Programs
Using SLK and Flex++ Followed by a Demo
Tortoise SubVersion Client Again
Windows Concurrency Concepts and APIs
Swapping Segmented paging allows us to have non-contiguous allocations
Chapter 19 Java Never Ends
Windows Communication Foundation and Web Services
The Prototyping Paradigm
Exceptions 10-Nov-18.
Predefined Dialog Boxes
CSE 154 Lecture 22: AJAX.
Topics Introduction to File Input and Output
Doing things more than once
Windows APIs File Processing Copyright © 2016 Curt Hill.
Internet Control Message Protocol Version 4 (ICMPv4)
Waiting and Synchronization
Arrays in Java What, why and how Copyright Curt Hill.
Throwing and catching exceptions
Miscellaneous Thoughts Nothing on the APIs
Exam 4 review Copyright © 2008 W. W. Norton & Company.
CMSC 202 Java Primer 2.
Other displays Saving Arrays Using fors to process
Cmdlets “Command-lets”
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Making something known
Examining Variables on Flow Paths
The Java switch Statement
PowerShell Flow of Control Copyright © 2016 – Curt Hill.
Console A presentation by Inti Vincenzo Pizzoni.
Final Jim Brucker.
Lecture 12: The Fetch Api and AJAx
The Threading Demo What is here Copyright © 2017 Curt Hill.
Clear and Unclear Windows
Introduction to Programming
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Tutorial 11 Using and Writing Visual Basic for Applications Code
INSTRUCTION SET DESIGN
Putting Values into a Suitable Form for Display
Varying Character Lengths
Presentation transcript:

Windows APIs Some odds and ends Copyright © 1997 – 2016 Curt Hill

Introduction This presentation considers some things that are commonly used but not considered else where These include: GetLastError FormatMessage CloseHandle Copyright © 1997 – 2016 Curt Hill

GetLastError Almost every API call has an opportunity to fail Duh! These errors do not cause an abort At least not immediately Instead of putting an error code within the parameter list of every function, MicroSoft decided to save the error within the process Then use GetLastError to retrieve it when needed Copyright © 1997 – 2016 Curt Hill

The Function The signature: DWORD WINAPI GetLastError(void); GetLastError returns an error code that is typically one of several defined constants In the documentation for each API call you should see a list of the eligible errors This suggests the following approach Copyright © 1997 – 2016 Curt Hill

One Way Capture the error code with GetLastError Decode it with a switch Display a different message in each case of the switch This is the wrong way, although it often can be made to work The documentation does not always show the constants that correspond to errors Copyright © 1997 – 2016 Curt Hill

Better Way There is another API that is helpful: FormatMessage Signature: DWORD WINAPI FormatMessage( _In_     DWORD   dwFlags, _In_opt_ LPCVOID lpSource, _In_     DWORD   dwMessageId, _In_     DWORD   dwLanguageId, _Out_    LPTSTR  lpBuffer, _In_     DWORD   nSize, _In_opt_ va_list *Arguments ); Copyright © 1997 – 2016 Curt Hill

Even Better Way The Threader demonstration has a method in it called DisplayAPIError This takes a string This is something about the context of the call that produced the error It then calls FormatMessage and appends the resulting string onto the context message Displays it all in a MessageBox Copyright © 1997 – 2016 Curt Hill

Final Thoughts If the DWORD returned by GetLastError is zero there was no error It is better to use the constant: NO_ERROR Only one error is saved and even that for a short time Almost any API call will clear it Therefore save and format the error quickly after the call GetLastError calls always work Return value may or may not Copyright © 1997 – 2016 Curt Hill

Handles Again Handles are one of those resources that need to be reclaimed You may drive Windows into problems if you repeatedly use and do not give back handles You return a handle with: CloseHandle which takes any handle Does not matter what type of thing Copyright © 1997 – 2016 Curt Hill

CloseHandle Again Signature: BOOL WINAPI CloseHandle( _In_ HANDLE hObject ); Any type of handle may be closed Usually the end of process recycles handles Long running programs need to be more careful Copyright © 1997 – 2016 Curt Hill

GetCurrentThread A function that returns a handle to the calling thread This handle allows the thread to modify thread attributes as if it were the thread creator Signature: HANDLE WINAPI GetCurrentThread(void); Handy so that any thread can get a handle to itself Copyright © 1997 – 2016 Curt Hill

Finally There is always more to know Next we consider synchronization Copyright © 1997 – 2016 Curt Hill