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.

Recommended Answers

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 …

Jump to Post

All 3 Replies

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.

DWORD WINAPI ThreadProc( LPVOID lpParameter)
{
  // blabla

  return 0;
}

now create an instance of the thread in your program

DWORD dwThreadID;
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)

DWORD dwThreadID;
HANDLE hThread = CreateThread(0,0,ThreadProc,this,0,&dwThreadID);

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:

struct Blah {
    char test[512];
    char testnext[512];
} TestStruct[5];

DWORD WINAPI testproc(LPVOID param) {
  //how would i grab the paramater here?
}

int main(void) {
   memset(TestStruct[0].test,0,sizeof(TestStruct[0].test));
   strncpy(TestStruct[0].test,"blah..",sizeof(TestStruct[0].test)-1);
   DWORD id;
   HANDLE ThreadHandle;
   //i know this is definately wrong, i just added it so you can see what i'm trying to pass to the thread function
   ThreadHandle = CreateThread(0, 0, &testproc, (LPVOID)&TestStruct[0], 0, &id);
   return 0;
}

I've seen examples of this being done but not with an array type structure so i'm stuck.

nevermind.. i figured it out, thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.