|
You are welcome.
Veni, vidi, vici.
|
|
|
|
|
|
I have a Delphi application which Creates a shared memory uses CreateFileMapping, OpenFileMapping, MapViewOfFile functions.
Now I wanted to share the same memory for my MFC application. I used the OpenFileMapping, MapViewOfFile functions.
I created a structure exactly same in size as the Delphi application and mapped the structure object.
sample code:
HANDLE hMapObject2;
hMapObject2 = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, "PP101U3_SHARED");
if( !hMapObject2 )
{
AfxMessageBox("Failed to open Simpack DataBase");
return( 0 );
}
Simpack = ( struct SIMPACKDB *) MapViewOfFile( hMapObject2, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
if( !Simpack )
{
AfxMessageBox("Failed to create Simpack File Map View");
return(0);
}
Esim->SPV1 = Simpack->SP_Z;
I am able to read the values exactly correct for all the member variables in the structure.
But when I try to write value in the shared memory, its not changing. It shows the previous value immediately.
The value of Simpack->SP_Z[15] is 0.5010 as read
from the shared memory which is set by the Delphi application.
When I set or write the value of the same variable to the shared memory using the code:
Simpack->SP_Z[15] = 0.6123;it still shows the previous value 0.5010. When I change the same variables value in the Delphi application it changes and the changed value can be read here in the MFC application.
Please help me how to write the values in the shared memory. Is there anything wrong in the code?
|
|
|
|
|
manoharbalu wrote: When I set or write the value of the same variable to the shared memory using the code:
Simpack->SP_Z[15] = 0.6123;it still shows the previous value 0.5010.
What does 'it still shows' mean? You checking it in debugger or printing the values.
I suggest you to check whether that process has write permission for that shared memory.
|
|
|
|
|
The previous value 0.5010 is the orginal value of the same variable in the Delphi application. Also it is the value in the MFC application that is got after mapping it to the shared memory using OpenFileMapping and MapFileView functions.
This value is seen in the watch window of the Debugger in the MFC application
|
|
|
|
|
|
Richard MacCutchan wrote: Why have you reposted this question[^]?
Probably because he wasn't able to get an urgentz answer from the first posting.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi all,
I am having a problem. I am having two threads, one thread i doing some work inside a for loop and inside that for loop its making a call for second thread.
My problem is that my first thread should not resume its working untill thread2 finishes it work, and after thread2 has finished its work it should start its working after thread2 was called.
Can i use CEvent for this???
I am not getting how to do it.
Thanks in advance
|
|
|
|
|
Have a look at this[^] for basic usage of event object. You can use the same logic to use CEvent on you own.
You may find this[^] code project article useful too.
|
|
|
|
|
Sounds like a good time to use WaitForSingleObject() in the first thread, waiting for an event or some other object to get notified/released/set in the second thread.
==============================
Nothing to say.
|
|
|
|
|
VCProgrammer wrote: my first thread should not resume its working untill thread2 finishes it work
So why are you using threads? This is serial processing not parallel, so using threads does not provide any advantage.
Programming is work, it isn't finger painting. Luc Pattyn
|
|
|
|
|
VCProgrammer wrote: Can i use CEvent for this???
yes you can! however am agree with richard!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I have a Delphi application which Creates a shared memory uses CreateFileMapping, OpenFileMapping, MapViewOfFile functions.
Now I wanted to share the same memory for my MFC application. I used the OpenFileMapping, MapViewOfFile functions.
I created a structure exactly same in size as the Delphi application and mapped the structure object.
sample code:
HANDLE hMapObject2;
hMapObject2 = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, "PP101U3_SHARED");
if( !hMapObject2 )
{
AfxMessageBox("Failed to open Simpack DataBase");
return( 0 );
}
Simpack = ( struct SIMPACKDB *) MapViewOfFile( hMapObject2, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
if( !Simpack )
{
AfxMessageBox("Failed to create Simpack File Map View");
return(0);
}
Esim->SPV1 = Simpack->SP_Z;
I am able to read the values exactly correct for all the member variables in the structure.
But when I try to write value in the shared memory, its not changing. It shows the previous value immediately.
The value of Simpack->SP_Z[15] is 0.5010 as read
from the shared memory which is set by the Delphi application.
When I set or write the value of the same variable to the shared memory using the code:
Simpack->SP_Z[15] = 0.6123;it still shows the previous value 0.5010. When I change the same variables value in the Delphi application it changes and the changed value can be read here in the MFC application.
Please help me how to write the values in the shared memory. Is there anything wrong in the code?
|
|
|
|
|
Please put your code between <pre> tags to make it easy to read.
What data type is the Simpack->SP_Z array?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
My Code:
hMapObject2 = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, "PP101U3_SHARED");
if( !hMapObject2 )
{
AfxMessageBox("Failed to open Simpack DataBase");
return( 0 );
}
Simpack = ( struct SIMPACKDB *) MapViewOfFile( hMapObject2, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
if( !Simpack )
{
AfxMessageBox("Failed to create Simpack File Map View");
return(0);
}
Simpack->SP_Z = 0.6212;
The member sp_z is of type float
|
|
|
|
|
Are you getting any Access violations when you try to set the value in the MFC program?
You might want to use the VirtualQuery function to investigate if the view you have mapped is as you expect.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I think it would have been mapped because I am able to read the values from the delphi application which is creating the shared memory. And the values are also updated in the MFC application when the value is changed in the Delphi application.
I checked till the last member variable in the entire structure.
|
|
|
|
|
I know, but I suggested using VirtualQuery because there might be something unusual about the access permissions on the pages of memory in the mapping.
Can you show the code that creates the file mapping?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Delphi Code:
while not bDone do begin
s_hMemFile := CreateFileMapping( $FFFFFFFF, Nil, PAGE_READWRITE,
0, cbSize, @sTemp[1] );
idErr := GetLastError;
if ( idErr <> 0 ) and { => Function Failed }
( idErr <> ERROR_ALREADY_EXISTS ) then begin
CloseHandle( s_hMemFile );
s_hMemFile := 0;
inc( iRetryCount );
// Map the File to Memory
if bDone or not FGenerateName then
Data := MapViewOfFile( s_hMemFile, FILE_MAP_ALL_ACCESS, 0, 0, ENTIRE_FILE )
else begin
if FGenerateName and (Data <> Nil) then begin
try
UnMapViewOfFile( Data );
finally
Data := Nil;
end; { try }
end; { if }
end; { if then else }
|
|
|
|
|
I don't see a problem there, however, I have to ask about the code you use to set the value(s) in the MFC program.
In your first post you showed SP_Z being used with an index, such as SP_Z[15] , and in the next post, you showed that you're setting it without an index. Is this an array or not an array?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I am sorry. The first one is correct. Its an array.
|
|
|
|
|
hello guys... I am trying to write this small chat application. Here, I want the user name, colored differently than his/her message. Now I had a look at the CHARFORMAT structure but don't know which of the value needs to be set, in order to change color of my user name only. Here is what it looks like
typedef struct _charformat {
UINT cbSize;
DWORD dwMask;
DWORD dwEffects;
LONG yHeight;
LONG yOffset;
COLORREF crTextColor;
BYTE bCharSet;
BYTE bPitchAndFamily;
TCHAR szFaceName[LF_FACESIZE];
} CHARFORMAT;
Here is definition for my function which adds the message to the rich edit control
void CConnectUserDlg::AddMessage(LPCTSTR strUser, LPCTSTR strMessage)
{
CString strCompleteMsg;
strCompleteMsg.Format("%s:\t%s\n", strUser, strMessage);
int nLength;
nLength = m_richMessages.GetTextLength();
m_richMessages.SetSel(nLength, nLength);
m_richMessages.ReplaceSel(strCompleteMsg);
m_richMessages.SetSel(-1, 0);
GetDlgItem(IDC_EDIT)->SetWindowText("");
GetDlgItem(IDC_EDIT)->SetFocus();
}
Thanks for any pointers.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
What have you tried so far? Did you examine the docs for the CHARFORMAT structure: CHARFORMAT[^]
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
oops..I forgot to paste the following code in the function body. Here is what I am trying at the top of the provided function body in OP.
CHARFORMAT Cfm;
m_richMessages.GetSelectionCharFormat(Cfm);
Cfm.cbSize = sizeof(CHARFORMAT);
Cfm.dwMask = CFM_ITALIC;
Cfm.dwEffects ^= CFE_ITALIC;
Cfm.crTextColor = RGB(0,255,0);
m_richMessages.SetSelectionCharFormat(Cfm);
1 - Now first, this code colors the entire rich edit box. But I want to change the color of the user name only, not his/her message.
2 - To surprise you, this code italicizes the text in the rich edit control but does not change the color of it.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Well, one thing I see is that you forgot to add the CFM_COLOR flag to the mask:
Cfm.dwMask = CFM_ITALIC | CFM_COLOR;
This flag tells the function to set the color based upon the crTextColor member of the structure.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|