我在Windows 7 x64中,尝试在两个进程之间使用相同的Event对象,一个是vb应用程序,另一个是C ++应用程序,但似乎在一个进程中创建的事件无法在另一个:
VB
Option Explicit
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Declare Function OpenEvent Lib "Kernel32" Alias "OpenEventW" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, lpName As String) As Long
Const EVENT_ALL_ACCESS = &H1F0003
Sub Testing()
Dim hCallEvent As Long
hCallEvent = OpenEvent(EVENT_ALL_ACCESS, 0, "GUID_Call_Event")
End Sub
Private Sub Form_Load()
Call Testing
End Sub
C ++
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hEvent = ::CreateEvent(NULL, FALSE, FALSE, _T("GUID_Call_Event"));
DWORD dwError = ::GetLastError();
return 0;
}
现在,如果我启动C ++应用程序来创建事件,然后启动vb应用程序来获取该事件,它就会失败并显示错误消息:“系统找不到指定的文件。”
注意:
你对此有什么想法吗?
答案 0 :(得分:2)
您正尝试在VB6中使用OpenEventW
。
很可能,您实际上想要使用OpenEventA
,在这种情况下声明必须像这样修复:
Private Declare Function OpenEvent Lib "Kernel32" Alias "OpenEventA" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
但是,如果你真的想使用OpenEventW,你必须这样做:
Private Declare Function OpenEvent Lib "Kernel32" Alias "OpenEventW" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As Long) As Long
然后
hCallEvent = OpenEvent(EVENT_ALL_ACCESS, 0, StrPtr("GUID_Call_Event"))