Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #include <stdlib.h>
- #include "windows.h"
- #include "winbase.h"
- #include "Queue.h"
- #define _CRT_SECURE_NO_WARNINGS
- #define _CRT_SECURE_NO_DEPRECATE
- #pragma warning(disable : 4996)
- #pragma comment (lib, "Queue.lib")
- struct queue *q;
- DWORD WINAPI Save_thread (LPVOID lpParam)
- {
- printf("Save process is started\n");
- CONST HANDLE hMutex = (CONST HANDLE)lpParam;
- DWORD dwWaitResult = WaitForSingleObject(hMutex, INFINITE);
- ReleaseMutex(hMutex);
- HANDLE hFileMapping = OpenFileMapping(FILE_MAP_READ, TRUE, L"shared_data.data");
- LPVOID buffer = (LPVOID)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, sizeof(int));
- if (hFileMapping == NULL || buffer == NULL)
- {
- printf("Unexpected Error");
- return 1;
- }
- int new_element = *(LPINT)buffer;
- add_element(q, new_element);
- save(q);
- printf("GOOOOOOOOOOOOOOOOOOOOD");
- ExitThread(0);
- }
- int main()
- {
- /*HMODULE hLib = LoadLibrary(L"Queue.dll");
- if(hLib == NULL)
- {
- printf("Loading is failed\n");
- system("pause");
- return 1;
- }
- typedef void (*importAddFunction)(struct queue *, int);
- importAddFunction add_element = (importAddFunction)GetProcAddress(hLib, "add_element");
- typedef bool (*importPollFunction)(struct queue *, int *);
- importPollFunction poll_element = (importPollFunction)GetProcAddress(hLib, "poll_element");
- typedef void (*importSaveOrLoadFunction)(struct queue *);
- importSaveOrLoadFunction save = (importSaveOrLoadFunction)GetProcAddress(hLib, "save");
- importSaveOrLoadFunction load = (importSaveOrLoadFunction)GetProcAddress(hLib, "load");
- if (add_element == NULL || poll_element == NULL || save == NULL || load == NULL)
- {
- printf("Loading is failed\n");
- system("pause");
- return 1;
- }*/
- q = (struct queue*) malloc(sizeof(struct queue));
- q->head = NULL;
- q->tail = NULL;
- while (1)
- {
- printf("\n---\n1) Add\n2) Poll\n3) Save to file\n4) Load from file\n0) Exit\n");
- int buf;
- scanf("%d", &buf);
- if (buf == 1)
- {
- printf("Element to add = ");
- int new_element;
- int res = scanf("%d", &new_element);
- if (!res)
- {
- printf("Input Error");
- continue;
- }
- //add_element(q, new_element);
- HANDLE hfile = CreateFile(L"shared_data.data", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- HANDLE hFileMapping = CreateFileMapping(hfile, NULL, PAGE_READWRITE, 0, sizeof(int), L"shared_data.data");
- LPVOID buffer = MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, sizeof(int));
- if (hFileMapping == NULL || buffer == NULL)
- {
- printf("Unexpected Error");
- return 1;
- }
- CopyMemory(buffer, &new_element, sizeof(int));
- UnmapViewOfFile(hFileMapping);
- CloseHandle(hfile);
- HANDLE hThread;
- CONST HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
- hThread = CreateThread(NULL, 0, &Save_thread, hMutex, 0, NULL);
- CloseHandle(hThread);
- CloseHandle(hMutex);
- Sleep(5000);
- continue;
- }
- if (buf == 2)
- {
- int res;
- if (poll_element(q, &res)) printf("First element was %d", res);
- else printf("Queue is empty");
- continue;
- }
- if (buf == 3)
- {
- save(q);
- printf("Queue succesfully saved");
- continue;
- }
- if (buf == 4)
- {
- q = (struct queue*) malloc(sizeof(struct queue));
- q->head = NULL;
- q->tail = NULL;
- load(q);
- printf("Queue succesfully loaded");
- continue;
- }
- if (buf == 0) break;
- printf("Input error");
- system("pause");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement