Solved

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

Posted on 2013-01-28
5
760 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
  • 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 32

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now