-4

this program is supposed to write to shared memory using win32 API. it is a program given as it is in text book but when i try to execute it it fails. it crashes as i click on execute

the program is supposed to write a string to shared memory

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

int main(int argc, char *argv[])   
{   
  HANDLE hFile, hMapFile;  
  LPVOID lpMapAddress;
  //mapping of memory
  hFile=CreateFile("temp.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);    
  hMapFile = CreateFileMapping(hFile,  NULL, PAGE_READWRITE, 0,0, TEXT("SharedObject"));  
  lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);   
  //writing into shared memory
  sprintf((char*)lpMapAddress,"shared memory writing");   


  UnmapViewOfFile(lpMapAddress);   
  CloseHandle(hFile);  
  CloseHandle(hMapFile);  
}  
Lukas
  • 1,320
  • 12
  • 20

1 Answers1

4

the reason it is crashing is because the file must be created with GENERIC_READ and GENERIC_WRITE access rights since in the CreateFileMapping() function your code specifies PAGE_READWRITE as its third argument(flProtect). This is from MSDN documentation of CreateFileMapping:

The file must be opened with access rights that are compatible with the protection flags that the flProtect parameter specifies

PAGE_READWRITE=>Gives read/write access to a specific region of pages. The file that hFile specifies must be created with the GENERIC_READ and GENERIC_WRITE access rights.

so change

hFile=CreateFile("temp.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);

to

hFile=CreateFile("temp.txt",GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);  

One more thing you can not map a file with a size of zero. Here is from MSDN documentation of CreateFileMapping() function:

If this parameter(dwMaximumSizeLow) and dwMaximumSizeHigh are 0 (zero), the maximum size of the file mapping object is equal to the current size of the file that hFile identifies.

An attempt to map a file with a length of 0 (zero) fails with an error code of ERROR_FILE_INVALID. Applications should test for files with a length of 0 (zero) and reject those files.

If an application specifies a size for the file mapping object that is larger than the size of the actual named file on disk, the file on disk is increased to match the specified size of the file mapping object.

so in your case since the file you are trying to map has initially a size of 0, the CreateFileMapping() function will fail unless you specify the size of a file mapping object in the dwMaximumSizeLow/dwMaximumSizeHigh parameters of CreateFileMapping(). You could do something like this...

HANDLE hFile, hMapFile;  
    LPVOID lpMapAddress;
//mapping of memory
    hFile=CreateFile(L"temp.txt",GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);    
    if(hFile!=INVALID_HANDLE_VALUE)
    {
        char* str="shared data to be written";//the data you want to write to the file
        int strLen=::strlen(str);//get the string length of the data you want to write
        hMapFile = CreateFileMapping(hFile,  NULL, PAGE_READWRITE, 0,strLen, TEXT("SharedObject")); //here you also specify the size of the mapping object to be equal to the size of data you want to write
        if (hMapFile != NULL && hMapFile != INVALID_HANDLE_VALUE) 
        {
            lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);   
            //writing into shared memory

            if(lpMapAddress!=NULL)
                sprintf((char*)lpMapAddress,"%s","shared file write");   
            else
                printf("error");//error message MapViewOfFile() failed

            UnmapViewOfFile(lpMapAddress);  
            CloseHandle(hMapFile); 
        }
        else
            printf("error");//error message CreateFileMapping() failed

        CloseHandle(hFile);  
    }
    else
        printf("error");//error message CreateFile() failed
stackerjoe
  • 98
  • 6
Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24