Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Visual Studio 2012 with Windows 10 and having problems passing pointers from one class to another using C++. I have a class:

CSAMT_Intf{} which contains variables and structures. I use this class to create a shared memory segment to save the data. I use the following code to produce a pointer to the shared memory and then pass it to another class.

int main(array<System::String ^> ^args)
{
m_pSharedMemory = new CSAMT_Intf;
m_pSharedMemory->CreateMapping();

m_pSockcomm = new CSAMT_Sock(m_pSharedMemory);
}

The receiving class process the pointer:

CSAMT_Sock::CSAMT_Sock(CSAMT_Intf* pSharedPtr)
{
m_pSharedPtr = pSharedPtr; // defined as CSAMT_Intf *m_pSharedPtr in header file

}

When pSharedPtr is received it contains all the addresses and data correctly but when the equal sign is processed in Windows 7 the data is intact but in Windows 10 some of the data has "error:cannot obtain value"

What I have tried:

I have tried making m_pSharedPtr not a pointer and it does compile and the addressing in Shared Memory looks good but the data doesn't get placed into Shared Memory correctly.
Posted
Updated 26-Nov-18 11:04am
Comments
Member 13218289 26-Nov-18 16:38pm    
The Shared memory contains int's and pointers such as HANDLES and pointer to the structure. The int's are being recognized but anything that is a pointer does not work.

1 solution

It sounds like your issue boils down to address space virtualization. A pointer set up in one address space won't be usable in another address space. In other words, you are sharing the pointers between processes, or applications, which isn't a good approach.

Windows does a better job of shielding programs from each other than it did in Windows 7. You might have to create the objects in shared memory if you wish to make use of them between processes. Or create them in a dll which your other program would also use.

Hope this helps. If you are doing everything in one program, and my analysis is incorrect, you will need to give more details.
 
Share this answer
 
Comments
Member 13218289 26-Nov-18 17:31pm    
This is happening in a single application. I have two different classes within the same application. The shared memory segment is created because it is utilized across many applications and DLLs. That's why I pass the address of the shared memory segment to each of the classes which may use it. This same project has been operating the same way since it was created using Windows XP.
David O'Neil 26-Nov-18 17:44pm    
So you are passing a pointer 'into' your shared memory block, then accessing that shared memory within the same program to retrieve that 'pointer', and then accessing the memory pointed to by that 'pointer.'

What happens if, instead of storing it as a pointer in the shared memory, you reinterpret_cast it and store it as a DWORD (assuming 32 bit) in the shared memory, and then reinterpret_cast that DWORD back to a pointer in your main program once you retrieve it?

Have you tried passing the pointer directly to the receiving class without using the shared memory, and then comparing how that works compared to the shared memory approach?
Rick York 26-Nov-18 20:49pm    
I have been using shared memory extensively since Windows NT first came out and the same code still works today.

Oops - I just noticed the ^s. That implies "managed c++" so never mind. I will not admit to any knowledge of that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900