Solved

VB CreateEvent() help!

Posted on 1998-10-02
17
994 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
Comment Utility
Go to :http://www.vbdomain.com/

and get winapihelp from the API section.

0
 
LVL 3

Expert Comment

by:a111a111a111
Comment Utility
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
Comment Utility
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
Comment Utility
Posst your code here, so we can see it
0
 

Author Comment

by:svfafel
Comment Utility
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
Comment Utility
Sorry, Change Dim SA as SECURITY_ATTRIBUTES and still doesn't work...
0
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:Jason_G
Comment Utility
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
Comment Utility
Sorry, forgot to click email notification.
0
 
LVL 1

Expert Comment

by:wford
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

10 Experts available now in Live!

Get 1:1 Help Now