ThreadID()

Just starting out? Need help? Post your questions and find answers here.
DarkPlayer
Enthusiast
Enthusiast
Posts: 107
Joined: Thu May 06, 2010 11:36 pm

Re: ThreadID()

Post by DarkPlayer »

Hello,

You have three possibilities:

1. you can use the native CreateThread_() API command, which gives use also the real id
2. on Vista and higher, you can use the GetThreadId() API.
3. you can use these undocumented NT API calls on Windows 2000/XP/Vista/7:

Code: Select all

Structure CLIENT_ID
   UniqueProcess.i;
   UniqueThread.i;
EndStructure

Structure THREAD_BASIC_INFORMATION
  ExitStatus.l;
  TebBaseAddress.i;
  ClientId.CLIENT_ID;
  AffinityMask.l;
  Priority.l;
  BasePriority.l;
EndStructure

Enumeration
  #ThreadBasicInformation
  #ThreadTimes
  #ThreadPriority
  #ThreadBasePriority
  #ThreadAffinityMask
  #ThreadImpersonationToken
  #ThreadDescriptorTableEntry
  #ThreadEnableAlignmentFaultFixup
  #ThreadEventPair
  #ThreadQuerySetWin32StartAddress
  #ThreadZeroTlsCell
  #ThreadPerformanceCount
  #ThreadAmILastThread
  #ThreadIdealProcessor
  #ThreadPriorityBoost
  #ThreadSetTlsArrayAddress
  #ThreadIsIoPending
  #ThreadHideFromDebugger
  #ThreadBreakOnTermination
  #ThreadSwitchLegacyState
  #ThreadIsTerminated
  #ThreadLastSystemCall
  #ThreadIoPriority
  #ThreadCycleTime
  #ThreadPagePriority
  #ThreadActualBasePriority
  #ThreadTebInformation
  #ThreadCSwitchMon
EndEnumeration

Procedure GetThreadIDFromHandle(Handle.i)

  Protected Info.THREAD_BASIC_INFORMATION
 
  If NtQueryInformationThread_(Handle, #ThreadBasicInformation, @Info, SizeOf(Info),0) = 0
    ProcedureReturn Info\ClientId\UniqueThread
  EndIf

EndProcedure
TestCode:

Code: Select all

Procedure Test(a.i)
  Debug GetCurrentThreadId_()
  Repeat
    Delay(10)
  ForEver

EndProcedure

HThread = CreateThread(@Test(),0)

Debug GetThreadIDFromHandle(ThreadID(HThread))
DarkPlayer
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: ThreadID()

Post by cas »

Thanks for this nice post, it solves my problem perfectly. :P
Post Reply