By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

Event help needed

P: n/a
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!
Nov 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
inside of a sub (probably new())

addhandler %printername%.mfx.complete, addressof PrintJobComplete
Private Sub PrintJobComplete
'call the rest of your code here
end sub
--
--Eric Cathell, MCSA
"Jon" <ru******@msn.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
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!

Nov 23 '05 #2

P: n/a
Jon
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

I did try your method, but the %printername% is just a string name of the
printer. I'm not very familiar with using named events, sorry.
"ECathell" <ec******@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
inside of a sub (probably new())

addhandler %printername%.mfx.complete, addressof PrintJobComplete
Private Sub PrintJobComplete
'call the rest of your code here
end sub
--
--Eric Cathell, MCSA
"Jon" <ru******@msn.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
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!


Nov 23 '05 #3

P: n/a

Jon wrote:
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 :)

--
Larry Lard
Replies to group please

Nov 23 '05 #4

P: n/a
Jon
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
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Jon wrote:
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 :)

--
Larry Lard
Replies to group please

Nov 23 '05 #5

P: n/a

Jon wrote:
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.


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
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Jon wrote:
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 :)

--
Larry Lard
Replies to group please


Nov 23 '05 #6

P: n/a
Jon
Thanks much!!
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

Jon wrote:
Yes, I'm sorry, these are win32 events. My conversion follows


I inferred the necessary Imports statements from the C# source :)

Nov 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.