Click here to Skip to main content
15,908,172 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Class wizard in VC 2005 Pin
neha.agarwal2723-Nov-07 17:50
neha.agarwal2723-Nov-07 17:50 
GeneralRe: Class wizard in VC 2005 Pin
Prasann Mayekar23-Nov-07 18:32
Prasann Mayekar23-Nov-07 18:32 
JokeRe: Class wizard in VC 2005 Pin
ThatsAlok23-Nov-07 18:55
ThatsAlok23-Nov-07 18:55 
QuestionTerminateThread hangs Pin
maciu202023-Nov-07 15:10
maciu202023-Nov-07 15:10 
QuestionRe: TerminateThread hangs Pin
Mark Salsbery23-Nov-07 15:22
Mark Salsbery23-Nov-07 15:22 
AnswerRe: TerminateThread hangs [modified] Pin
maciu202023-Nov-07 21:11
maciu202023-Nov-07 21:11 
GeneralRe: TerminateThread hangs Pin
Mark Salsbery24-Nov-07 7:04
Mark Salsbery24-Nov-07 7:04 
GeneralRe: TerminateThread hangs Pin
maciu202024-Nov-07 7:28
maciu202024-Nov-07 7:28 
Mark Salsbery wrote:
maciu2020 wrote:
Unable to attach a debugger


hmm...why?


This is what OllyDbg tells me..

Mark Salsbery wrote:
It really sounds like you have active threads left running.
Closing a thread handle does not eliminate the thread.
All threads should terminate themselves by returning.


Sure. All threads should do it as they have no loops or infinite waits. And when I run one at the time, they all return.

Mark Salsbery wrote:
You shouldn't ever need to terminate a thread or
process forcefully.


Yep. And I didn't do it until this strange case.
Mark Salsbery wrote:
Also, make sure you use the proper thread creation functions, depending on what
the thread uses:


I use the correct one.

I thing it would be good to post the code. It's a slightly modified part of Zoltan Csizmadia's SystemInfo library.
My ThreadProc just calls GetFileName() as below. For readability, I stripped security checks and cleanup.
<br />
BOOL SystemHandleInformation::GetFileName( HANDLE h, CString& str, DWORD processId ) throw()<br />
{<br />
	ULONG size = 0x8000;<br />
	UCHAR* lpBuffer = NULL;<br />
	BOOL ret = FALSE;<br />
<br />
	HANDLE handle;<br />
	HANDLE hRemoteProcess;<br />
	BOOL remote = processId != GetCurrentProcessId();<br />
	DWORD dwId = 0;<br />
	HANDLE hHeap;<br />
	<br />
<br />
	if ( remote )<br />
	{<br />
		hRemoteProcess = ::OpenProcess( PROCESS_DUP_HANDLE, TRUE, processId );<br />
<br />
		handle = DuplicateHandle( hRemoteProcess, h );<br />
	}<br />
	else<br />
		handle = h;<br />
<br />
	// let's be happy, handle is in our process space, so query the infos :)<br />
<br />
	ret = GetFileNameHelper( handle, str );<br />
<br />
	INtDll::NtQueryObject ( handle, 1, NULL, 0, &size );<br />
<br />
	lpBuffer = (UCHAR*)HeapAlloc(hHeap = GetProcessHeap(), 0, sizeof(UCHAR)*size);<br />
<br />
	if ( INtDll::NtQueryObject( handle, 1, lpBuffer, size, NULL ) == 0 )<br />
	{<br />
		SystemInfoUtils::Unicode2CString( (UNICODE_STRING*)lpBuffer, str );<br />
		ret = TRUE;<br />
	}<br />
	<br />
	<br />
	return ret;<br />
}<br />
<br />
#define FILE_NAME_INFORMATION 9<br />
<br />
//File related functions<br />
void __cdecl SystemHandleInformation::GetFileNameThread( PVOID pParam ) throw()<br />
{<br />
	// This thread function for getting the filename<br />
	// if access denied, we hang up in this function, <br />
	// so if it times out we just kill this thread<br />
	GetFileNameThreadParam* p = (GetFileNameThreadParam*)pParam;<br />
<br />
	UCHAR *lpBuffer = (UCHAR*)<br />
				VirtualAlloc( NULL, 0x1000*sizeof(UCHAR), MEM_COMMIT, PAGE_READWRITE );<br />
	DWORD  iob[2];<br />
	<br />
	p->rc = INtDll::NtQueryInformationFile( p->hFile, iob, lpBuffer, sizeof(lpBuffer), FILE_NAME_INFORMATION );<br />
<br />
	if ( p->rc == 0 )<br />
		*p->pName = lpBuffer;<br />
	<br />
	VirtualFree( lpBuffer, 0, MEM_RELEASE );<br />
}<br />
<br />
// handle must be in own address space<br />
BOOL SystemHandleInformation::GetFileNameHelper( HANDLE handle, CString& str, DWORD processId ) throw()<br />
{<br />
	BOOL ret=FALSE;<br />
	HANDLE hThread;<br />
	GetFileNameThreadParam tp;	<br />
<br />
<br />
	tp.hFile = handle;<br />
	tp.pName = &str;<br />
	tp.rc = 0;<br />
<br />
	// Let's start the thread to get the file name<br />
	hThread = (HANDLE)_beginthread( GetFileNameThread, 0, &tp );<br />
<br />
	if ( hThread == NULL )<br />
		goto cleanup;<br />
<br />
	// Wait for finishing the thread<br />
	if ( WaitForSingleObject( hThread, 80 ) == WAIT_TIMEOUT )<br />
	{	<br />
		// Access denied<br />
		// Terminate the thread<br />
		TerminateThread( hThread, 0 );<br />
<br />
		str = _T("");<br />
<br />
		ret = TRUE;<br />
<br />
	}<br />
	else<br />
		ret = ( tp.rc == 0 );<br />
		<br />
	return ret;<br />
}<br />

GeneralRe: TerminateThread hangs Pin
Mark Salsbery24-Nov-07 8:06
Mark Salsbery24-Nov-07 8:06 
GeneralRe: TerminateThread hangs [modified] Pin
maciu202024-Nov-07 8:47
maciu202024-Nov-07 8:47 
GeneralRe: TerminateThread hangs Pin
Mark Salsbery24-Nov-07 9:42
Mark Salsbery24-Nov-07 9:42 
GeneralRe: TerminateThread hangs Pin
maciu202024-Nov-07 10:15
maciu202024-Nov-07 10:15 
GeneralRe: TerminateThread hangs Pin
Mark Salsbery24-Nov-07 10:28
Mark Salsbery24-Nov-07 10:28 
GeneralRe: TerminateThread hangs Pin
maciu202024-Nov-07 22:27
maciu202024-Nov-07 22:27 
QuestionCan VC realize the localizable dialog ? Pin
BegtostudyBoy23-Nov-07 14:04
BegtostudyBoy23-Nov-07 14:04 
AnswerRe: Can VC realize the localizable dialog ? Pin
Nelek25-Nov-07 21:26
protectorNelek25-Nov-07 21:26 
GeneralRe: Can VC realize the localizable dialog ? Pin
BegtostudyBoy25-Nov-07 22:00
BegtostudyBoy25-Nov-07 22:00 
QuestionI have put PlaySound() in the View - and it dosent play any thing until at rest Pin
simon alec smith23-Nov-07 12:53
simon alec smith23-Nov-07 12:53 
QuestionRe: I have put PlaySound() in the View - and it dosent play any thing until at rest Pin
Mark Salsbery23-Nov-07 15:28
Mark Salsbery23-Nov-07 15:28 
AnswerRe: I have put PlaySound() in the View - and it dosent play any thing until at rest Pin
simon alec smith24-Nov-07 4:00
simon alec smith24-Nov-07 4:00 
GeneralRe: I have put PlaySound() in the View - and it dosent play any thing until at rest Pin
Mark Salsbery24-Nov-07 7:12
Mark Salsbery24-Nov-07 7:12 
QuestionMFC ON_COMMAND problem Pin
FredrickNorge23-Nov-07 10:19
FredrickNorge23-Nov-07 10:19 
QuestionRe: MFC ON_COMMAND problem Pin
Mark Salsbery23-Nov-07 10:23
Mark Salsbery23-Nov-07 10:23 
AnswerRe: MFC ON_COMMAND problem Pin
FredrickNorge23-Nov-07 10:27
FredrickNorge23-Nov-07 10:27 
GeneralRe: MFC ON_COMMAND problem Pin
Mark Salsbery23-Nov-07 10:32
Mark Salsbery23-Nov-07 10:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.