Event help needed

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

I have a service program that creates a crystal report and prints the report
to a named printer driver. The printer driver raises an event when it is
finished. I am supposed to trap for the following event to determine when I
can continue with my code flow.
Event:
%printername%.mfx.complete

Can anyone help me out with some sample code to loop and wait for this
event? I found one example in C#, but I'm not having much luck getting to
work in vb.net.

I'm using v. 2003.

Thanks!
 
inside of a sub (probably new())

addhandler %printername%.mfx.complete, addressof PrintJobComplete


Private Sub PrintJobComplete
'call the rest of your code here
end sub
 
Jon said:
Thank you. Apparently, this article below demonstrates how it should be
done, but it's in C#, and I can't get it converted correctly.
http://www.codeproject.com/csharp/interopevents.asp

Ah, what we're talking about here aren't .NET events, they are Win32
events. So really, since C# and VB.NET both interop with Win32 in a
very similar way, it's just a translation problem. Let's see your VB
translation :)
 
Yes, I'm sorry, these are win32 events. My conversion follows

The three errors I get are
1)Dim unEventPermissions As UInt32 = 2031619
It says error converting 2031619 to a UInt32
2) OpenEvent was not found in dll Kernel32.dll
3) IntPtr.Zero = hEvent
operator = is not defined for IntPtr

Here it is:

Namespace CSharpEventSink


' <summary>
' Summary description for Class1.
' </summary>
Class Class1

'OPEN EVENT
Private Declare Function OpenEvent Lib "Kernel32.dll" (ByVal
dwDesiredAccess As UInt32, ByVal bInheritHandle As Boolean, ByVal lpName As
String) As IntPtr

' <summary>
' The main entry point for the application.
' </summary>
<STAThread()> _
Private Shared Sub Main(ByVal args() As String)
Dim unEventPermissions As UInteger = 2031619
' Same as EVENT_ALL_ACCESS value in the Win32 realm
Dim hEvent As IntPtr = IntPtr.Zero
' Get a handle to the Win32 Event. The name, "MfcEventSource",
is known in advance
hEvent = OpenEvent(unEventPermissions, False, "MfcEventSource")
If (IntPtr.Zero = hEvent) Then
Console.WriteLine("OpenEvent failed")
Return
' Exit
End If
' Create an AutoResetEvent object to wrap the handle we got from
OpenEvent
Dim arEvent As AutoResetEvent = New AutoResetEvent(False)
arEvent.Handle = hEvent
' Set the Handle to the event from
Dim waitHandles() As WaitHandle
'Put it in our array for WaitAny()
waitHandles(0) = arEvent
Dim bDone As Boolean = False

While Not bDone
Console.WriteLine("Top of while")
Dim waitResult As Integer = WaitHandle.WaitAny(waitHandles,
2000, False)
' For timeout, just loop and wait again
If (waitResult = WaitHandle.WaitTimeout) Then
Console.WriteLine(" WaitAny timed out.")
ElseIf (0 = waitResult) Then
Console.WriteLine("0 == waitResult. Yippee !!!")
' Do Something
ElseIf (1 = waitResult) Then
Console.WriteLine("1 == waitResult !!!")
' Do Something Else
Else
Console.WriteLine("Error else")
End If

End While
End Sub
End Class
End Namespace
 
Jon said:
Yes, I'm sorry, these are win32 events. My conversion follows

I inferred the necessary Imports statements from the C# source :)
The three errors I get are
1)Dim unEventPermissions As UInt32 = 2031619
It says error converting 2031619 to a UInt32

Until VB2005 (I think), VB's support for unsigned types such as UInt32
is not ideal. You have to do this to initialize one:

Dim unEventPermissions As UInt32 = Convert.ToUInt32(2031619)

(Here the conversion is actually from a Long, which is what the
compiler assumes 2031619 is)

2) OpenEvent was not found in dll Kernel32.dll

When P/Invoking API functions that have both ANSI and Unicode versions,
you need to add 'Ansi', 'Unicode', or (usually the appropriate choice)
'Auto' to the Declare statement. If you did any API work in VB classic,
you may remember having to always declare the -A version of APIs - this
is the improved replacement for that. So:

Private Declare Auto Function OpenEvent Lib "Kernel32.dll"
(ByVal _
dwDesiredAccess As UInt32, ByVal bInheritHandle As Boolean, _
ByVal lpName As String) As IntPtr

3) IntPtr.Zero = hEvent
operator = is not defined for IntPtr

Until VB2005 (I think), VB doesn't support operator overloading (ie
redefining things like = to have a specific meaning for non-intrinsic
types), so we have to explicitly call Equals:

If IntPtr.Zero.Equals(hEvent) Then

The rest is fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top