ThreadID to Thread Handle under Windows 9x

Windows 9x does not support the OpenThread function.  I obtain the Thread ID via calls to CreateToolhelp32Snapshot, Process32First, Module32First, Thread32First....

But I need to get a handle to the thread, instead of the ID.  I can use OpenProcess onthe ProcessID, but still need the thread handle.  I am probly overlooking something...

I need the same kind of handle that you get when you do a CreateProcess...the handle stored in the process information block.

Thanks.
lordxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Is this a thread within your own application?
0
lordxAuthor Commented:
No, the thread is in another application.  I obtain the process and thread id's via the functions above.  From there I am calling other functions that need the process and thread handles..not the id's.  

Normally, I can get these handles via a CreateProcess call...but thats when I create the process from inside my program.  I need to get the handles from programms already running.
0
chensuCommented:
There is no way to get the thread handle from the thread ID. For more information, see

INFO: Thread Handles and Thread IDs
http://support.microsoft.com/support/kb/articles/q127/9/92.asp
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

lordxAuthor Commented:
What about using DebugActiveProcess?
I know the MS article said it is not possible to get the thread handle...but the information is available on the system.  It is avaiable when the process is created (in the process information block), and unless it is destroyed, it should be somewhere.

0
jkrCommented:
'DebugActiveProcess()' works for this, as the debugger will be notified about every active thread. However, there's no way to detach from the debuggee. Alternatively, I'd suggest loading a DLL into the process' address space, as the DLL will be notified also via 'DLL_THREAD_ATTACH'...
0
lordxAuthor Commented:
Well, there's part of the problem.  I am using code to 'inject' a DLL into another process.  I am doing it to intercept the winsock calls...specifically the send and recieve calls so I can monitor the amount of bandwidth that is being used by an application.

But, in order for me to inject the DLL into the process, I need the handle for both the thread and the process.  I have the one for the process via OpenProcess, but not for the thread.
If I am not able to get a solution, I will use CreateProcess and force my users to start their program via my program...but ideally I would like them to be able to just run my program, and have it automatically inject its self into the other apps.
0
jkrCommented:
0
MadshiCommented:
There is a way to open a thread under win9x, but it's quite hacky. Do you know about the magic number (called obfuscator)?

Regards, Madshi.
0
lordxAuthor Commented:
jkr: I read over the hook information in the link you provided.  I can probly use that information for other projects...but I didn't see an obvious way of obtaining the thread handle (unless I missed something).  The closest one in the hooks list is WH_DEBUG...but it still only gives the ThreadID.  And it would rely on an existing hook in the thread I am tring to get information on.

Madshi: I do not know about the 'magic number'.  And until now I have not heard of 'obfuscator'...but am looking it up on the net.  I am willing to try any ideas that you have.

If it is necessary, I can increase the point value of the question (double, trippe...whatever).  Getting a solution is important, and you will be compensated for your help.  So, if your solution does what I need...let me know how much it is worth to you.
0
MadshiCommented:
Well, I can give you complete sources for obtaining a thread handle from a thread ID under win9x - but in Delphi!! If you can live with that: Okay. But you should know that it is very hacky and will perhaps not work on Windows Millennium (I have no beta version yet). At least it works under all other win9x versions.
About the points: If you're satisfied with my code you can look how complicated it is and then decide how much it's worth for you...   :-)

If you are interested, give me your eMail address. The code is a bit too long to post here...
0
MadshiCommented:
P.S: Or write to "madshi@gmx.net".
0
jkrCommented:
>>but I didn't see an obvious way of obtaining the thread
>>handle

;-)

extern "C" int APIENTRY DllMain (   HINSTANCE   hInstance,
                                    DWORD       dwReason,
                                    LPVOID      lpReserved
                                )
{
    if  (   dwReason    ==  DLL_THREAD_ATTACH)
        {
            dwTID   =   GetCurrentThreadId  ();
            hThread =   GetCurrentThread    ();

            //  use 'DuplicateHandle()' on this thread handle and store
            //  it away with the TID...
        }

    return  (   bRC);
}

0
MadshiCommented:
Jürgen, lordx needs the handle IN ORDER TO inject the dll (at least that's how I understood him), not AFTER he injected the dll...   :-)

Regards, Madshi.
0
jkrCommented:
I know - but it's much easier to install a hook in order to get the handle ;-)

(WOW, I just received the notification on your last comment - 12 HOURS LATER!!! .... Now THAT'S interactivity!)
0
jkrCommented:
By the way - why don't you use Jeffrey Richter's 'injlib' to perform the task?

See http://www.microsoft.com/msj/backissuesindex.htm and download msjmay94.zip ...
0
jkrCommented:
Ooops, MS likes to change URLs :-(

Try  ftp://ftp.monash.edu.au/pub/msj/newfiles/MSJMAY94.ZIP instead...
0
MadshiCommented:
Sorry to disappoint you, Jürgen, but Jeffrey Richter's stuff works only for winNT... It uses CreateRemoteThread which isn't available for win9x.   :-(
0
lordxAuthor Commented:
Madshi provided me with the code I was looking for.
0
MadshiCommented:
Thanx for the points...   :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lordxAuthor Commented:
Thanks for the help.
0
hakanbCommented:
Hi madshi,

I have gone thru the comments and the source code you have provided for lordx is the one I have needed.  Could you also send me the source? I am willing to give you points for it.

Thanks.
hakanb@intermat.com.tr
BTW: You can tell me howmany points.
0
MadshiCommented:
hakan, did you get the sources? I've sent you the stuff and didn't get any response yet!
0
Romi KuntsmanBig Data EngineerCommented:
Madshi, can you please post the code here?
0
MadshiCommented:
romi_k, to be honest, I was not too happy with the points I got for this one. I wrote in one comment (when lordx asked me about the points before he had accepted the answer):

>> About the points: If you're satisfied with my code you can look how complicated it is and then decide how much it's worth for you...   :-)

And I thought lordx would raise the points. Well, he didn't and I didn't explicitly say he has to. So it was more or less okay for this question. But to be honest I think my solution is worth much more than those 200 points, since I don't know anyone else who knows a way to open a thread in win9x. AFAIK I'm the only one...  :-)  So I'd rather not post it here. Besides, it's too long to post here... (And furthermore I've worked over it since January 2000 and now it works even for win95 with usb extension (didn't work before) and for winME...)

Regards, Madshi.
0
lordxAuthor Commented:
I appologize for not giving you a fair number of points for your answer.  Please accept the points in the windows programming section, since I am unable to change the points after the accepting the answer (this late).  
0
MadshiCommented:
Hi lordx,

well I didn't complain, because I didn't tell you explicitly that I wanted more points. It was my fault.
So the more I thank you very much for the additional points, you're really fair!   :-)

Regards, Madshi.
0
lordxAuthor Commented:
Well, I am sure you put time into developing the code...be it for your own use or to help someone else.  I have come to EE many times over the past few years...and asked questions that I didn't think I could ever get an answer too.  But to my suprise, someone allways replied (generally within a few hours).  My time, like yours, is valuable...and I think you should be compensated for it.

Everytime I get a "there is no way to do that" answer I think it needs to be challenged.  The data/information is there, its just a matter of knowing how to manipulate or gain access to it.

Anyways, thanks for your help.

l8r
LX
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.