views:

13

answers:

1

I am in Windows 7 x64, and trying to use the same Event object between 2 processes, one is an vb application, and the other is a C++ application, but seems the event created in one process can't be recognized in another one:

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;
}

Now if I start the C++ application to have the event created, and then start the vb application to get that event, it just failed with error message: "The system cannot find the file specified."

Notice:

  • It failed both with VB and VBA when interact with a C++ process, but succeeded between 2 C++ processes
  • Event created in VB could not be identified in a C++ process either.

Do you have any idea on this?

+1  A: 

You are trying to use OpenEventW in VB6.

Quite likely, you actually want to use OpenEventA, in which case the declaration must be fixed like this:

Private Declare Function OpenEvent Lib "Kernel32" Alias "OpenEventA" _
  (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long 

But, if you actually want to use OpenEventW, you must do this:

Private Declare Function OpenEvent Lib "Kernel32" Alias "OpenEventW" _
  (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As Long) As Long 

and then

hCallEvent = OpenEvent(EVENT_ALL_ACCESS, 0, StrPtr("GUID_Call_Event"))
GSerg
Thanks GSerg, it does work!!!
lz_prgmr
BTW, StrPtr could get an address from a string, Is there a built in function to get a string from an address (represented by long)?
lz_prgmr
@Dbger: This question was asked [very recently](http://stackoverflow.com/questions/3921852/how-to-get-the-string-from-an-address-specified-by-a-long-in-vb) :)
GSerg
@GSerg: Yea, asked by me:) seems there is no built in function. Thanks so much for helping on my VB issues:)
lz_prgmr