?
Solved

File Mapping code example for some Array in Windows c++

Posted on 2013-01-28
5
Medium Priority
?
832 Views
Last Modified: 2013-12-14
I have some array and want to maintain it in File Mapping.
Need example for such in Windows c++.
0
Comment
Question by:longjumps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Accepted Solution

by:
jiangsheng earned 750 total points
ID: 38828350
0
 
LVL 1

Author Comment

by:longjumps
ID: 38830223
"jiangsheng", I downloaded the package from http://sourceforge.net/projects/boost/files/boost/1.52.0/boost_1_52_0.zip/download

extracted to some dir, but once compile get error:
...
Generating Code...
cl : Command line error D8027 : cannot execute 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\BIN\c2.dll'

I added permission for Everyone for Full, but same error happens.

Please advice.
0
 
LVL 1

Author Comment

by:longjumps
ID: 38872338
Is there another framework for File mapping except of Boost?
I tried Boost on performance and once I adding and deleting for same structure, from different processes I have a lock and stuck in processes.
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 750 total points
ID: 38953791
you can use native file mapping:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

#define ARR_SIZE 256

typedef struct tagS
{
    int  i;
    char t[20];
} S;

enum
{
    create_Arr,
    add_S,
    get_S_at,
    free_Arr,
    // more 
};

int HandleArray(int mode, int idx, S* ps)
{
   static HANDLE hMapFile;
   static S *    pSArr = NULL;
   static char   szName[]="Global\\MyFileMappingObject";
   static int    numElements = 0;

   switch (mode)
   {
       case create_Arr:
       {
           int    totalSiz = ARR_SIZE * sizeof(S);

           hMapFile = CreateFileMapping(
                         INVALID_HANDLE_VALUE,    // use paging file
                         NULL,                    // default security
                         PAGE_READWRITE,          // read/write access
                         0,                       // maximum object size (high-order DWORD)
                         totalSiz,                // maximum object size (low-order DWORD)
                         szName);                 // name of mapping object

           if (hMapFile == NULL)
           {
              printf("Could not create file mapping object (%d).\n", GetLastError() );
              return 1;
           }
           pSArr = (S *) MapViewOfFile(hMapFile,   // handle to map object
                                FILE_MAP_ALL_ACCESS, // read/write permission
                                0,
                                0,
                                totalSiz);

           if (pSArr == NULL)
           {
               printf("Could not map view of file (%d).\n", GetLastError());

               CloseHandle(hMapFile);

               return 1;
           }
       }
       break;

       case add_S:
       {
           if (pSArr != NULL && ps != NULL && numElements < ARR_SIZE-1)
           {
               memcpy(pSArr+numElements, ps, sizeof(S));
               numElements++;
           }
       }
       break;

       case free_Arr:
       {
           UnmapViewOfFile(pSArr);
           pSArr = NULL;
           CloseHandle(hMapFile);
           hMapFile = NULL;
       }
       break;
   }
   return 0;
}

int main()
{
    S s = { 0 };
    if (HandleArray(create_Arr, 0, NULL) != 0)
        return 1;
    s.i = 1;
    strcpy(s.t, "Text1");
    if (HandleArray(add_S, 0, &s) != 0)
        return 2;
    // add here more calls
    if (HandleArray(free_Arr, 0, NULL) != 0)
        return 99;
    return 0;
}

Open in new window


Sara
0
 
LVL 1

Author Closing Comment

by:longjumps
ID: 39056412
not exact
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question