C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-26-2010, 01:33 AM   #1
Registered User
 
Join Date: Dec 2008
Posts: 5
WindowsXP FileMapping problem,help!!

hello,
When I want to use the shared memory, I got a problem.
1.I wrote a server to create a piece of shared memory. Source is below:
// shmsv.cpp
HANDLE hMapFile = NULL;
LPVOID pVoid = NULL;

hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
BUF_SIZE, // 1024
SHM_NAME);



pVoid = MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);

memset(pVoid, '*', BUF_SIZE);
char *szMsg = "Hello shared memory.";
CopyMemory(pVoid, szMsg, strlen(szMsg));

2.I wrote another program to read message from the shared memory. Source is below:
// readFrom.cpp
HANDLE hFileMap = NULL;
LPVOID pVoid = NULL;

hFileMap = OpenFileMapping(FILE_MAP_READ,
FALSE,
SHM_NAME);

pVoid = MapViewOfFile(hFileMap,
FILE_MAP_READ,
0,
0,
8);
//BUF_SIZE);
printf("message from shared memory is: %s.\n", (char *)pVoid);

3.Here comes the probelm: I only want to output the 8 bytes' info from the shared memory.
But the "readFrom" program outputed all the message from the shared memory.

4.If I only want to output the 8 bytes' info from the shared memory, what should I do?

regards,
yogo
wal_sug is offline   Reply With Quote
Old 01-26-2010, 07:05 AM   #2
Registered User
 
Join Date: Jan 2010
Posts: 231
Quote:
Originally Posted by wal_sug View Post
3.Here comes the probelm: I only want to output the 8 bytes' info from the shared memory.
But the "readFrom" program outputed all the message from the shared memory.
printf will keep on printing until it either finds a null character, or throws an access violation from trying to reading memory you don't have read access to. Since you are not null-terminating the string or using exception handling you're basically just waiting for a crash to happen
Code:
char *szMsg = "Hello shared memory.";
CopyMemory(pVoid, szMsg, strlen(szMsg));
strlen returns the length of a string without counting the terminating 0. Use strlen(szMsg)+1 instead.

Quote:
Originally Posted by wal_sug View Post
4.If I only want to output the 8 bytes' info from the shared memory, what should I do?
You can either copy 8 chars to a new string, or if you don't care about corrupting the original data just insert a NULL at the 9th position.
_Mike is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The C system command with long filenames in windowsXP esbo C Programming 30 09-12-2006 07:06 PM
windowsxp networking. xddxogm3 Tech Board 10 10-05-2004 12:18 PM
WindowsXP themes! compjinx A Brief History of Cprogramming.com 11 10-31-2002 06:18 AM
WindowsXP Look Crux++ Windows Programming 10 04-08-2002 04:06 PM
WindowsXP Security Dude101 A Brief History of Cprogramming.com 19 10-03-2001 01:22 PM


All times are GMT -6. The time now is 08:02 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22