Link to home
Start Free TrialLog in
Avatar of svfafel
svfafel

asked on

VB CreateEvent() help!

I am looking for an example of how to use the WIN32 API CreateEvent(x,x,x,"") in a real way.  I have only been able to hunt down C++ examples.  I;m trying to do it but am failing miserably...any help would be great.
Avatar of a111a111a111
a111a111a111

Go to :http://www.vbdomain.com/

and get winapihelp from the API section.

Download the: winapihelp from the API from http://www.vbdomain.com 

Here is some of the help file content:

The CreateEvent function creates a named or unnamed event object.

HANDLE CreateEvent(

    LPSECURITY_ATTRIBUTES  lpEventAttributes,      // address of security attributes  
    BOOL  bManualReset,      // flag for manual-reset event
    BOOL  bInitialState,      // flag for initial state
    LPCTSTR  lpName       // address of event-object name  
   );      
Parameters

lpEventAttributes

Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the event object.
If lpEventAttributes is NULL, the event object is created with a default security descriptor and the resulting handle is not inheritable.

bManualReset

Specifies whether a manual-reset or auto-reset event object is created. If TRUE, then you must use the ResetEvent function to manually reset the state to nonsignaled. If FALSE, Windows automatically resets the state to nonsignaled after a single waiting thread has been released.

bInitialState

Specifies the initial state of the event object. If TRUE, the initial state is signaled; otherwise, it is nonsignaled.

lpName

Points to a null-terminated string specifying the name of the event object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.
If lpName matches the name of an existing named event object, this function requests EVENT_ALL_ACCESS access to the existing object. In this case, the bManualReset and bInitialState parameters are ignored because they have already been set by the creating process. If the lpEventAttributes parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.

If lpName is NULL, the event object is created without a name.
If lpName matches the name of an existing semaphore, mutex, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because event, mutex, semaphore, and file-mapping objects share the same name space.

Return Value

If the function succeeds, the return value is a handle of the event object. If the named event object existed before the function call, the GetLastError function returns ERROR_ALREADY_EXISTS. Otherwise, GetLastError returns zero.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

The handle returned by CreateEvent has EVENT_ALL_ACCESS access to the new event object and can be used in any function that requires a handle of an event object.
Any thread of the calling process can specify the event-object handle in a call to one of the wait functions (
MsgWaitForMultipleObjects, WaitForSingleObject, WaitForMultipleObjects, WaitForSingleObjectEx, or WaitForMultipleObjectsEx). The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one or when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.

The initial state of the event object is specified by the bInitialState parameter. Use the SetEvent function to set the state of an event object to signaled. Use the ResetEvent function to reset the state of an event object to nonsignaled.
When the state of a manual-reset event object is signaled, it remains signaled until it is explicitly reset to nonsignaled by the ResetEvent function. Any number of waiting threads, or threads that subsequently begin wait operations for the specified event object, can be released while the object's state is signaled.

When the state of an auto-reset event object is signaled, it remains signaled until a single waiting thread is released; the system then automatically resets the state to nonsignaled. If no threads are waiting, the event object's state remains signaled.
Multiple processes can have handles of the same event object, enabling use of the object for interprocess synchronization. The following object-sharing mechanisms are available:

7      A child process created by the CreateProcess function can inherit a handle of an event object if the lpEventAttributes parameter of CreateEvent enabled inheritance.
7      A process can specify the event-object handle in a call to the DuplicateHandle function to create a duplicate handle that can be used by another process.
7      A process can specify the name of an event object in a call to the OpenEvent or CreateEvent function.

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The event object is destroyed when its last handle has been closed.

See Also

CloseHandle, CreateProcess, DuplicateHandle, MsgWaitForMultipleObjects, OpenEvent, ResetEvent, SECURITY_ATTRIBUTES, SetEvent, WaitForMultipleObjects, WaitForMultipleObjectsEx, WaitForSingleObject, WaitForSingleObjectEx .........
....
Avatar of svfafel

ASKER

The CreateEvent() api call returns a handle of "0" every time i try it.  I am looking for a VB example to show how to return a proper handle.  I have looked through all the API docs and my code looks ok but it is not working.  
Posst your code here, so we can see it
Avatar of svfafel

ASKER

In this code, I always get a "0" for the hEvent value.  I can't seem to get a correct Handle from the CreateEvent. Any Clues?

Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Private Sub Command1_Click()
    Dim hEvent As Long, dRet As Long, SA As Long
    hEvent = CreateEvent(SA, False, False, "LanFaxPrinter")
    List1.AddItem hEvent
    dRet = WaitForSingleObject(hEvent, 10000)
End Sub
Avatar of svfafel

ASKER

Sorry, Change Dim SA as SECURITY_ATTRIBUTES and still doesn't work...
Try passing vbNull as the first param. If VB won't accept it, change the first param in the declare to As Any
Avatar of svfafel

ASKER

I still am getting a "0" that returns from the CreateEvent as the Handle.  I don't think this is correct.  Is my thinking right?  It should be a LONG value other than "0".  This is the million dollar question....
Hi svfafel,
Here's some additional information on the CreateEvent for you!

http://www.vbonline.com/vb-mag/9806/article/event.htm
http://www.safew.com/ss.htm

They both have some good sample code.

Sincerely,
Jason

Jason Gorman (jason_gorman@scholars.com)
Learning Advisor, http://scholars.com
Microsoft Authorized Technical Education Center
Novell Authorized Online Training Provider
Sorry, forgot to click email notification.
The documentation I have suggests the 0 indicates and error, use the getlasterror function to find out whats going on.

but also make sure you set the nlength in the security type, (normaly = 12) Or
make sure your API dec is changed to:

Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" ( ByVal lpEventAttributes As Long, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
and set you long SA = 0, before passing it, just to be safe.

If this works out, ask me to post, if not, tell us what the error turns out to be.
Avatar of svfafel

ASKER

Jason G and WFord - thanks for the assistance.  You both gave the correct solutions.  Works like a charm.  I appreciate it.  How do i give you both points?
Hi svfafel,
Well, what I've seen done before was for the questionee, to withdraw the question, and then repost to questions addressed to the Experts in question, with the points given appropriately.  Then when the two experts answered these new questions, the points are awarded as you wanted.

BTW, thanks for the points.

Sincerely,
Jason

Jason Gorman (jason_gorman@scholars.com)
Learning Advisor, http://scholars.com
Microsoft Authorized Technical Education Center
Novell Authorized Online Training Provider
Avatar of svfafel

ASKER

WFord and Jason_G...I haven't forgotten about you...I am trying to figure out how to repost the question to you.  I can't seem to get my points back...Any clue?
You can't delete cause once a question is answered and rejectered it cannot be deleted, but there are two ways: post a 0 point question  to Linda in the Experts Excange in Customer service with a message "delete question Q10085758". otherwise you could post the two other messages, and just let this one die a natural death in a month or so...if you have enough points that is.
Hi,
WFord is correct, I've also seen Linda divy the points up for people.

Sincerely,
Jason

Jason Gorman (jason_gorman@scholars.com)
Learning Advisor, http://scholars.com
Microsoft Authorized Technical Education Center
Novell Authorized Online Training Provider
ASKER CERTIFIED SOLUTION
Avatar of linda101698
linda101698

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial