Guest User

Untitled

a guest
Jan 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. DWORD WINAPI mHandler(LPVOID sId) {
  2. ...
  3. arr[(int)sId]
  4. ...
  5. }
  6.  
  7. int id=1;
  8. CreateThread(NULL, NULL, mHandler, (LPVOID)id, NULL, NULL);
  9.  
  10. warning C4311: 'type cast' : pointer truncation from 'LPVOID' to 'int'
  11. warning C4312: 'type cast' : conversion from 'int' to 'LPVOID' of greater size
  12.  
  13. // parameter on the heap to avoid possible threading bugs
  14. int* id = new int(1);
  15. CreateThread(NULL, NULL, mHandler, id, NULL, NULL);
  16.  
  17.  
  18. DWORD WINAPI mHandler(LPVOID sId) {
  19. // make a copy of the parameter for convenience
  20. int id = *static_cast<int*>(sId);
  21. delete sId;
  22.  
  23. // now do something with id
  24. }
  25.  
  26. DWORD WINAPI mHandler(LPVOID p)
  27. {
  28. INT_PTR id=reinterpret_cast<INT_PTR>(p);
  29. }
  30. ...
  31.  
  32. INT_PTR id = 123;
  33. CreateThread(NULL, NULL, mHandler, reinterpret_cast<LPVOID>(id), NULL, NULL);
  34.  
  35. static_assert(sizeof(int) <= sizeof(LPVOID),
  36. "Use new int() - cannot fit int to LPVOID");
  37. int id = 7;
  38. LPVOID idP = 0;
  39. memcpy(&idP, &id, sizeof(id));
  40. CreateThread(NULL, NULL, mHandler, idP, NULL, NULL);
  41.  
  42. void mHandle(..., LPVOID idP, ...)
  43. {
  44. static_assert(sizeof(int) <= sizeof(LPVOID),
  45. "Use new int() - cannot fit int to LPVOID");
  46. int id;
  47. memcpy(&id, &idP, sizeof(id));
  48. }
Add Comment
Please, Sign In to add comment