based pointer crashed my app
-
Hi for all I'm trying to pass some data between 2 windows. This is the class that I pass between my 2 windows. It's just a single class with a based pointer:
#ifndef TESTE_H #define TESTE_H #include void * vValue; class Teste { public: Teste(); ~Teste(); void SetMsg(char * cMsg); void ExibeMsg(); Teste __based(vValue) * Single; private: char * m_cMsg; char * m_cClassName; }; Teste::Teste() { if(m_cClassName == 0)m_cClassName = "Classe Teste"; } Teste::~Teste(){} void Teste::SetMsg(char * cMsg) { m_cMsg = cMsg; } void Teste::ExibeMsg() { if(m_cMsg != NULL)MessageBox(NULL, m_cMsg, "msg", MB_OK);} #endif TESTE_H
This is the code that sends the data and handles the data:case WM_COPYDATA: { COPYDATASTRUCT * dataReceived = (COPYDATASTRUCT*)lParam; Teste * tempTeste = (Teste*)dataReceived->lpData; Teste * ttt = tempTeste->Single; char cResult[256] = {0}; int nCount = 0; if(ttt != NULL) { MessageBox(NULL, "exibindo msg janela 1", "", MB_OK); ttt->ExibeMsg(); } break; } case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_BUTTON1: { char cLength[20], cSize[5]; if(HIWORD(wParam) == BN_CLICKED) { if(GetDlgItemText(hwnd, IDC_EDIT1, cLength, 20) != 0) { Teste teste1; teste1.SetMsg("aaa"); vValue = (void*)&teste1; COPYDATASTRUCT dataToSend; &
-
Hi for all I'm trying to pass some data between 2 windows. This is the class that I pass between my 2 windows. It's just a single class with a based pointer:
#ifndef TESTE_H #define TESTE_H #include void * vValue; class Teste { public: Teste(); ~Teste(); void SetMsg(char * cMsg); void ExibeMsg(); Teste __based(vValue) * Single; private: char * m_cMsg; char * m_cClassName; }; Teste::Teste() { if(m_cClassName == 0)m_cClassName = "Classe Teste"; } Teste::~Teste(){} void Teste::SetMsg(char * cMsg) { m_cMsg = cMsg; } void Teste::ExibeMsg() { if(m_cMsg != NULL)MessageBox(NULL, m_cMsg, "msg", MB_OK);} #endif TESTE_H
This is the code that sends the data and handles the data:case WM_COPYDATA: { COPYDATASTRUCT * dataReceived = (COPYDATASTRUCT*)lParam; Teste * tempTeste = (Teste*)dataReceived->lpData; Teste * ttt = tempTeste->Single; char cResult[256] = {0}; int nCount = 0; if(ttt != NULL) { MessageBox(NULL, "exibindo msg janela 1", "", MB_OK); ttt->ExibeMsg(); } break; } case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_BUTTON1: { char cLength[20], cSize[5]; if(HIWORD(wParam) == BN_CLICKED) { if(GetDlgItemText(hwnd, IDC_EDIT1, cLength, 20) != 0) { Teste teste1; teste1.SetMsg("aaa"); vValue = (void*)&teste1; COPYDATASTRUCT dataToSend; &
The Memory that you point to, is only valid in the function because it goes out of scope. It is better with static memory or allocated with GlobalAlloc. Attention: I think with most above written you 'may' run into trouble on the new MS Windows Vista.:confused:
Greetings from Germany
-
The Memory that you point to, is only valid in the function because it goes out of scope. It is better with static memory or allocated with GlobalAlloc. Attention: I think with most above written you 'may' run into trouble on the new MS Windows Vista.:confused:
Greetings from Germany
KarstenK wrote:
allocated with GlobalAlloc
why don't you just
new
it ?
Don't know where to start ?
Refer the Forums Guidelines and ask a friend -
The Memory that you point to, is only valid in the function because it goes out of scope. It is better with static memory or allocated with GlobalAlloc. Attention: I think with most above written you 'may' run into trouble on the new MS Windows Vista.:confused:
Greetings from Germany
In a site(www.flounder.com), I read this: "The problem of storing pointers is a little bit misleading. You must not store pointers in the structure you pass, even if they are pointers into the same area you are passing, because when the message is received the recipient has no control over where it is placed, and the pointers are likely to be nonsense. Obviously, you can't store pointers to other areas of your own memory because they will be completely nonsensical in the receiving process; they will be interpreted in its own address space. But the misleading aspect is that if you have a structure which is entirely self-contained in the data area you are sending over (that is, every pointer refers to another structure in the area you are sending), then you can use based pointers to encode them. Based pointers are actually relative offsets measured from a specific starting point (such as the start of the area being transmitted), and are automatically adjusted by the recipient (as long as you set up the correct base value) so they are valid pointers." So I have a based pointer in my class that is the same type of the class. This pointer must have to arrive(I think) to the other window safetly. toxcct, if I use
new
I lose the control over the pointer in the other window. -
Hi for all I'm trying to pass some data between 2 windows. This is the class that I pass between my 2 windows. It's just a single class with a based pointer:
#ifndef TESTE_H #define TESTE_H #include void * vValue; class Teste { public: Teste(); ~Teste(); void SetMsg(char * cMsg); void ExibeMsg(); Teste __based(vValue) * Single; private: char * m_cMsg; char * m_cClassName; }; Teste::Teste() { if(m_cClassName == 0)m_cClassName = "Classe Teste"; } Teste::~Teste(){} void Teste::SetMsg(char * cMsg) { m_cMsg = cMsg; } void Teste::ExibeMsg() { if(m_cMsg != NULL)MessageBox(NULL, m_cMsg, "msg", MB_OK);} #endif TESTE_H
This is the code that sends the data and handles the data:case WM_COPYDATA: { COPYDATASTRUCT * dataReceived = (COPYDATASTRUCT*)lParam; Teste * tempTeste = (Teste*)dataReceived->lpData; Teste * ttt = tempTeste->Single; char cResult[256] = {0}; int nCount = 0; if(ttt != NULL) { MessageBox(NULL, "exibindo msg janela 1", "", MB_OK); ttt->ExibeMsg(); } break; } case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_BUTTON1: { char cLength[20], cSize[5]; if(HIWORD(wParam) == BN_CLICKED) { if(GetDlgItemText(hwnd, IDC_EDIT1, cLength, 20) != 0) { Teste teste1; teste1.SetMsg("aaa"); vValue = (void*)&teste1; COPYDATASTRUCT dataToSend; &
Alex Cutovoi wrote:
Teste __based(vValue) * Single;
So what address is 4 bytes from
vValue
? Unless it is the beginning ofExibeMsg()
, there's no way that the following could work:ttt->ExibeMsg();
What you're doing seems very unreliable and dangerous. What is the receivng window supposed to do with the data it receives?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
KarstenK wrote:
allocated with GlobalAlloc
why don't you just
new
it ?
Don't know where to start ?
Refer the Forums Guidelines and ask a friend -
In a site(www.flounder.com), I read this: "The problem of storing pointers is a little bit misleading. You must not store pointers in the structure you pass, even if they are pointers into the same area you are passing, because when the message is received the recipient has no control over where it is placed, and the pointers are likely to be nonsense. Obviously, you can't store pointers to other areas of your own memory because they will be completely nonsensical in the receiving process; they will be interpreted in its own address space. But the misleading aspect is that if you have a structure which is entirely self-contained in the data area you are sending over (that is, every pointer refers to another structure in the area you are sending), then you can use based pointers to encode them. Based pointers are actually relative offsets measured from a specific starting point (such as the start of the area being transmitted), and are automatically adjusted by the recipient (as long as you set up the correct base value) so they are valid pointers." So I have a based pointer in my class that is the same type of the class. This pointer must have to arrive(I think) to the other window safetly. toxcct, if I use
new
I lose the control over the pointer in the other window. -
if it is in the same app/process it is OK, but he didnt say that. In the same app I would use a global (memory) object.
Greetings from Germany
but GlobalAlloc() is not portable...
Don't know where to start ?
Refer the Forums Guidelines and ask a friend -
Try use GlobalAlloc()/GlobalFree, but check/test it on Vista. Read the MSDN.
Greetings from Germany
What about GlobalAlloc()/GlobalFree changed on Vista? I see nothing in the PSDK. Mark
-
Alex Cutovoi wrote:
Teste __based(vValue) * Single;
So what address is 4 bytes from
vValue
? Unless it is the beginning ofExibeMsg()
, there's no way that the following could work:ttt->ExibeMsg();
What you're doing seems very unreliable and dangerous. What is the receivng window supposed to do with the data it receives?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
The code that I put in my question is the same for both windows. It doesn't change. If the way I choose is unreliable and dangerous, what you suggest for me to solve this?? I need a big help
-
The code that I put in my question is the same for both windows. It doesn't change. If the way I choose is unreliable and dangerous, what you suggest for me to solve this?? I need a big help
Alex Cutovoi wrote:
The code that I put in my question is the same for both windows.
Which is irrelevant. What matters is that
vValue
plus 4 bytes does not referenceExibeMsg()
, but referencesm_cMsg
instead. Again I ask, what is it that you are trying to do?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Alex Cutovoi wrote:
The code that I put in my question is the same for both windows.
Which is irrelevant. What matters is that
vValue
plus 4 bytes does not referenceExibeMsg()
, but referencesm_cMsg
instead. Again I ask, what is it that you are trying to do?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
Ok, I need to pass an entire object between 2 windows. In both windows they recieve different handles. I only get to pass structs(which is very easy).
-
Ok, I need to pass an entire object between 2 windows. In both windows they recieve different handles. I only get to pass structs(which is very easy).
Try this:
// the structure to pass around
class MyStruct
{
public:
MyStruct()
{
m_pMsg = new char[128];
lstrcpy(m_pMsg, "Hello World");
}~MyStruct() { delete \[\] m\_pMsg; m\_pMsg = NULL; } char \*m\_pMsg; void Exec( void ) { AfxMessageBox(m\_pMsg); }
};
// on the sending end
MyStruct myStruct;
COPYDATASTRUCT dataToSend;dataToSend.cbData = sizeof(MyStruct);
dataToSend.lpData = &myStruct;
dataToSend.dwData = 0;::SendMessage(GetSafeHwnd(), WM_COPYDATA, (WPARAM) GetSafeHwnd(), (LPARAM)&dataToSend);
// on the receiving end
COPYDATASTRUCT *dataReceived = (COPYDATASTRUCT *)pCopyDataStruct;
MyStruct *pStruct = (MyStruct *) dataReceived->lpData;
pStruct->Exec();
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hi for all I'm trying to pass some data between 2 windows. This is the class that I pass between my 2 windows. It's just a single class with a based pointer:
#ifndef TESTE_H #define TESTE_H #include void * vValue; class Teste { public: Teste(); ~Teste(); void SetMsg(char * cMsg); void ExibeMsg(); Teste __based(vValue) * Single; private: char * m_cMsg; char * m_cClassName; }; Teste::Teste() { if(m_cClassName == 0)m_cClassName = "Classe Teste"; } Teste::~Teste(){} void Teste::SetMsg(char * cMsg) { m_cMsg = cMsg; } void Teste::ExibeMsg() { if(m_cMsg != NULL)MessageBox(NULL, m_cMsg, "msg", MB_OK);} #endif TESTE_H
This is the code that sends the data and handles the data:case WM_COPYDATA: { COPYDATASTRUCT * dataReceived = (COPYDATASTRUCT*)lParam; Teste * tempTeste = (Teste*)dataReceived->lpData; Teste * ttt = tempTeste->Single; char cResult[256] = {0}; int nCount = 0; if(ttt != NULL) { MessageBox(NULL, "exibindo msg janela 1", "", MB_OK); ttt->ExibeMsg(); } break; } case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_BUTTON1: { char cLength[20], cSize[5]; if(HIWORD(wParam) == BN_CLICKED) { if(GetDlgItemText(hwnd, IDC_EDIT1, cLength, 20) != 0) { Teste teste1; teste1.SetMsg("aaa"); vValue = (void*)&teste1; COPYDATASTRUCT dataToSend; &
You cannot pass a pointer between processes. All the data has to be self-contained in the block of memory that you refer to in the
COPYDATASTRUCT
.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?
-
You cannot pass a pointer between processes. All the data has to be self-contained in the block of memory that you refer to in the
COPYDATASTRUCT
.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?
Exactly, like I said to toxcct, if I do this, I lost the control of the pointer. And I did this before, Michael, can you tell me how can I solve? I've tested with structures, works very fine, but what I want is a object. Hence I'm trying to solve this using based pointers. It's my very first time using based pointers.
-
Exactly, like I said to toxcct, if I do this, I lost the control of the pointer. And I did this before, Michael, can you tell me how can I solve? I've tested with structures, works very fine, but what I want is a object. Hence I'm trying to solve this using based pointers. It's my very first time using based pointers.
You cannot pass a pointer between processes. A based pointer doesn't solve this, because it's just an offset from another pointer.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?
-
You cannot pass a pointer between processes. A based pointer doesn't solve this, because it's just an offset from another pointer.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?
That means, it's not possible to send an object to another window right? If an object is a pointer, there's not a way to send it. My app actually is passing structs, but what I want to pass is an object.