VB6:CreateFile() 在当前工作目录中创建名称乱码的文件
当我发现 CreateFile() 不能很好地使用文件名参数时,我正在对 CreateFile 无法打开现有命名管道的问题进行故障排除。我的代码是:
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
pipeHandle = CreateFile("C:\\test.txt", GENERIC_READ Or GENERIC_WRITE, 0&, 0&, CREATE_ALWAYS, 0&, 0&)
它不会在C:\中创建文件,而是在当前VB工作目录中创建一个文件,文件名是乱码。 CreateFile 似乎无法识别和解析给定的文件名字符串。
为什么会发生这种情况?我在 Windows 7 上使用 VB6(使用一些技巧来安装它)。这可能是导致问题的原因吗?
I was troubleshooting a problem where CreateFile couldn't open an existing named pipe when I found CreateFile() didn't work well with the filename parameter. My code is:
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
pipeHandle = CreateFile("C:\\test.txt", GENERIC_READ Or GENERIC_WRITE, 0&, 0&, CREATE_ALWAYS, 0&, 0&)
It does not create the file in C:\, instead, it creates a file in the current VB working directory, with a garbled filename. It seems CreateFile cannot recognize and parse the given filename string.
Why is this happening? I'm using VB6 on Windows 7 (used some trick to install it). Could that be causing the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已经很久了,但我认为这是 ansi/unicode 的事情。尝试 CreateFileA 函数,看看会发生什么。 (另外,IIRC,你无法逃避 \ ....尽管距离我认真使用 VB6 编码已经过去了大约 7 年。)
It's been a long time, but I think this is an ansi/unicode thing. Try the
CreateFileA
function and see what happens. (Also, IIRC, you don't escape the \ ....although again it's been about 7 years since I seriously coded with VB6.)