code_junkie

How to pass an interface pointer to a thread

Nov 14th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. IS8Simulation *pis8 = NULL;
  2.  
  3. ...
  4.  
  5. CoCreateInstance(
  6. clsid,
  7. NULL,
  8. CLSCTX_LOCAL_SERVER,
  9. __uuidof(IS8Simulation),
  10. (void **) &pis8);
  11.  
  12.  
  13.  
  14. ...
  15.  
  16. hThread = CreateThread(
  17. NULL,
  18. 0,
  19. SecondaryThread,
  20. //interface pointer pis8
  21. 0,
  22. &dwGenericThreadID);
  23.  
  24. ...
  25.  
  26. DWORD WINAPI SecondaryThread(LPVOID iValue)
  27. {
  28. //using iValue accordingly
  29. //E.g.: iValue->Open
  30.  
  31. hThread = CreateThread(
  32. NULL,
  33. 0,
  34. SecondaryThread,
  35. (LPVOID) pis8
  36. 0,
  37. &dwGenericThreadID);
  38.  
  39. DWORD WINAPI SecondaryThread(LPVOID iValue)
  40. {
  41. ((IS8Simulation*) iValue)->Open();
  42. }
  43.  
  44. void MainThread()
  45. {
  46. IStream* psis8;
  47. HRESULT res = CoMarshalInterThreadInterfaceInStream (IID_IS8SIMULATION, pis8, &psis8);
  48. if (FAILED(res))
  49. return;
  50. hThread = CreateThread(
  51. NULL,
  52. 0,
  53. SecondaryThread,
  54. (LPVOID) psis8
  55. 0,
  56. &dwGenericThreadID
  57. );
  58. }
  59.  
  60. DWORD WINAPI SecondaryThread(LPVOID iValue)
  61. {
  62. IS8Simulation* pis8;
  63. HRESULT res = CoGetInterfaceAndReleaseStream((IStream*) iValue, IID_IS8SIMULATION, &pis8);
  64. if (FAILED(res))
  65. return (DWORD) res;
  66. pis8->Open();
  67. }
Add Comment
Please, Sign In to add comment