Solved

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

Posted on 2013-01-28
5
812 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 250 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 34

Assisted Solution

by:sarabande
sarabande earned 250 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

691 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