Click here to Skip to main content
15,912,072 members
Home / Discussions / COM
   

COM

 
GeneralAccess Violation in Release of COM+ object in Release Version Pin
ameysp16-Jun-05 20:01
ameysp16-Jun-05 20:01 
GeneralRe: Access Violation in Release of COM+ object in Release Version Pin
GuimaSun23-Jun-05 9:53
GuimaSun23-Jun-05 9:53 
GeneralRe: Access Violation in Release of COM+ object in Release Version Pin
ameysp23-Jun-05 23:27
ameysp23-Jun-05 23:27 
GeneralAdd RTP filter Pin
mausot16-Jun-05 0:43
mausot16-Jun-05 0:43 
GeneralHelp with IDropTarget Interface really needed Pin
Jim Parsells15-Jun-05 17:29
Jim Parsells15-Jun-05 17:29 
GeneralManipulate CWnd-derived object in ActiveX Control Pin
dhamilton15-Jun-05 12:15
dhamilton15-Jun-05 12:15 
GeneralRe: Manipulate CWnd-derived object in ActiveX Control Pin
dhamilton17-Jun-05 6:57
dhamilton17-Jun-05 6:57 
GeneralSharing Memory between two components with MapViewOfFile... Pin
metal_rob15-Jun-05 5:17
metal_rob15-Jun-05 5:17 
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 IMemoryServer,
public IMemoryClient
{
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 between two components with MapViewOfFile... Pin
GuimaSun23-Jun-05 9:45
GuimaSun23-Jun-05 9:45 
GeneralIPropertyBag who knows?! Pin
Member 115323814-Jun-05 22:29
Member 115323814-Jun-05 22:29 
GeneralRe: IPropertyBag who knows?! Pin
User 21559722-Jun-05 4:50
User 21559722-Jun-05 4:50 
GeneralIPropertyBag who knows?! Pin
Member 115323814-Jun-05 22:28
Member 115323814-Jun-05 22:28 
GeneralStrange ActiveX behaviour. Pin
Jan van den Baard13-Jun-05 8:09
professionalJan van den Baard13-Jun-05 8:09 
Generalactivexscripting Pin
dittygrail12-Jun-05 23:08
dittygrail12-Jun-05 23:08 
GeneralBSTR Pin
pardis11-Jun-05 20:20
pardis11-Jun-05 20:20 
GeneralRe: BSTR Pin
User 21559713-Jun-05 3:44
User 21559713-Jun-05 3:44 
GeneralRe: BSTR Pin
Lim Bio Liong13-Jun-05 23:44
Lim Bio Liong13-Jun-05 23:44 
GeneralRe: BSTR Pin
Get.Well14-Jun-05 1:57
Get.Well14-Jun-05 1:57 
GeneralLooking for Excel ver.9 type library (and how to make it work!) Pin
minette9-Jun-05 3:58
minette9-Jun-05 3:58 
GeneralCopy data to spread sheet component from excel file Pin
Member 20313358-Jun-05 22:34
Member 20313358-Jun-05 22:34 
GeneralCapturing ActiveX events Pin
Haroon8-Jun-05 19:51
Haroon8-Jun-05 19:51 
GeneralSending User Defined Types over COM Interface Pin
ChemmieBro8-Jun-05 4:53
ChemmieBro8-Jun-05 4:53 
GeneralRe: Sending User Defined Types over COM Interface Pin
Andy Moore8-Jun-05 11:06
Andy Moore8-Jun-05 11:06 
GeneralRe: Sending User Defined Types over COM Interface Pin
Lim Bio Liong14-Jun-05 1:16
Lim Bio Liong14-Jun-05 1:16 
GeneralRe: Sending User Defined Types over COM Interface Pin
Logan from Singapore26-Jun-05 23:05
Logan from Singapore26-Jun-05 23:05 

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.