WinAPI Thread unable to write to memory mapped file -


i trying make 2 threads, 1 writes mapped memory location, , other reading location.
far have made threads , annexed functions them, main problem when first process want write mapped file, throws me error value 6, meaning invalid_handle_exception, can't figure out why since have made program write mapped location in same manner. code, , error thrown @ write_in_mapped function:

#include <iostream> #include <string> #include <windows.h>  using namespace std;  handle hmutex = createmutex(null, false, null);  int write_in_mapped(handle memoryfile) {     cout << memoryfile << endl;      waitforsingleobject(hmutex, infinite);      string message = "";      (int = 0; < 200; i++) {     int = rand() % 10000;     int b = * 2;      message.append(to_string(a)).append(",").append(to_string(b)).append(";"); }  handle mapfile = mapviewoffile(memoryfile, file_map_all_access, 0, 0, 1024); if (mapfile == invalid_handle_value) {     cout << "unable map shared memory 1 - error value:" << getlasterror() << endl;     closehandle(memoryfile);     system("pause");     return false; }  if (size(message)) {     cout << message << endl;     copymemory(mapfile, message.c_str(), strlen(message.c_str()) * sizeof(tchar));     cout << "succesfully loaded numbers!" << endl; } else {     cout << "couldn't create numbers!" << endl;     closehandle(mapfile);     closehandle(memoryfile);     return false; }      releasemutex(hmutex);      cout << "done write_to_mapped" << endl;      return 0; }   int read_from_mapped(tchar name[]) {  cout << name << endl;  waitforsingleobject(hmutex, infinite);  handle openfile = openfilemapping(file_map_all_access, false, name); if (openfile == invalid_handle_value) {     cout << "error @ opening mapped memory 2 - error value:" << getlasterror() << endl;     system("pause");     return false; }  lptstr mapfile = (lptstr)mapviewoffile(openfile, file_map_all_access, 0, 0, 1024); if (mapfile == null) {     cout << "error @ mapping memory 3 file - error value:" << getlasterror() << endl;     closehandle(openfile);     system("pause");     return false; }      string result = string(mapfile).c_str();      cout << result << endl;      releasemutex(hmutex);      return 0; }  int main() {      handle process_1, process_2, memoryfile;     tchar name[] = text("global\\myfilemappingobject");      memoryfile = createfilemapping(0, 0, page_readwrite, 0, 1024, name);      if (memoryfile == invalid_handle_value) {         cout << "error @ opening mapped memory area - " << getlasterror() << endl;      system("pause");     return false; }      process_1 = createthread(null, null, (lpthread_start_routine)write_in_mapped, &memoryfile, null, null);     waitforsingleobject(process_1, infinite);      process_2 = createthread(null, null, (lpthread_start_routine)read_from_mapped, &name, null, null);     waitforsingleobject(process_2, infinite);     closehandle(memoryfile);    closehandle(hmutex);     system("pause");    return 0; } 

your code containing incredible number of mistakes. code 1 big mistake. createfilemapping , openfilemapping return 0 on error not invalid_handle_value . &memoryfile , &name parameters createthread ?!? when need memoryfile , name without & call closehandle(memoryfile); int write_in_mapped(handle memoryfile) , int main() - try 2 time close memoryfile, got invalid handle in write_in_mapped because use &memoryfile - , got invalid_handle_exception in closehandle handle mapfile = mapviewoffile - ?!? , on, , on

nightmare


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -