Windows 7上的CreateFile失败 [英] CreateFile fails on Windows 7

查看:132
本文介绍了Windows 7上的CreateFile失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在1990年代写了一个安装程序,正在将其更新为现代UI.

我遇到了以下代码行说明的问题:

I wrote an installer back in the 1990''s that I am updating to a modern UI.

I ran across a problem illustrated by the following line of code:

HANDLE hfile = CreateFile("c:\\windows\\test.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);


我正在Windows 7上进行测试.

使用我的旧代码(最后使用VC6在2004年编译),我的SETUP.EXE基本上可以毫无问题地创建文件. (我不知道Microsoft是否为我的安装程序建立了任何向后兼容性,我想这是可能的,因为它已经广泛分发,但是谁知道.)

我在WinXP下使用VC6创建了一个新的测试程序,它也成功创建了该文件,但是一个小问题是,除非您在Win7资源管理器中单击兼容性文件"按钮,否则它不会出现.
我还在Win7下使用VC10创建了一个新的测试程序,它绝对失败,并显示错误#5访问被拒绝".

我所有的测试都是在运行Windows 7 64位版本的同一台计算机上完成的.

这是我的问题:

#1-为什么这三个程序在本质上执行完全相同的Win32 API调用时会有所不同?第一个示例工作正常.第二个可以,但是默认情况下,该文件在资源管理器视图中是隐藏的.第三个根本不起作用.

#2-在任何情况下,如何使用在Windows 7上运行的Visual Studio 2010编写的C ++程序写入Windows(或Windows System)目录?是否有一些简单的方法可以解决此问题,而无需使用Visual Studio 6进行编译?

感谢您提供了指向正确方向的指针.


I am testing on Windows 7.

With my old code (last compiled in 2004 using VC6) my SETUP.EXE essentially creates the file without any problem. (I don''t know if Microsoft built any sort of backwards compatibility in for my installer or not, I suppose that is possible since it was widely distributed, but who knows.)

I created a new test program using VC6 under WinXP, and it also successfully creates the file, however a minor issue in that it will not appear unless you click the "Compatibility Files" button in Win7 explorer.

I also created a new test program using VC10 under Win7, and it absolutely fails with Error #5 "Access Denied".

All of my tests were done on the same computer running the 64-bit version of Windows 7.

Here are my questions:

#1 - Why the difference between all three programs, when they essentially do the exact same Win32 API call? The first example works fine. The second works okay, but the file is hidden by default from explorer view. The third does not work at all.

#2 - In any case, how can I write to the Windows (or Windows System) directory using a C++ program written in Visual Studio 2010 running on Windows 7??? Is there some easy way around this, short of compiling using Visual Studio 6 instead?

Thanks for any pointers in the correct direction.

推荐答案

不要紧.如果您对答案感兴趣,请使用Google海拔清单".基本上,只需将.EXE设置为要求Admin,Windows便会在执行前提示,之后一切正常.
Nevermind. Google "elevation manifest" if you are interested in the answer. Basically, just set the .EXE to require Admin and Windows will prompt before executing and everything is fine after that.


这篇关于Windows 7上的CreateFile失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆