我正在尝试调用命令提示符将文本文件复制到用VB6编写的已编译OCX中的端口。它通过命令提示符中的copy命令执行此操作,将文本文件发送到“LPT1”等端口。这在XP和7上运行良好,但Windows 8不允许它发生。我采取了以下步骤:
由于该命令适用于Windows XP和7,我倾向于认为这是Windows 8的权限问题,但到目前为止所有的解决方法都失败了。如果需要更多信息,请告诉我,我会提供。谢谢!
编辑:我发现OCX在调试下表现正常......我的猜测是OCX的调试授予它IDE的特权,即Visual Studio 6。
编辑:根据要求,这是用VB6编写的原始ShellAndWait代码,用于启动cmd控制台。
Public Function ShellAndWait(ByVal FilePath As String, ByVal eAppStyle As VbAppWinStyle) As Boolean
Dim lPID As Long
'Default to not found'
m_lShellHandle = 0
'run the program'
lPID = Shell(FilePath, eAppStyle)
' Check for errors'
If lPID = 0 Then
ShellAndWait = False
Exit Function
End If
'The command console window is tough to get a hold of, so'
'that's the reason there are two attempts before moving on'
'and the delays, allow time for window to start'
DoEvents
Sleep 500
'Find the window handle'
EnumWindows AddressOf FindThread, lPID
'Not found - Try finding again (more aggressive wait)'
If m_lShellHandle = 0 Then
Sleep (1000)
EnumWindows AddressOf FindThread, lPID
End If
'Make sure we have a valid window'
If m_lShellHandle <> 0 Then
'Keep checking to see if window is still available'
Do While IsWindow(m_lShellHandle)
'Allow repaint'
DoEvents
'Allow other processes to run'
SleepEx 300, True
Loop
End If
ShellAndWait = True
End Function
答案 0 :(得分:0)
您发布的代码提供了答案,问题是对EnumWindows的调用。 VB6没有64位编译器可用,任何VB6应用程序都会声明EnumWindows函数类似于:
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As _
Long, ByVal lParam As Long) As Long
问题是第一个参数是回调函数指针,Long是32位内存地址,而AddressOf运算符只返回32位回调地址。据我所知,没有真正的方法来声明一个API函数来接受VB6中的64位指针。事实上,正如上面的评论所述,64位Office需要扩展到VBA运行时,以允许对64位安全的API调用使用LongPtr类型和PtrSafe声明。您可以在问题'Can a VB6 component be compiled to 64 bit?'和Office Dev Center page for PtrSafe中找到有关此内容的更多信息。
它可能在调试器的上下文中正常运行,因为它在WOW环境中运行,但如果从另一个上下文调用则会失败,因为它将尝试在非WOW环境中启动。
虽然可能是一些解决方法,但它会涉及剥离绝大多数本机API调用。正如评论所说,&#34;命令控制台窗口很难掌握,所以...&#34;将它移植到.NET可能会更容易。