Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-28
5
Medium Priority
?
849 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

598 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