Click to See Complete Forum and Search --> : problems allocating memory for structs


Ralphi
September 22nd, 1999, 03:59 AM
hello !

i have a struct and i want to send this with
sendmessage to another application.
this application should extract the struct
and go on with the data.
but it doesnīt work. maybe itīs something wrong
with the allocation of memory for the struct.
but i have no experience in doing that.
thanks for help,
ralph


/////////////////////////////////////////////

typedef struct
{
int number;
char Name[20];
char Lastname[30];
} Student;


/////////////////////////////////////////////

// function, application 1
.
.
.

Student *pData = new Student;
pData->number = 12;

memcpy (pData->Name,m_Name,Name_Length);
memcpy (pData->Name+Name_Length,"\0",1);

memcpy (pData->Lastname,m_Lastname,Lastname_Length);
memcpy (pData->Lastname+Lastname_Length,"\0",1);

COPYDATASTRUCT pCopyDataStruct;
pCopyDataStruct.dwData = 0;
pCopyDataStruct.cbData = 0;
pCopyDataStruct.lpData = (void*)pData;
LRESULT res = ::SendMessage(Hwnd_Target,WM_COPYDATA,NULL,(LPARAM)&pCopyDataStruct);

/////////////////////////////////////////////

// function, application 2

LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_COPYDATA: COPYDATASTRUCT *pCopyDataStruct = (COPYDATASTRUCT *) lParam;

Student *pData = new Student;
pData = (Student*)pCopyDataStruct->lpData;

if (pData->number == 12)
{
// use pData->Name;
// use pData->Lastname;
}
break;
}
}

/////////////////////////////////////////////

Karl
September 22nd, 1999, 04:35 AM
You can't share objects between applications, because the adress of your object is not absolute through the system, but relative to your app. You should use shared memory between your apps

Look at http://www.microsoft.com/MSJ/1198/WICKED/wicked1198top.htm

HTH.

K.

Meteorain
September 22nd, 1999, 04:57 AM
every application have own memory space on windows platform and the space was private.

a application can't to access anthor application's
memory space.

you need using FileMapping to solution the question.


-----------My Signature-----------
Hi,everybody
I am Sound_Wang from P.R.China. :)