进程外 COM 服务器 - 无法创建文件
我试图在进程外 COM 服务器的主线程中运行以下代码,但从未创建任何文件。
我想知道是否有人可以告诉我这是为什么?
FILE *f = fopen("Log.txt", "w");
fputs("Tony", f);
fputs("\n", f);
fclose(f);
I'm attempting to run the following piece of code from an Out-of-Process COM Server in its main thread, however no file is ever created.
I wondered if anyone can tell me why this is?
FILE *f = fopen("Log.txt", "w");
fputs("Tony", f);
fputs("\n", f);
fclose(f);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了权限问题之外,很有可能该文件实际上已创建,但您只是找不到它。您没有提供文件的完整路径名(例如
"c:\\blah\\log.txt"
),这意味着它将在 COM 客户端应用程序的当前工作目录中创建。保证与 COM 服务器所在的目录不相同。您需要指定完整路径。在 Windows 中,该目录应该是您保证对其具有写入权限的目录。使用 SHGetFolderPath() 获取 appdata 文件夹的路径。或者预留一个固定的目录名称用于日志记录(不推荐)。
Short from a permission problem, the odds are pretty good that the file actually got created but that you just can't find it back. You are not giving a full path name for the file (like
"c:\\blah\\log.txt"
) which means that it will be created in the current working directory of the COM client app. Which is guaranteed to be not the same directory where your COM server is located.You'll need to specify the full path. In Windows, that should be a directory that you have guaranteed write access to. Use SHGetFolderPath() to get the path to the appdata folder. Or set aside a fixed directory name for logging (not recommended).
我认为最可能的原因是 fopen 因访问被拒绝错误而失败。您是否尝试过单步执行代码?诊断此问题的最佳方法是附加调试器并检查 errno 的值。
I think the most likely cause is that fopen is failing with an access denied error. Have you tried stepping through the code? The best way to diagnose this is to attach a debugger and examine the value of errno.