Click here to Skip to main content
15,921,113 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Unistaller Pin
Alexander M.,15-Jun-05 9:28
Alexander M.,15-Jun-05 9:28 
GeneralEnter and Esc Pin
Alexander M.,15-Jun-05 5:39
Alexander M.,15-Jun-05 5:39 
GeneralRe: Enter and Esc Pin
David Crow15-Jun-05 5:41
David Crow15-Jun-05 5:41 
GeneralRe: Enter and Esc Pin
myth767615-Jun-05 7:23
myth767615-Jun-05 7:23 
GeneralRe: Enter and Esc Pin
Alexander M.,15-Jun-05 7:39
Alexander M.,15-Jun-05 7:39 
GeneralRe: Enter and Esc Pin
GKarRacer15-Jun-05 13:37
GKarRacer15-Jun-05 13:37 
GeneralRe: Enter and Esc Pin
Alexander M.,16-Jun-05 7:01
Alexander M.,16-Jun-05 7:01 
GeneralSharing memory with MapViewOfFile Pin
metal_rob15-Jun-05 5:09
metal_rob15-Jun-05 5:09 
Hey everyone, I'm a little stuck with this and this is my fist question on these boards, and any advice anyone could give would be greatly appreciated Smile | :)

So basically, I have these two COM components that share memory using a MapViewOfFile object... like so:

-----------------------------------------------------------------
In the "server" component:
HANDLE hBlockMap;
...
hBlockMap = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0,
sizeof(Block), "SharedBlockMemory");

In the "client" component:
HANDLE hBlockMap;
...
hBlockMap = ::OpenFileMapping(FILE_MAP_WRITE, FALSE, "SharedBlockMemory");
-----------------------------------------------------------------

I also have a simple "Block" object, just to test sharing data, which looks like this:

-----------------------------------------------------------------
class Block
{
public:
void Write(char* msg);
char* Read();
int getSize() { return n_size; }

Block();
~Block();

private:
BYTE* data;
int n_size;
};

and just the Write and Read methods:

void Block::Write(char* msg, int size)
{
data = new BYTE[strlen(msg) + 1];
memcpy(data, (BYTE* )msg, strlen(msg));

n_size = size;
}

char* Block::Read(int size)
{
char* result = new char[n_size + 1];
memcpy(result, (char* )data, n_size);

return result;
}
-----------------------------------------------------------------

So basically, my problem is this:
The Write and Read methods both actually "work", and by that I mean I'm not getting any wierd memory access violations... If I expand the write method to check and make sure its writing, like so,

void Block::Write(char* msg)
{
...
n_size = size;

char* check = new char[strlen(msg) + 1];
memcpy(check, (char* )data, strlen(msg));

check[strlen(msg)] = '\0';

cout << check << endl;
delete[] check;
}

It outputs the correct string. However, no matter what I seem to do, the Read method just outputs garbage... I'm completely mystified, because if I change, in the Block class, the definition of data to

BYTE data[250];

and then change my Write and Read method to deal with that accordingly, I get the correct output in the Read method. But if use BYTE* data, I just get garbage...

Am I just missing something small, maybe some syntactical error? I haven't programmed in C++ as much lately, and I wouldn't put that myself, but I'm fairly sure my code dealing with the pointers and copying memory is correct... Or is it something else, could I be using the MapViewOfFile incorrectly?

And also, here is an example of how I read from it, just to demonstrate that access to the shared memory is synchronized (using a seperate mutex)...

-----------------------------------------------------------------
class CMemoryMgr
{
public:
...

private:
Block* c_Block;
...
};

void __stdcall CMemoryMgr::TPF_PrintMessage()
{
if(::WaitForSingleObject(hBlockMutex, 5000L) == WAIT_OBJECT_0)
{
cout << c_Block->Read(c_Block->getSize());
cout << endl;

::ReleaseMutex(hBlockMutex);
}
}
-----------------------------------------------------------------
Also, the reason I want to dynamically allocate the data array is because I will be developing a customized circular buffer, and I want to be able to allow data of any reasonable size to be enqueued, so thats why I'm trying to stay away from a fixed array size.


Anyway, thank you all for reading my problem!
Like I said, any advice would be greatly appreciated Smile | :)
GeneralRe: Sharing memory with MapViewOfFile Pin
Blake Miller15-Jun-05 8:02
Blake Miller15-Jun-05 8:02 
GeneralDFT of image data using FFT Pin
Aqiruse15-Jun-05 4:52
sussAqiruse15-Jun-05 4:52 
GeneralEnhanced Metafiles Pin
bkphat15-Jun-05 4:08
bkphat15-Jun-05 4:08 
GeneralRe: Icon On Tabctrl Pin
Alexander M.,15-Jun-05 5:36
Alexander M.,15-Jun-05 5:36 
GeneralRe: Icon On Tabctrl Pin
LiYS15-Jun-05 15:24
LiYS15-Jun-05 15:24 
GeneralClosing an application. Pin
mcgahanfl15-Jun-05 3:31
mcgahanfl15-Jun-05 3:31 
GeneralRe: Closing an application. Pin
Bob Stanneveld15-Jun-05 3:48
Bob Stanneveld15-Jun-05 3:48 
GeneralRe: Closing an application. Pin
mcgahanfl16-Jun-05 3:24
mcgahanfl16-Jun-05 3:24 
GeneralRe: Closing an application. Pin
David Crow15-Jun-05 4:50
David Crow15-Jun-05 4:50 
GeneralRe: Closing an application. Pin
GKarRacer15-Jun-05 13:44
GKarRacer15-Jun-05 13:44 
GeneralRe: Closing an application. Pin
mcgahanfl16-Jun-05 3:25
mcgahanfl16-Jun-05 3:25 
GeneralRe: Closing an application. Pin
Toby Opferman15-Jun-05 14:24
Toby Opferman15-Jun-05 14:24 
GeneralRe: Closing an application. Pin
mcgahanfl16-Jun-05 3:26
mcgahanfl16-Jun-05 3:26 
GeneralBuilding question Pin
Lampros Giampouras15-Jun-05 2:49
Lampros Giampouras15-Jun-05 2:49 
GeneralRe: Building question Pin
Cedric Moonen15-Jun-05 3:24
Cedric Moonen15-Jun-05 3:24 
GeneralRe: Building question Pin
normanS15-Jun-05 3:53
normanS15-Jun-05 3:53 
GeneralRe: Building question Pin
Lampros Giampouras15-Jun-05 14:09
Lampros Giampouras15-Jun-05 14:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.