无法在C ++和VB进程之间共享Windows事件内核对象

时间:2010-10-14 07:45:59

标签: vb6 kernel

我在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应用程序来获取该事件,它就会失败并显示错误消息:“系统找不到指定的文件。”

注意:

  • 与C ++进程交互时,VB和VBA都失败了,但在2个C ++进程之间取得了成功
  • 无法在C ++过程中识别在VB中创建的事件。

你对此有什么想法吗?

1 个答案:

答案 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"))