Solved

VB CreateEvent() help!

Posted on 1998-10-02
17
1,004 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:svfafel
  • 6
  • 4
  • 2
  • +3
17 Comments
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1437890
Go to :http://www.vbdomain.com/

and get winapihelp from the API section.

0
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1437891
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 .........
....
0
 

Author Comment

by:svfafel
ID: 1437892
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.  
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1437893
Posst your code here, so we can see it
0
 

Author Comment

by:svfafel
ID: 1437894
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
0
 

Author Comment

by:svfafel
ID: 1437895
Sorry, Change Dim SA as SECURITY_ATTRIBUTES and still doesn't work...
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1437896
Try passing vbNull as the first param. If VB won't accept it, change the first param in the declare to As Any
0
 

Author Comment

by:svfafel
ID: 1437897
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....
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:Jason_G
ID: 1437898
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
0
 
LVL 1

Expert Comment

by:Jason_G
ID: 1437899
Sorry, forgot to click email notification.
0
 
LVL 1

Expert Comment

by:wford
ID: 1437900
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.
0
 

Author Comment

by:svfafel
ID: 1437901
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?
0
 
LVL 1

Expert Comment

by:Jason_G
ID: 1437902
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
0
 

Author Comment

by:svfafel
ID: 1437903
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?
0
 
LVL 1

Expert Comment

by:wford
ID: 1437904
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.
0
 
LVL 1

Expert Comment

by:Jason_G
ID: 1437905
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
0
 
LVL 7

Accepted Solution

by:
linda101698 earned 200 total points
ID: 1437906
Since this question seems to contain useful information, I'm posting an answer here.  I will post a question for the experts whose cooperation helped solve the problem Jason_G and wford.

Linda Gardner
Customer Service @ Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now