在Windows 10中传递指针的问题 [英] Problem with passing pointers in windows 10

查看:111
本文介绍了在Windows 10中传递指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 10中使用Visual Studio 2012,并且在使用C ++将指针从一个类传递到另一个类时遇到了问题。我有一个班级:



CSAMT_Intf {}包含变量和结构。我使用这个类创建一个共享内存段来保存数据。我使用以下代码生成指向共享内存的指针,然后将其传递给另一个类。



int main(数组< System :: String ^> ^ args)

{

m_pSharedMemory = new CSAMT_Intf;

m_pSharedMemory-> CreateMapping();



m_pSockcomm =新的CSAMT_Sock(m_pSharedMemory);

}



接收类处理指针:



CSAMT_Sock :: CSAMT_Sock(CSAMT_Intf * pSharedPtr)

{

m_pSharedPtr = pSharedPtr; //在头文件中定义为CSAMT_Intf * m_pSharedPtr



}



当收到pSharedPtr时,它包含所有地址和数据正确但在Windows 7中处理等号时数据完好无损但在Windows 10中有些数据有错误:无法获取价值



我尝试了什么:



我试过让m_pSharedPtr不是一个指针,它确实编译了,共享内存中的寻址看起来不错但是数据没有正确放入共享内存。

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.

推荐答案

听起来你的问题归结为解决空间虚拟化问题。在一个地址空间中设置的指针将不能在另一个地址空间中使用。换句话说,您正在共享进程或应用程序之间的指针,这不是一个好方法。



Windows在保护程序彼此之间做得更好如果您希望在进程之间使用它们,则可能必须在共享内存中创建对象。或者在你的其他程序也会使用的dll中创建它们。



希望这会有所帮助。如果您在一个程序中执行所有操作,并且我的分析不正确,则需要提供更多详细信息。
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.


这篇关于在Windows 10中传递指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆