Thread: How to use HRESULT in C function

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    4

    How to use HRESULT in C function

    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
    Code:
    struct NODE
    {
           NODE* pLeft;
           NODE* pRight;
    }
    Now I want to write a function
    Code:
    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 ?

    Any help is greatly appreciated.

    Regards,
    Ved

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    The `HRESULT' thing is a part of the "WinAPI" used for programming "Windows".

    Don't use it for your own interfaces.

    Beyond that, I don't understand what you are asking about.

    This "we will have to override HRESULT" stuff for example; that's meaningless.

    Soma

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    yes, it is part of WINAPI, but we can use it in our program, by including the required header file.
    I felt that by using this we have a proper way of returning the error in our function, if at all it comes up, like if my function returns S_OK i know and every one else using my function will know that there is no error in this function, this is an advantage over returning "-1" in case of error which is programmers understanding, and i will have to document it fr others to understand, that if
    "-1" is returned something went wrong.
    Makes sense ?
    So, I want to return HRESULT from my function.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Note to mods: this is not a C programming problem. It is a windows programming problem. Suggest moving to appropriate forum.

    Google is your friend. With a search string of "C HRESULT" I found this link.


    HRESULT is just a user defined type. As long as you have a definition of a type (eg by #include'ing an appropriate winAPI header), returning a value of that type is just like any other defined type (int, float, pointers, user defined types, etc etc).

    Within a function, you just need to know how to manipulate values, and comply with any conventions. The link I gave provides that information. Depending on what values you want to return you will need to delve into the "bit values" that you have not - as yet - bothered to delve into.

    I generally would not use HRESULT unless my functions were specifically doing things involving the win32 API. There are easier ways, such as defining error codes that make sense to your application.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    Agreed, there are easier way of defining error codes, but this is how I am suppose to write this function, in general any function with HRESULT as return type.
    I figured this link earlier, but not much of coding help.
    Finding a hard time to understand that, so posted here.
    I will repost it in Windows forum.

    Thanks !!

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You write it exactly like any function that returns an int.

    An HRESULT is just a typedef for a 32 bit signed integer, and all the string codes are just #defines (e.g. S_OK is translated to 0 by the preprocessor)

    The only thing that makes it different from any other integral return type is that it uses various bits of the result as flags (severity, etc.)

    Edit: And returning an HRESULT doesn't absolve you of the necessity of documenting your return values. Apart from S_OK, everything you return is going to be a custom code, and the recipient is going to need to know what it means.
    Last edited by Cat; 04-12-2013 at 10:29 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Catching A HRESULT?
    By Jonnster in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2010, 02:49 PM
  2. E_OUTOFMEMORY hResult with DirectX 10
    By DarkMasterBosel in forum Game Programming
    Replies: 3
    Last Post: 01-04-2009, 01:43 PM
  3. LPCTSTR and HRESULT
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-27-2007, 02:17 AM
  4. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  5. Hresult
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 06-25-2002, 01:11 PM