944,018 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16583
  • C++ RSS
Dec 27th, 2005
0

CreateThread & CreateRemoteThread

Expand Post »
Hi, can someone point me to a good tutorial on using the CreateThread and CreateRemoteThread API?

I've already read the MSDN documentation for both functions, i'm looking for a site that explains the use of both functions in more detail and provides alot of example code, thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
osean is offline Offline
14 posts
since Dec 2005
Dec 27th, 2005
0

Re: CreateThread & CreateRemoteThread

I don't know about CreateRemoteThread -- never used it.

you probably won't find any "tutorials" because there isn't that much to it. Most of the parameters are 0, unless you want more control over the thread.

First create the thread itself -- name it anything you want. If it is a member of c++ class it must be static method.
C++ Syntax (Toggle Plain Text)
  1. DWORD WINAPI ThreadProc( LPVOID lpParameter)
  2. {
  3. // blabla
  4.  
  5. return 0;
  6. }

now create an instance of the thread in your program
C++ Syntax (Toggle Plain Text)
  1. DWORD dwThreadID;
  2. HANDLE hThread = CreateThread(0,0,ThreadProc,0,0,&dwThreadID);
The parameter immediately following ThreadProc is a parameter you want to pass to the thread. So if you want to pass the this pointer (assuming c++ program)
C++ Syntax (Toggle Plain Text)
  1. DWORD dwThreadID;
  2. HANDLE hThread = CreateThread(0,0,ThreadProc,this,0,&dwThreadID);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 28th, 2005
0

Re: CreateThread & CreateRemoteThread

Thanks for your reply,
the main reason why I posted was because i'm specifically having problems with what you mentioned in the last part of your post, passing a paramater to the thread (I thought if people could point me to a site with some examples I could see real examples of it in use and figure it out myself).

Basically, I have an array type structure and I need to pass the object (i think?) associated with it to the thread, like the example code below:
C++ Syntax (Toggle Plain Text)
  1. struct Blah {
  2. char test[512];
  3. char testnext[512];
  4. } TestStruct[5];
  5.  
  6. DWORD WINAPI testproc(LPVOID param) {
  7. //how would i grab the paramater here?
  8. }
  9.  
  10. int main(void) {
  11. memset(TestStruct[0].test,0,sizeof(TestStruct[0].test));
  12. strncpy(TestStruct[0].test,"blah..",sizeof(TestStruct[0].test)-1);
  13. DWORD id;
  14. HANDLE ThreadHandle;
  15. //i know this is definately wrong, i just added it so you can see what i'm trying to pass to the thread function
  16. ThreadHandle = CreateThread(0, 0, &testproc, (LPVOID)&TestStruct[0], 0, &id);
  17. return 0;
  18. }
I've seen examples of this being done but not with an array type structure so i'm stuck.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
osean is offline Offline
14 posts
since Dec 2005
Dec 28th, 2005
0

Re: CreateThread & CreateRemoteThread

nevermind.. i figured it out, thanks anyway.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
osean is offline Offline
14 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: #include custom files
Next Thread in C++ Forum Timeline: please fix the error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC