Can someone please help with this?

W

William Morgan

Public Declare Sub ydec_set_callback Lib "yDecLib.dll" (ByVal CallbackFunc As Long)
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpvDest As String, ByVal lpvSource As String, ByVal cbCopy As Long)

Private Function yDecEventHandler(ByVal MsgType As Long, ByVal Data As Long, ByVal Msg As Long, ByVal MsgSize As Long) As Long
' this function handles all events fired by the decoder library
' for most events, returning 0 will cancel the decoding process; returning 1
' will continue normally
Dim MsgStr As String

' allocate buffer to receive text message string, then copy the message
' to our buffer - be sure to check MsgSize first; some events don't have
' message text associated with them

If MsgSize > 0 Then
Dim i As Integer
MsgStr = New String(" ", MsgSize)
CopyMemory(MsgStr, Msg, MsgSize)
End If
' on YDEC_MSG_PROGRESS events, returning 0 will abort the decoding process
yDecEventHandler = 1


' determine what type of message this is, and act appropriately

Select Case MsgType

'Case Is = YDEC_MSG_ADD_OK
' file was successfully added to the input file list
' you probably don't need to handle this, but it's here anyway
' frmMain.lstMessages.AddItem("Added file: " & MsgStr & " (file #" & Data + 1 & ")")

Case Is = 1
' occasionally called to allow your application to update a
' progress meter, refresh a window, etc.
' frmMain.ProgressBar.Value = Data

Case Is = 2
' starting to decode a file part
'frmMain.lstMessages.AddItem("Decoding part #" & Data & " of file " & MsgStr)

Case Is = 3 ' YDEC_MSG_PART_OK
' file part was decoded successfully
'frmMain.lstMessages.AddItem("Successfully decoded part # " & Data & " of file " & MsgStr)

Case Is = 4
' file part was corrupt
'frmMain.lstMessages.AddItem("Error: part # " & Data & " of file " & MsgStr & " is corrupt!")

Case Is = 5 'YDEC_MSG_FILE_OK
' entire file was decoded successfully
'frmMain.lstMessages.AddItem("Successfully decoded file " & MsgStr & " (" & Data & " bytes)")
MsgBox("Filedone")
Case Is = 6 'YDEC_MSG_FILE_INCOMPLETE
' file was missing one or more parts
'frmMain.lstMessages.AddItem("Error: file " & MsgStr & " is missing one or more parts!")

Case Is = 7 'YDEC_MSG_FILE_CORRUPT
' file was complete, but corrupt
'frmMain.lstMessages.AddItem("Error: file " & MsgStr & " is corrupt! (" & Data & " bytes)")

Case Is = 8 'YDEC_MSG_NOTICE
' miscellaneous warning messages from the decoder library
MsgBox("Notice: " & MsgStr)

Case Else
MsgBox("Received unsupported event message from yDecoder library!", vbOKOnly, "Error")
End Select
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
ydec_set_callback(AddressOf yDecEventHandler)

End Sub

how do you set the callback now that you can't use the addressof?
 
A

Armin Zingler

William Morgan said:
Code:
how do you set the callback now that you can't use the addressof?[/QUOTE]


I haven't examined your code in details but maybe the following topic helps:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconusingcallbackfunctions.asp
 
J

Jay B. Harlow [MVP - Outlook]

William,
how do you set the callback now that you can't use the addressof?
The CallbackFunc parameter needs to be defined in the terms of a Delegate to
use the AddressOf expression. AddressOf is the correct expression to use
when setting the callback.

Public Delegate Function MyCallbackFunc(ByVal MsgType As Long, ByVal Data As
Long, ByVal Msg As Long, ByVal MsgSize As Long) As Long
Public Declare Sub ydec_set_callback Lib "yDecLib.dll" (ByVal CallbackFunc
As MyCallbackFunc)

Are you certain about all those Long values? Remember that Long in VB.NET is
a 64bit integer, while a Integer is 32bit integer. Also I have to wonder if
you want System.IntPtr for some of the parameters, as System.IntPtr is the
size of a pointer to memory.

Public Delegate Function MyCallbackFunc(ByVal MsgType As Integer, ByVal Data
As Integer, ByVal Msg As IntPtr, ByVal MsgSize As Integer) As Integer
MsgStr = New String(" ", MsgSize)
CopyMemory(MsgStr, Msg, MsgSize)
Can you say, bad! Very bad! :-| Hopefully your app will blow up immediately
before you damage the managed heap, at worst you will have obscure runtime
errors, many many routines later...

Strings are immutable, you should not use an API that attempts to modify the
contents of a string. You should not use CopyMemory from .NET, as CopyMemory
is an unmanaged API that does not understand Managed Memory, its better to
use the functions in System.Runtime.InteropServices.Marshal that understand
Managed & Unmanaged memory. I would recommend one of the Marshal.PtrToString
functions to convert the Msg parameter to a String.

Use either of, depending on the unmanaged representation of the string.

' Msg is a Unicode string
MsgStr = Marshal.PtrToStringUni(Msg, MsgSize)

' Msg is a ANSI string
MsgStr = Marshal.PtrToStringAnsi(Msg, MsgSize)

Also, I would recommend defining MsgType in terms of an Enum

Public Enum YDEC_MSG
ADD_OK
PROGRESS = 1 ' ?
' Case Is = 2 ?
PART_OK = 3
' Case Is = 4?
FILE_OK = 5
FILE_INCOMPLETE = 6
FILE_CORRUPT = 7
NOTICE = 8
End Enum

Public Delegate Function MyCallbackFunc(ByVal MsgType As YDEC_MSG, ByVal
Data As Integer, ByVal Msg As IntPtr, ByVal MsgSize As Integer) As Integer

Using the enum gives you intellisense, and avoids 'magic numbers' (you deal
with YDEC_MSG.FILE_INCOMPLETE instead of the literal 6).

Hope this helps
Jay

William Morgan said:
Public Declare Sub ydec_set_callback Lib "yDecLib.dll" (ByVal CallbackFunc As Long)
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal
lpvDest As String, ByVal lpvSource As String, ByVal cbCopy As Long)
Private Function yDecEventHandler(ByVal MsgType As Long, ByVal Data As
Long, ByVal Msg As Long, ByVal MsgSize As Long) As Long
' this function handles all events fired by the decoder library
' for most events, returning 0 will cancel the decoding process; returning 1
' will continue normally
Dim MsgStr As String

' allocate buffer to receive text message string, then copy the message
' to our buffer - be sure to check MsgSize first; some events don't have
' message text associated with them

If MsgSize > 0 Then
Dim i As Integer
MsgStr = New String(" ", MsgSize)
CopyMemory(MsgStr, Msg, MsgSize)
End If
' on YDEC_MSG_PROGRESS events, returning 0 will abort the decoding process
yDecEventHandler = 1


' determine what type of message this is, and act appropriately

Select Case MsgType

'Case Is = YDEC_MSG_ADD_OK
' file was successfully added to the input file list
' you probably don't need to handle this, but it's here anyway
' frmMain.lstMessages.AddItem("Added file: " & MsgStr & " (file #" & Data + 1 & ")")

Case Is = 1
' occasionally called to allow your application to update a
' progress meter, refresh a window, etc.
' frmMain.ProgressBar.Value = Data

Case Is = 2
' starting to decode a file part
'frmMain.lstMessages.AddItem("Decoding part #" & Data & " of file " & MsgStr)

Case Is = 3 ' YDEC_MSG_PART_OK
' file part was decoded successfully
'frmMain.lstMessages.AddItem("Successfully decoded part #
" & Data & " of file " & MsgStr)
Case Is = 4
' file part was corrupt
'frmMain.lstMessages.AddItem("Error: part # " & Data & "
of file " & MsgStr & " is corrupt!")
Case Is = 5 'YDEC_MSG_FILE_OK
' entire file was decoded successfully
'frmMain.lstMessages.AddItem("Successfully decoded file "
& MsgStr & " (" & Data & " bytes)")
MsgBox("Filedone")
Case Is = 6 'YDEC_MSG_FILE_INCOMPLETE
' file was missing one or more parts
'frmMain.lstMessages.AddItem("Error: file " & MsgStr & "
is missing one or more parts!")
Case Is = 7 'YDEC_MSG_FILE_CORRUPT
' file was complete, but corrupt
'frmMain.lstMessages.AddItem("Error: file " & MsgStr & "
is corrupt! (" & Data & " bytes)")
Case Is = 8 'YDEC_MSG_NOTICE
' miscellaneous warning messages from the decoder library
MsgBox("Notice: " & MsgStr)

Case Else
MsgBox("Received unsupported event message from
yDecoder library!", vbOKOnly, "Error")
End Select
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
 

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

Top