如何正确使用文件映射并将数据传递给子进程?

发布于 2025-01-07 17:02:18 字数 877 浏览 3 评论 0原文

我的应用程序正在执行另一个进程(“update.exe”),我想将大数据(可能是一条记录)从我的应用程序传递到update程序。

使用命令行传递数据参数不是一个选项,因为数据太大(而且数据大小可能会有所不同)。

如何正确创建CreateFileMapping/MapViewOfFile/UnmapViewOfFile
然后执行我的update.exe
最后在update.exe程序中接收数据(OpenFileMapping),
并释放所有句柄(来自主应用程序和update.exe),这样我就不会出现内存/句柄泄漏?

代码会很好(请不要使用 JCL)。 C++也不错。 谢谢。


编辑:我认为我的主要问题是如何在 update.exe 读取数据后向主应用程序“发出信号”到 UnmapViewOfFileCloseHandle 。 (或者也许我需要在子进程中使用 OpenFileMapping 并将 bInheritHandle 设置为 True ?)
下面是一个示例。如果主进程调用UnmapViewOfFileCloseHandle,第二个进程如何读取数据?

My application is executing another process ("update.exe"), and I would like to pass large data (a Record maybe) from my app to the update program.

Using command line to pass the data parameter(s) is not an option, because the data is too big (also Data size may vary).

How to correctly create CreateFileMapping/MapViewOfFile/UnmapViewOfFile,
then Executing my update.exe,
finally Receiving the data in the update.exe program (OpenFileMapping),
and freeing all handles (from main app and update.exe) so I don't have memory/handle leaks?

Code would be nice (No JCL please). C++ is also fine.
Thanks.


Edit: I think my main problem is how to "signal" the main app to UnmapViewOfFile and CloseHandle after update.exe done reading the data. (or maybe I need to use OpenFileMapping with bInheritHandle set to True in my child process?)
Here is an Example. How can the Second process read the data if the main process calls UnmapViewOfFile and CloseHandle?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2025-01-14 17:02:18

您可能会在进程间通信中找到一些很好的示例。正确的方法取决于数据的大小和速度要求。

You may find some good example at Inter-process communication. Right method depends on the size of your data and speed requirements.

真心难拥有 2025-01-14 17:02:18

这是内存映射文件中的代码片段,我们用来在应用程序之间传递实时视频(未压缩),不需要 SecurityDescriptor,但我已将其保留,并且 CreateWorldAccessDescriptor 是我们的函数之一。

如果您需要使用这种类型的系统来控制从服务到应用程序的消息传递,或者从使用不同用户凭据运行的应用程序的消息传递,请确保以“Global\”开头

procedure TRawFeedImageQueue.CreateFileMap;
var
  SysInfo: TSystemInfo;
  sd: TSecurityDescriptor;
begin
  GetSystemInfo( SysInfo );
  MapGranularity := SysInfo.dwAllocationGranularity;
  MapSize := sizeof( TRawFeedImageQueueData );

  FSecObjOk := CreateWorldAccessDescriptor( sd, GENERIC_ALL );

  FileMappingHandle := CreateFileMapping( $FFFFFFFF, @sd, PAGE_READWRITE OR SEC_COMMIT, 0, MapSize and $FFFFFFFF, PChar(FFileName) );
  if FileMappingHandle <> 0 then
  begin
    MapView := MapViewOfFile( FileMappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, MapSize );
  end
  else
  begin
    MapView := nil;
  end;
end;

procedure TRawFeedImageQueue.ReleaseFileMap;
begin
  if FileMappingHandle <> 0 then
  begin
    unMapViewOfFile( MapView );
    CloseHandle( FileMappingHandle );
  end;
end;

希望这会有所帮助。

更新

这段代码只是在整个文件的文件上创建一个MapView,并且只是一个视图,当然如果需要的话,您可以在同一个文件上创建多个较小的视图。

Heres a code snippet from a memory mapped file we use to pass live video (uncompressed) between our applications, the SecurityDescriptor isn't needed but i've left it in and CreateWorldAccessDescriptor is one of our functions.

If you need to use this type of system to control messaging from services to application, or from applications running with different user credentials make sure you start your FileName with "Global\"

procedure TRawFeedImageQueue.CreateFileMap;
var
  SysInfo: TSystemInfo;
  sd: TSecurityDescriptor;
begin
  GetSystemInfo( SysInfo );
  MapGranularity := SysInfo.dwAllocationGranularity;
  MapSize := sizeof( TRawFeedImageQueueData );

  FSecObjOk := CreateWorldAccessDescriptor( sd, GENERIC_ALL );

  FileMappingHandle := CreateFileMapping( $FFFFFFFF, @sd, PAGE_READWRITE OR SEC_COMMIT, 0, MapSize and $FFFFFFFF, PChar(FFileName) );
  if FileMappingHandle <> 0 then
  begin
    MapView := MapViewOfFile( FileMappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, MapSize );
  end
  else
  begin
    MapView := nil;
  end;
end;

procedure TRawFeedImageQueue.ReleaseFileMap;
begin
  if FileMappingHandle <> 0 then
  begin
    unMapViewOfFile( MapView );
    CloseHandle( FileMappingHandle );
  end;
end;

Hope that helps a bit.

Update

This code simply creates a MapView on the file of the whole file, and just a single view, you can of course create multiple smaller views on the same file if required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文