• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Who knows USB driver in WinCE?


I am converting the usbmouse.dll into my own driver. When I study the codes, I can't understand the following codes.

In the init routine,

  m_hThread = CreateThread(0, 0, MouseThreadStub, this, 0, NULL);

ULONG CALLBACK CD12::MouseThreadStub(PVOID context)
    CD12 * pMouse = (CD12 *)context;

And it will call the MouseThread routine. My question is,
why doesn't it call MouseThread directly in CreateThread? Instead, it call a stub routine, in this routine, it invoke the MouseThread?

If I don't make it clear, Let me know
  • 3
1 Solution
This is a common thing to have to do with asynchronous processing in C++. The same thing happens with interrupt service routines.

In C++, the code is executed in the context of the current object (*this). But what happens when an asynchronous execution (eg. independent thread or interrupt service routine) happens? There is no way to know which object is being referred to.

When an interrupt occurs, all the system can do is call a function. If the code is in C++, then the function must call the method of the appropriate object. That's the purtpose od the stub.



As you can see, the CreateThread function passes this in to the function. this is a pointer to the calling object.

The tub function runs and invokes the appropriate method on the object, thus performing the tie up.

zouyingAuthor Commented:
Why don't they put the MouseStub and MouseThread together?

What you explains is: (I can understand so much: ), by the time the MouseStub is invoked, it may not be in the current context, so, we pass a this parameters? But this doesn't fix my above question?

is it?
I stand by my answer.

When you start a thread, it is not in the context of the current object (because it is independent execution).

You can't just call a method of an object without specifying the object (iether implicitely or explicitely) becuase the system needs to know which object you are manipulating. Imagine there were a hundred of these objects which object are you refering to?

When MouseThread runs it needs to be within the context of an object of type CD12 (ie. it needs to be called as follows).


The mechanism above does this. The CreateThead starts  When the thread is created, it starts MouseThreadStub and provides a parameter "this". "this" is the current object.

MouseThreadStub then effectively calls

Now MouseThread knows what object you are refering to.

You can do this because MouseThreadStub is a static method of the class. It does not run in the context of an object.

Does my answer clarify things?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now