?
Solved

Who knows USB driver in WinCE?

Posted on 2000-03-09
4
Medium Priority
?
249 Views
Last Modified: 2013-12-27
Hello,

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;
    return(pMouse->MouseThread());
}

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
Thanks
zouying
0
Comment
Question by:zouying
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Expert Comment

by:CharlesDHManning
ID: 2614922
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.

ie.

stubfunction()
{
  objectPtr->InterruptMethod();
}


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.

0
 

Author Comment

by:zouying
ID: 2615186
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?
Zouying
0
 

Accepted Solution

by:
CharlesDHManning earned 400 total points
ID: 2617372
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).

  CD12Pointer->MouseThread()

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
  this->MouseThread();

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.





0
 

Expert Comment

by:CharlesDHManning
ID: 2621197
Does my answer clarify things?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question