Advertisement
Guest User

Untitled

a guest
May 25th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. void WriteStackToBuf(Stack *stack, char *pBuf) {
  2. CopyMemory((void*) (pBuf+sizeof(int)*0),&stack->max_len, sizeof(int));
  3. CopyMemory((void*) (pBuf+sizeof(int)*1),&stack->top, sizeof(int));
  4. CopyMemory((void*) (pBuf+sizeof(int)*2),&stack->buf, sizeof(int));
  5. for(int i=0;i<stack->max_len;i++){
  6. CopyMemory((void*) (pBuf+sizeof(int)*3+i),&stack->stack[i], sizeof(char));
  7. }
  8. }
  9. Stack ReadStackFromBuf(char *pBuf) {
  10. int max_len,top,buf;
  11. Stack s;
  12. CopyMemory((void*)&max_len,pBuf+sizeof(int)*0, sizeof(int));
  13. CopyMemory((void*)&top, pBuf+sizeof(int)*1,sizeof(int));
  14. CopyMemory((void*)&buf,pBuf+sizeof(int)*2, sizeof(int));
  15.  
  16. s.max_len=max_len;
  17. s.top=top;
  18. s.buf=buf;
  19. char* stack=new char[max_len];
  20. for(int i=0;i<max_len;i++){
  21. CopyMemory((void*)&stack[i],pBuf+sizeof(int)*3+i,sizeof(char));
  22. }
  23. s.stack=stack;
  24. return s;
  25. }
  26. int _tmain(int argc, _TCHAR* argv[]) {
  27.  
  28. HANDLE hEventBufEmpty=CreateEvent(NULL, FALSE, TRUE, _T("Global\\BufEmpty"));
  29. HANDLE hEventBufFull=CreateEvent(NULL, FALSE, FALSE, _T("Global\\BufFull"));
  30. Stack stack;
  31. HANDLE hMapFile = CreateFileMapping(
  32. INVALID_HANDLE_VALUE, // use paging file
  33. NULL, // default security
  34. PAGE_READWRITE, // read/write access
  35. 0, // maximum object size (high-order DWORD)
  36. BUF_SIZE, // maximum object size (low-order DWORD)
  37. FileInMemoryName); // name of mapping object
  38. char* pBuf;
  39. for(;;){
  40. WaitForSingleObject(hEventBufFull, INFINITE);
  41. std::cout<<"ASDASD";
  42. pBuf = (char*) MapViewOfFile(hMapFile, // handle to map object
  43. FILE_MAP_ALL_ACCESS, // read/write permission
  44. 0,
  45. 0,
  46. BUF_SIZE);
  47. stack=ReadStackFromBuf(pBuf);
  48. push(&stack,stack.buf);
  49.  
  50. pBuf = (char*) MapViewOfFile(hMapFile, // handle to map object
  51. FILE_MAP_ALL_ACCESS, // read/write permission
  52. 0,
  53. 0,
  54. BUF_SIZE);
  55. WriteStackToBuf(&stack,pBuf);
  56.  
  57. SetEvent(hEventBufEmpty);
  58.  
  59. }
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement