closing handle

This is a discussion on closing handle within the C++ Programming forums, part of the General Programming Boards category; ok, i have first defined a handle as a private and them i am inititating it in method and again ...

  1. #1
    swordfish
    Guest

    Angry closing handle

    ok, i have first defined a handle as a private and them i am inititating it in method and again when i try to close it in another method close fails. here is how it is:

    class abc{

    private:
    HANDLE hCom;

    public:
    initiate();
    closeP();
    }

    abc :: initiate()
    {

    hCom = CreateFile(pcCommPort, GENERIC_READ, 0, NULL, OPEN_EXISTING, ,..., NULL );

    /////////if i call closeP() here it works
    }



    abc::closeP()
    ///////but closeP() does not work when i call it otherwise
    ////// and i always get the message box
    {
    if ( CloseHandle(hCom) == 0 )
    {
    MessageBox(NULL, "close failed", NULL, MB_OK);
    }
    }


    why is above?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Doesn't CloseHandle returns 0 when successfull?
    // Gliptic

  3. #3
    swordfish
    Guest
    nop it returns zero when it fails

    "If the function fails, the return value is zero"

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Ohh, most of the API functions returns zero when successfull (except functions returning handles) so I just did an assumption.
    // Gliptic

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    What does GetLastError() tell you?
    zen

  6. #6
    swordfish
    Guest
    it says 0 (zero)

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ...hmmm, that would indicate that it was working. I tried a simple example based on your class and created a handle to my COM1 and it closed in it's own function, so I think the problem must be somewhere else in your code.

    If you're using MSVC, you can create a watch on variable "@err,hr" and step through your code in the debugger, any error returned from a win32 function will be displayed in the corresponding value box. This may indicate where your code is failing.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Closing a handle
    By swordfish in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2001, 05:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21