是否有一个控制台窗口打开,我正在与它交谈吗? C++ /C#、Windows 和 Linux

发布于 2024-11-30 10:08:09 字数 1109 浏览 0 评论 0原文

我一直在开发一个嵌入式 C++ 应用程序作为 DLL/SO(“C”外部),在 Linux (Ubuntu 11.04) 和 Windows(XP) 上运行。因此,我不知道我的调用者应用程序是否正在运行 GUI、控制台或两者都不运行。我相信当没有控制台连接到 stdout 和 stderr 时,它们会指向位桶,因此仅写入 cout 或 cerr 是无害的,尽管我想知道输出是否确实位于页面文件/交换文件中的某个位置,浪费空间。

所以我当前的想法是让调用者程序给我一个回调函数(地址),并使该函数看起来像 (void)(*)(void *),其中 (void *) 指向调用者程序中定义良好的结构我可以在其中写入错误/状态/任何信息。我之前在 C 和 C++ 中做过类似的事情,但我知道我至少会有一个 C# 调用者。 C# 可以给我这样的回调吗?它/我读取对方可能写入的数据会有困难吗?

或者,我可以弹出一个控制台并向其写入消息。有人可以告诉我该怎么做吗?

我看着 Windows 中弹出的消息框,觉得这是一件痛苦的事情。没看过linux。有人有使用 char* 或 std::strings 的示例,而不是 Windows 所需的任何数据类型?

FWIW,我们的架构没有文件,没有共享内存,没有命名管道,也没有用于消息传递的套接字。请记住,作为一个程序,我不知道可写文件位置,无论如何我都无法可靠地写入文件。正如我所做的那样,某些系统操作会禁用用户写入我选择的任何位置。我必须依赖于用户将可访问的文件路径和文件名传递给我。

有人可以评论一下我的情况和想法吗?

非常感谢,

韦斯

评论: 如果我理解正确,您是否正在寻找一种适当的方法来记录 C++ DLL 库中的错误/状态/调试消息?由于您不了解 DLL 使用者的类型(C/C++ 控制台、C# GUI,..),因此您不确定是否可以写入标准输出? – m3rLinEz 1 小时前

嗯,不完全是。我想确认,在没有控制台的情况下写入 sdtout/stderr 会将输出发送到任何地方,并且不会使交换文件膨胀。我想这意味着无论我的消费者如何,都可以写入标准输出吗?

它的真正含义是,您能否向我指出示例代码,我可以使用它来演示将 extern“C”DLL 附加到 C# 程序并让程序设置回调和变量/结构/等。并将其地址传递给DLL,然后DLL可以将数据写入var/struct等。并调用回调。 W.

I've been developing an embedded C++ application as a DLL/SO ("C" externed) that runs on Linux (Ubuntu 11.04) and Windows(XP). As such, I have no idea if my caller application is running GUI, console or neither. I believe stdout and stderr point to the bit bucket when there is no console connected to them so just writing to cout or cerr is harmless though I wonder if the output literally sits in some location in the pagefile/swapfile wasting space.

So my current thought is to have the caller program give me a callback function (address) and have that function look like (void)(*)(void *) where the (void *) points to a well defined struct in the caller's program where I can write error/status/whatever information. I've done similar things before in C and C++, but I know I will have at least one C# caller. Can C# hand me such a callback? Will there be any difficulty for it/me to read the data the other might write?

Alternately, I could pop open a console and write messages to it. Can someone tell me how to do this?

I looked at popping up a message box in Windows and thkat IS A PAINFUL thing to do. Did n't look at linux. Someone have examples using char* or std::strings, not the whatever-thet-are datatypes required by windows?

FWIW, our architecture says no files, no shared memory, no named pipes and no sockets for message passing. And remembering that as a program I have no idea about writable file locations, I couldn't write to a file reliably anyway. As surely as I did some sys op would disable user writes to wherever I selected. I have to depend on the user passing accessible file path and file name to me.

Can someone please comment on my situation and ideas?

Many thanks,

Wes

COMMENT:
f I understand this correctly, you are finding a appropriate way to log error/status/debug message from your C++ DLL library? And since you have no knowledge of the type of DLL-consumer (C/C++ console, C# GUI, ..), you are not sure if it's okay to write to stdout? – m3rLinEz 1 hour ago

Well, not exactly. I want to confirm that writing to sdtout/stderr when there is no console sends the output to nowhere and that it isn't bloating the swap file. I guess that translates to is it ok to write to stdout regardless of my consumer.

What it really means is, can you point me to sample code that I can use to demonstrate attaching an extern "C" DLL to a C# program and to have the program set up a callback and variable/structure/etc. and pass the addresses thereof to the DLL, which can then write data to the var/struct/etc. and call the callback. W.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文