简体   繁体   中英

Delphi: String[255] in 32-bit vs 64-bit

I use the following to share a shortstring between 2 executables:

Type
  PInstanceInfo = ^TInstanceInfo;
  TInstanceInfo = packed record
    MainAppHandle: THandle;
    SessionPath: String[255];
  end;
Var MappingHandle: THandle;
    InstanceInfo: PInstanceInfo;

  MappingHandle := CreateFileMapping(INVALID_HANDLE_VALUE,
                                     nil,
                                     PAGE_READWRITE,
                                     0,
                                     SizeOf(TInstanceInfo),
                                     PChar('MyApp'));

  InstanceInfo := MapViewOfFile(MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TInstanceInfo));
  InstanceInfo^.MainAppHandle := Application.Handle;

I use SessionPath to store the path to a file. This works fine when both apps are x86 or x64, but when App1 is 32-bit and App2 is 64-bit, App2 is missing the first 4 characters of the string when I read it. What's different in the ShortString structure in this case?

THandle is pointer sized. So it is 32 or 64 bit depending on the platform. That explains the missing 4 bytes. In fact this could easily be seen yourself by using the SizeOf function in test programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM