Pelles C forum

C language => Expert questions => Topic started by: vedsharma on April 13, 2013, 03:26:02 AM

Title: how to use HRESULT in C function ?
Post by: vedsharma on April 13, 2013, 03:26:02 AM
 how can i write a function to return HRESULT, i Don't know what is HRESULT and how should it be used for, googled it and found that, HRESULT can be used to represent the error condition in our program, will not go deep it those bit values, but what i understood was
1. 0 or positive number indicates sucess
2. negative number will indicate failure

My Scenario, I have the following structure
struct NODE
{
       NODE* pLeft;
       NODE* pRight;
}

Now I want to write a function
HRESULT TreeHasDepth(NODE* pNode, int depth)
{
  // my code here
}

In above function,
1. if depth of tree is equal to the given value of depth passed
2. if depth of tree is greater than the given argument passed
3. if depth is less than the given value of integer passed
4. if the tree is null

For the above scenarios to be tested, probably we will have to override HRESULT, can someone help me to understand how can  we do it ?

Experts your help is greatly appreciated.

Thanks,
Ved
Title: Re: how to use HRESULT in C function ?
Post by: sapero on April 13, 2013, 12:50:06 PM
Hi vedsharma,
All what you need can be found in the original winerror.h from any of the released sdks,msdns (search for and download windows sdk). The header in Pelles package is outdated, and does not include all the comments.

Code: [Select]
// winerror.h
//   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
//   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
//  +-+-+-+-+-+---------------------+-------------------------------+
//  |S|R|C|N|r|    Facility         |               Code            |
//  +-+-+-+-+-+---------------------+-------------------------------+

#include <winerror.h> // if not included from windows.h

// define some custom constants
#define FACILITY_TREE 1234 // any 11-bit value
//1. if depth of tree is equal to the given value of depth passed
#define E_TREE_DEPTH_EQUAL   MAKE_HRESULT(SEVERITY_ERROR, FACILITY_TREE, 0)
//2. if depth of tree is greater than the given argument passed
#define E_TREE_DEPTH_GREATER MAKE_HRESULT(SEVERITY_ERROR, FACILITY_TREE, 1)
//3. if depth is less than the given value of integer passed
#define E_TREE_DEPTH_LESS    MAKE_HRESULT(SEVERITY_ERROR, FACILITY_TREE, 2)
//4. if the tree is null
// use E_POINTER or E_INVALIDARG, or define another E_TREE_DEPTH_*

The FACILITY constant can be any 11 bit value (0-2047), but in the ideal case it should not conflict with other facilities from winerror.h. Of course you can use error codes and facilities already defined, for example (just pick the correct names):
Code: [Select]
#define E_TREE_DEPTH_EQUAL   MAKE_HRESULT(SEVERITY_ERROR, FACILITY_UI, ERROR_SAME_DRIVE)