I am having a very strange problem with the following code. Weirdest thing, the code worked before just fine, I didn't change anything.
I am running Windows 7 Professional 64 bit. The code below worked fine on it before.
With the following code I am trying to get the handle to the primary thread.
GetMainThread Frame
Local ourPID:D
Local hProcessSnap:D
Local te32:THREADENTRY32
Invoke GetCurrentProcessId
mov [ourPID], Eax
Invoke CreateToolhelp32Snapshot, TH32CS_SNAPTHREAD, [ourPID]
Mov [hProcessSnap], Eax
mov D[te32.dwSize], SIZEOF THREADENTRY32
Invoke Thread32First, [hProcessSnap], Addr te32 ;First handle found is usually the main thread
Test Eax, Eax
Jnz > FoundThreadID
W1THREAD:
Invoke Thread32Next, [hProcessSnap], Addr te32
Test Eax, Eax
Jz >> L2THREAD
FoundThreadID:
Mov Eax, [te32.th32ThreadID] //Always zero??
invoke OpenThread, THREAD_SET_CONTEXT | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, NULL, Eax
Mov [SteamhThread], Eax
Ret
L2THREAD:
Xor Eax, Eax
Ret
Endf
The main problem Thread32First works, returns 1, but the buffer is not filled correct. So when grabbing: [te32.th32ThreadID] and copying it to Eax it always copies the value 0.
Anyone has any idea why that problem occurs. The code below is posted by Donkey before and I have been using it a lot on my project.
It's just on this new project it doesn't work anymore??
you should close the handle on hProcessSnap when done (CloseHandle)
the snapshot may require a lot of system resources
No idea why, but it looks like you still need to walk through the list recorded in the snapshot:
jz > L2THREAD
W1THREAD:
mov eax,[te32.th32OwnerProcessID]
cmp eax,[ourPID]
je >FoundThreadID ;First handle found is usually the main thread
INVOKE Thread32Next, [hProcessSnap], Addr te32
test eax, eax
jnz <W1THREAD
jmp >L2THREAD
Instead of doing all of that, it seems like you can just call the GetCurrentThreadId function and use that in OpenThread...