如何集成开源C程序而不是通过系统调用调用其可执行文件?
我有一个可执行文件(fossil scm),我的程序通过 ::CreateProcess windows 调用从外部调用它。然后捕获 stdout 和 stderr。由于化石的源代码是可用的,我更愿意用它创建一个静态库并直接发出调用。目前,与化石的通信是通过命令行参数完成的,而返回的通信是通过进程返回代码、stdout 和 stderr 完成的。 Fossil 通过 printf 和 fprintf 调用写入 stdout/err。
在最小化化石来源改变的情况下解决这个问题的最佳方法是什么?是否有可靠且跨平台的方法来拦截 stdout/err 并将其发送到内存缓冲区?
I have an executable (fossil scm) that is being invoked by my program externally through ::CreateProcess windows call. The stdout and stderr are then captured. Since the source code for fossil is available, I would prefer to create a static library out of it and issue calls directly. Currently, communication to fossil is done through the command line parameters, and the communication back is through the process return code, stdout and stderr. Fossil writes to stdout/err through printf and fprintf calls.
What is the best way to solve this with minimum alteration of fossil source? Is there a reliable and cross-platform way to intercept stdout/err and send it into a memory buffer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说你想要
这表明您不想为 SCM 程序引入 API,而是希望在不更改现有代码的情况下继续解析文本输出。如果是这样,那么我认为没有必要改变你当前的方法。与当前方法相比,使用内存缓冲区和静态链接到底能获得什么?
You say that you want to
This would indicate that you don't want to introduce an API for the SCM program and instead wish to carry on parsing the textual output without changing your existing code. If that is so then I see no point in changing from your current approach. What exactly is to be gained by using a memory buffer and static linking over the current approach?
您可以按照如下步骤执行此操作:
freopen("filename.out" , "w", stdout);
但是,请注意,这是脆弱的:
通常,你可以这样做,但除非你有充分的理由(即性能)并且你准备好面对后果并自己修复错误,否则不要这样做。
You can do it in steps like these:
freopen("filename.out", "w", stdout);
However, please note that this is fragile:
Generally, you can do this, but don't unless you have a good reason to (i.e. performance) and you're ready to face the consequences and fix bugs yourself.
将化石转变为共享库,然后从您的自定义程序中使用它。
Turn fossil into a shared library, and then use that from your custom program.