Solved

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

Posted on 2013-01-28
5
800 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

756 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