Archive for March 26, 2008

GetCurrentThread() returns pseudo handle, not the real handle.

0

Icon Description
For getting our thread handle, we usually call GetCurrentThread() api. But actually it returns a pseudo handle. A pseudo handle, is a special handle which represents the handle of current thread which uses it. Please see the following e.g. for more clarify.

1) Thread A gets its threadID by calling GetCurrentThread and Passed to Thread B.
2) Thread B needs to terminate Thread A. So it calls TerminateThread( handle passed by Thread A ).
3) But instead of terminating Thread A, Thread B will be terminated, because the handle passed by Thread A was pseudo and it will become Thread B’s handle when Thread B uses it.

Debug one GetCurrentThread() call and GetCurrentProcess() call and watch its return values. You can see what they returns are follows,

  • GetCurrentThread() – 0xfffffffe
  • GetCurrentProcess() – 0xffffffff

Ofcourse pseudo… isn’t it ?

Icon How Can I Do It?
For getting a real handle which represents your thread, create a duplicate handle by calling DuplicateHandle(). Please see the code block below.

HANDLE hRealHandle = 0;
DuplicateHandle( GetCurrentProcess(), // Source Process Handle.
                 GetCurrentThread(),  // Source Handle to dup.
                 GetCurrentProcess(), // Target Process Handle.
                 &hRealHandle,        // Target Handle pointer.
                 0,                   // Options flag.
                 TRUE,                // Inheritable flag
                 DUPLICATE_SAME_ACCESS );// Options
// Now the hRealHandle contains a real handle for your thread.

How to skip a class or library from being profiled?

0

Icon Description
We used to run profiler for performance tuning. By using profiler, you can check – function timing, function coverage etc. By default profiler profiles the entire modules. But how can we skip a particular user library or class from profiling?

Icon How Can I Do It?
There is a setting file named profile.ini, where profiler keep its settings.
For skipping a user library just add a line like this.
exclude:myuser.lib

Assume you have a class named CHighPerfomance and want it to be skipped from profiling, then add the following line.
exclude:CHighPerfomance.obj

Icon - Where is it?
The profiler setting file is located at
<VisualStudioDir>\VC98\bin\PROFILER.INI

Go to Top