Answer the question
In order to leave comments, you need to log in
Delphi to C#
Good afternoon!
I ran into a problem that arose in the process of translating a program from Delphi to C#.
There is the following Delphi code:
procedure TForm1 . FormCreate ( Sender : TObject ) ;
var pFD : pChar ;
begin
fMH : = CreateFileMapping ( INVALID_HANDLE_VALUE , nil , PAGE_READWRITE , 0 , sizeof ( longint ) , 'MM_MAP' ) ;
if ( fMH <> 0 ) then
begin
pFD : = MapViewOfFile (fMH , FILE_MAP_WRITE , 0 , 0 , 0 ) ;
if Assigned ( pFD ) then
try
pLongint ( pFD ) ^ : = Self . handle ;
finally
UnmapViewOfFile ( pFD ) ;
end ;
end ;
end ;
I am not a big connoisseur of C#, so I can not figure out what the problem is. My code:
private const int WM_COPYDATA = 0x4A ;
private IntPtr INVALID_HANDLE_VALUE = new IntPtr ( - 1 ) ;
private void Form1_Load ( object sender, EventArgs e )
{
fh = Win32API. CreateFileMapping ( INVALID_HANDLE_VALUE, IntPtr. Zero , Win32API. FileMapProtection . PageReadWrite , 0 , Int32. MaxValue , "MM_MAP" ) ;
if ( fh != IntPtr. Zero )
{
IntPtr fd = ( IntPtr ) Win32API. MapViewOfFile ( fh, Win32API. FileMapAccess . FileMapWrite , 0 , 0 , 0 ) ;
if ( fd != IntPtr. Zero )
try
{
Marshal. WriteIntPtr ( fd, this . Handle ) ;
}
finally
{
Win32 API. UnmapViewOfFile ( fd ) ;
}
}
}
The actual task of the program is as follows: when starting, write your descriptor to memory (if I didn’t mess up anything in the terminology), and then receive the WM_COPYDATA system messages, which need to be processed.
Potentially, I see three problem areas:
Can anyone suggest what could be wrong and how to solve the problem?
Answer the question
In order to leave comments, you need to log in
sizeof(longint) and Int32.MaxValue? sizeof in Delphi returns the size of the used memory under a variable? It's just that Int32.MaxValue is two billion kopecks.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question