Solved

see if handle is valid ?

Posted on 1998-03-19
5
641 Views
Last Modified: 2013-12-03
GetExitCodeThread(pReadingThread->m_hThread, &ExitCode);
 
when I use it as above, I sometimes crash as debugger tells:

pReadingThread      0x00dddb50 {CWinThread h=??? proc=???}

so how should I check if pReadingThread->m_hThread is
valid ?
0
Comment
Question by:has
5 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1412113
There is no way to test if a thread handle is valid, but I suspect the problem is that pReadingThread is a bad pointer, not that pReadingThread->m_hThread is a bad handle.
0
 
LVL 1

Expert Comment

by:rayofunreal
ID: 1412114
Hi all...

Dear niethod, I think there is a function to do this. It is called GetHandleInformation(HANDLE hObj,DWORD* flg);
I don't know if it works with all handles, but I think yes. If no, reject me :-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412115
That returns information about the object referenced by the handle.  It does work on all valid handles, but it does not test if a handle is valid.  I suspect that if you pass it a bad handle the results will probably be unpredicatable.  That is, it won't always returns an error.  You might want to test this, however.  Create and then destoy something and then test the handle with this procedure.  To be safe you need to do a bunch of tests under different circumstances.
0
 

Author Comment

by:has
ID: 1412116
gethandleinformation will fail too, but saving
pReadingThread->m_hThread to a variable when thread starts
then using it in getexitcodethread will work, as stefanr
suggests, so I will give the credit to him, when he answers
my question again here, thanks.
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1412117
There are APIs to check whether a memory block is valid.  Try the IsBad{something}Ptr() family of functions.  Ffor example, IsBadReadPtr().

First, check pReadingThread, then m_hThread (handles are actually pointers, as you can see in the windows include files).

A different approach can be using the Win32 Structured Exception Handling and catching the exception that crashes your program.  See the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now