Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include "windows.h"
  6. #include "winbase.h"
  7. #include "Queue.h"
  8.  
  9. #define _CRT_SECURE_NO_WARNINGS
  10. #define _CRT_SECURE_NO_DEPRECATE
  11.  
  12. #pragma warning(disable : 4996)
  13. #pragma comment (lib, "Queue.lib")
  14.  
  15. struct queue *q;
  16.  
  17. DWORD WINAPI Save_thread (LPVOID lpParam)
  18. {
  19.     printf("Save process is started\n");  
  20.  
  21.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  22.  
  23.     DWORD dwWaitResult = WaitForSingleObject(hMutex, INFINITE);
  24.  
  25.     ReleaseMutex(hMutex);
  26.  
  27.     HANDLE hFileMapping = OpenFileMapping(FILE_MAP_READ, TRUE, L"shared_data.data");
  28.     LPVOID buffer = (LPVOID)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, sizeof(int));
  29.  
  30.     if (hFileMapping == NULL || buffer == NULL)
  31.     {
  32.         printf("Unexpected Error");
  33.         return 1;
  34.     }
  35.    
  36.     int new_element = *(LPINT)buffer;
  37.  
  38.     add_element(q, new_element);
  39.  
  40.     save(q);
  41.  
  42.     printf("GOOOOOOOOOOOOOOOOOOOOD");
  43.  
  44.     ExitThread(0);  
  45. }
  46.  
  47. int main()
  48. {
  49.  
  50.     /*HMODULE hLib = LoadLibrary(L"Queue.dll");
  51.  
  52.     if(hLib == NULL)
  53.     {
  54.         printf("Loading is failed\n");
  55.         system("pause");
  56.         return 1;
  57.     }
  58.  
  59.     typedef void (*importAddFunction)(struct queue *, int);
  60.     importAddFunction add_element = (importAddFunction)GetProcAddress(hLib, "add_element");
  61.  
  62.     typedef bool (*importPollFunction)(struct queue *, int *);
  63.     importPollFunction poll_element = (importPollFunction)GetProcAddress(hLib, "poll_element");
  64.  
  65.     typedef void (*importSaveOrLoadFunction)(struct queue *);
  66.     importSaveOrLoadFunction save = (importSaveOrLoadFunction)GetProcAddress(hLib, "save");
  67.     importSaveOrLoadFunction load = (importSaveOrLoadFunction)GetProcAddress(hLib, "load");
  68.  
  69.     if (add_element == NULL || poll_element == NULL || save == NULL || load == NULL)
  70.     {
  71.         printf("Loading is failed\n");
  72.         system("pause");
  73.         return 1;
  74.     }*/
  75.    
  76.     q = (struct queue*) malloc(sizeof(struct queue));
  77.     q->head = NULL;
  78.     q->tail = NULL;
  79.  
  80.     while (1)
  81.     {
  82.         printf("\n---\n1) Add\n2) Poll\n3) Save to file\n4) Load from file\n0) Exit\n");
  83.         int buf;
  84.         scanf("%d", &buf);
  85.  
  86.         if (buf == 1)
  87.         {
  88.             printf("Element to add = ");
  89.             int new_element;
  90.             int res = scanf("%d", &new_element);
  91.             if (!res)
  92.             {
  93.                 printf("Input Error");
  94.                 continue;
  95.             }
  96.             //add_element(q, new_element);
  97.  
  98.             HANDLE hfile = CreateFile(L"shared_data.data", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
  99.                 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  100.  
  101.             HANDLE hFileMapping = CreateFileMapping(hfile, NULL, PAGE_READWRITE, 0, sizeof(int), L"shared_data.data");
  102.  
  103.             LPVOID buffer = MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, sizeof(int));
  104.  
  105.             if (hFileMapping == NULL || buffer == NULL)
  106.             {
  107.                 printf("Unexpected Error");
  108.                 return 1;
  109.             }
  110.  
  111.             CopyMemory(buffer, &new_element, sizeof(int));
  112.  
  113.             UnmapViewOfFile(hFileMapping);
  114.             CloseHandle(hfile);
  115.  
  116.             HANDLE hThread;
  117.             CONST HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
  118.  
  119.             hThread = CreateThread(NULL, 0, &Save_thread, hMutex, 0, NULL);
  120.  
  121.                 CloseHandle(hThread);
  122.                 CloseHandle(hMutex);
  123.                 Sleep(5000);
  124.  
  125.             continue;
  126.         }
  127.        
  128.         if (buf == 2)
  129.         {
  130.             int res;
  131.             if (poll_element(q, &res)) printf("First element was %d", res);
  132.             else printf("Queue is empty");
  133.             continue;
  134.         }
  135.        
  136.         if (buf == 3)
  137.         {
  138.             save(q);
  139.             printf("Queue succesfully saved");
  140.             continue;
  141.         }
  142.        
  143.         if (buf == 4)
  144.         {
  145.             q = (struct queue*) malloc(sizeof(struct queue));
  146.             q->head = NULL;
  147.             q->tail = NULL;
  148.             load(q);
  149.             printf("Queue succesfully loaded");
  150.             continue;
  151.         }
  152.  
  153.         if (buf == 0) break;
  154.         printf("Input error");
  155.         system("pause");
  156.     }
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement