Shared Memory

Hello all,

I have a question about shared memory and I was looking for further understanding.
I am making a program that uses shared memory on Win 32 API and I am having a problem with the CreateFile function. I understand that the first argument to this function is the file name but I am getting the red squiggly line under "temp.txt". I have a general understanding of (int argc, char *argv[]) but I still don't know why I have this error.
If any one could take a look and add some advice I would appreciate it.

This is a homework question. With that being said, is there a separate place for this post?

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <windows.h>
#include <stdio.h>
#define SIZE 256

int factorial( int num ) 
{
     if ( num == 0 ) 
        return 1;
     else
         return num * factorial( num-1 );
}
// returns the nth catalan numbers
int catalan( int n ) 
{    
    return( factorial( 2 * n ) / ( factorial( n + 1 ) * factorial( n ) ) );
}

int main( int argc, char *argv[] )
{
	HANDLE hFile;
	HANDLE hMapFile;
	LPVOID mapAddress;
	char series[SIZE];
	char buf[SIZE];

	int i;
	int seriesSize = 0;
	seriesSize = atoi( argv [ 1 ] );
    
	// first create/open the file
	hFile = CreateFile( "temp.txt", // this is my error //
		GENERIC_READ | GENERIC_WRITE,
		0,
		NULL,
		OPEN_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL );

	if( hFile == INVALID_HANDLE_VALUE ) 
	{
   		fprintf( stderr,"Could not open file temp.txt (%d).\n",GetLastError() );
   		return -1;
	}

	// now obtain a mapping for it
	hMapFile = CreateFileMapping( hFile,
					NULL,
					PAGE_READWRITE,
					0,
					0,
					TEXT("SharedObject") );

	if( hMapFile == NULL ) 
	{
		fprintf(stderr,"Could not create mapping (%d).\n", GetLastError());
   		return -1;
	}

	// now establish a mapped viewing of the file
	mapAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 );

	if( mapAddress == NULL ) 
	{
		printf( "Could not map view of file (%d).\n", GetLastError() );
		return -1;
	}

	// write to shared memory
	for( i = 1; i <= seriesSize; i++ ) 
	{
        sprintf( buf, "%d, ", catalan(i) );
        strcat( series, buf );
        //printf( "**%s**", buf);
    }
        
    sprintf((char *)mapAddress, "%s\n", series);
    //sprintf((char *)mapAddress,"%s","Shared memory message");

	while (1);
	// remove the file mapping
	UnmapViewOfFile(mapAddress);

	// close all handles
	CloseHandle(hMapFile);
	CloseHandle(hFile);
}
Perhaps you are using the wide character version of the win32 api.

You should be ok with
either: hFile = CreateFileA( "temp.txt", // const char*
or: hFile = CreateFileW( L"temp.txt", // const wchar_t*

Or you could use the _T or TEXT macro to have it define the correct type automatically.

hFile = CreateFile(TEXT("temp.txt"), // type determined by unicode settings
Last edited on
Topic archived. No new replies allowed.