如何正确使用文件映射并将数据传递给子进程?
我的应用程序正在执行另一个进程(“update.exe”),我想将大数据(可能是一条记录)从我的应用程序传递到update程序。
使用命令行传递数据参数不是一个选项,因为数据太大(而且数据大小可能会有所不同)。
如何正确创建CreateFileMapping
/MapViewOfFile
/UnmapViewOfFile
,
然后执行我的update.exe,
最后在update.exe程序中接收数据(OpenFileMapping
),
并释放所有句柄(来自主应用程序和update.exe),这样我就不会出现内存/句柄泄漏?
代码会很好(请不要使用 JCL
)。 C++也不错。 谢谢。
编辑:我认为我的主要问题是如何在 update.exe
读取数据后向主应用程序“发出信号”到 UnmapViewOfFile
和 CloseHandle
。 (或者也许我需要在子进程中使用 OpenFileMapping
并将 bInheritHandle
设置为 True
?)
下面是一个示例。如果主进程调用UnmapViewOfFile
和CloseHandle
,第二个进程如何读取数据?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会在进程间通信中找到一些很好的示例。正确的方法取决于数据的大小和速度要求。
You may find some good example at Inter-process communication. Right method depends on the size of your data and speed requirements.
这是内存映射文件中的代码片段,我们用来在应用程序之间传递实时视频(未压缩),不需要 SecurityDescriptor,但我已将其保留,并且 CreateWorldAccessDescriptor 是我们的函数之一。
如果您需要使用这种类型的系统来控制从服务到应用程序的消息传递,或者从使用不同用户凭据运行的应用程序的消息传递,请确保以“Global\”开头
希望这会有所帮助。
更新
这段代码只是在整个文件的文件上创建一个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\"
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.