How to use CopyMemory with integer? API in C++

I use Shared Memory for communication between 2 processes.

Then I use CopyMemory function to send an array to shared memory, and use it again in the second process to read that array from shared memory.

The problem begin when I try to send just 1 integer to shared memory. I cannot fixed correctly

Here's the error code
 
CopyMemory((PVOID)pBuf,n,BUF_SIZE);

n is an integer, contain data (5 for example)
line 72 in first process

and Here's the correct code
1
2
3
CopyMemory((PVOID)pBuf,a,512*(n+1));
printf("\n\nSending Matrix Information...\n");
getch();

a is an array. a[MAX][MAX], with MAX=100

The full Code
First Process
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "stdlib.h"

#define MAX 100
#define BUF_SIZE 256

TCHAR MapObjName[]=TEXT("MyFileMappingObject");

void CreateFile(char *FileName, int a[][MAX], int &n);
void ReadFile(char *FileName, int a[][MAX], int n);
void PrintMatrix(int a[][MAX], int n);

int main()
{
	//Usual Task for 2D Array
	int a[MAX][MAX];
	int n=0;
	CreateFile("input.txt",a,n);
	ReadFile("input.txt",a,n);
	PrintMatrix(a,n);
	//END

	//Windows Programming with API
	HANDLE hMapFile;
	LPCTSTR pBuf;

	//Create File Mapping
	hMapFile = CreateFileMapping
		(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		BUF_SIZE,
		MapObjName
		);
	//End Create File Mapping

	//Check if creation success
	if(hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE)
	{
		printf("\nCannot create file mapping: %d",GetLastError());
		exit(1);
	}
	else
		printf("\nCreate File Mapping success!");

	//Map View of File
	pBuf = (LPCTSTR)MapViewOfFile
		(
		hMapFile,
		FILE_MAP_ALL_ACCESS,
		0,
		0,
		BUF_SIZE
		);

	//Check if Create success
	if(pBuf==NULL)
	{
		printf("\nCannot Create Map View of File: %d",GetLastError());
		exit(1);
	}

	//Send Information to shared memory
	CopyMemory((PVOID)pBuf,a,512*(n+1));
	printf("\n\nSending Matrix Information...\n");
	getch();

	CopyMemory((PVOID)pBuf,n,BUF_SIZE);


	UnmapViewOfFile(pBuf);
	CloseHandle(hMapFile);

	//END
	getch();
	return 0;

}

void ReadFile(char *FileName, int a[][MAX], int n)
{
	int temp;
	FILE *File;
	File=fopen(FileName,"r");
	if(File==NULL)
	{
		printf("\nCannot open %s",FileName);
		printf("\nPause Program and Exiting...");
		system("PAUSE");
		exit(-1);
	}
	fscanf(File,"%d",&n);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
		{
			fscanf(File,"%d",&temp);
			a[i][j]=temp;
		}
	fclose(File);
}

void CreateFile(char *FileName, int a[][MAX], int &n)
{
	FILE *f;
	int ran; //Create Random Matrix
	printf("\n Enter the size of Matrix: ");
	scanf("%d",&n);
	printf("\nDo you want program automatically create a matrix? 1/0: ");
	scanf("%d",&ran);
	if(ran==1)
	{
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				a[i][j]=rand();
		f=fopen(FileName,"w");
		fprintf(f,"%d",n); //Print size of Matrix
		fprintf(f,"\n");
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
				fprintf(f,"%10d",a[i][j]);
			fprintf(f,"\n");
		}
		fclose(f);
	}
	else
	{
		printf("\nEnter your Matrix\n");
		f=fopen(FileName,"w");
		fprintf(f,"%d",n); //Print the size of Matrix
		fprintf(f,"\n");
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				printf("Column [%d] - Row [%d]: ",i,j);
				scanf("%d",&a[i][j]);
				fprintf(f,"%10d",a[i][j]);
			}
			fprintf(f,"\n");
		}
		fclose(f);
	}
}

void PrintMatrix(int a[][MAX], int n)
{
	printf("\nMatrix\n");
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
			printf("%10d",a[i][j]);
		printf("\n");
	}
}



The Second Process
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
#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "stdlib.h"

#define BUF_SIZE 256
#define MAX 100
TCHAR szName[]=TEXT("MyFileMappingObject");
TCHAR size[]=TEXT("SizeOfMatrix");

void PrintMatrix(int a[][MAX], int n);

int main()
{
	HANDLE hMapFile;
	HANDLE hSize;
	LPCTSTR pBuf;
	LPCTSTR pSize;

	hMapFile = OpenFileMapping
		(
		FILE_MAP_ALL_ACCESS, //Read,write access
		FALSE, //do not inherit the name
		szName //name of mapping object
		);

	if(hMapFile==NULL)
	{
		printf("\nCould not open file mapping object: %d\n",GetLastError());
		return 0;
	}

	pBuf = (LPCTSTR) MapViewOfFile
		(
		hMapFile,
		FILE_MAP_ALL_ACCESS,
		0,
		0,
		BUF_SIZE
		);

	if (pBuf== NULL)
	{
		printf("Could not map view of file: %d\n", GetLastError());
		return 0;
	}

	int a[MAX][MAX];
	int n;

	n=6;

	printf("\nReceiving Matrix Information...\n");
	CopyMemory(a,(PVOID)pBuf,512*n);
	getch();

	PrintMatrix(a,n);
	return 0;
}

void PrintMatrix(int a[][MAX], int n)
{
	printf("\nMatrix\n");
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
			printf("%10d",a[i][j]);
		printf("\n");
	}
}


I look for this CopyMemory function on MSDN, not help very much
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Syntax

void CopyMemory(
  __in  PVOID Destination,
  __in  const VOID *Source,
  __in  SIZE_T Length
);

Parameters

Destination [in]

    A pointer to the starting address of the copied block's destination.
Source [in]

    A pointer to the starting address of the block of memory to copy.
Length [in]

    The size of the block of memory to copy, in bytes.

Return value

This function has no return value. 
This: CopyMemory((PVOID)pBuf,&n,BUF_SIZE); // Notice the & before n is the short solution

and Here's the correct code
No! BUF_SIZE is 256 bytes. You're trying to copy 512 bytes that should lead to an access violation.

By the way: you don't need to cast to (PVOID) since PVOID accepts all pointers to non const objects.

I'd recommend to use a struct instead of 'LPCTSTR pBuf'

Like so:
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
struct D
{
	int a[MAX][MAX];
	int n=0;
};
...

hMapFile = CreateFileMapping
		(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		sizeof(D), // Note sizeof()
		MapObjName
		);

...

D *d = (D *)MapViewOfFile
		(
		hMapFile,
		FILE_MAP_ALL_ACCESS,
		0,
		0,
		sizeof(D), // Note sizeof()
		);

...

d->n = 5;


The second process accordingly. The need a shared header file with the definition of D (or whatever you want to name it). So you don't need that dangarous CopyMemory().
Last edited on
Topic archived. No new replies allowed.