I
I
igamity2011-08-18 15:44:36
Delphi
igamity, 2011-08-18 15:44:36

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:
  • the INVALID_HANDLE_VALUE value is incorrectly set, but then I could not find how to do it correctly ...
  • Marshal.WriteIntPtr(fd, this.Handle); does not work adequately, in the sense that the result does not correspond to the Denf variant pLongint(pFD)^:= Self.Handle;
  • WM_COPYDATA value is set incorrectly, although msdn says so.


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

2 answer(s)
V
VenomBlood, 2011-08-18
@igamity

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.

P
PashaPash, 2011-08-18
@PashaPash

Try using the standard System.IO.MemoryMappedFiles.MemoryMappedFile class , and just write this.Handle to the BinaryWriter from the example.
The WM_COPYDATA value is not used anywhere in the code, by the way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question