Click here to Skip to main content
16,006,440 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questionour team creating the hotel management project we want concepts of what accounts maintain hotel management Pin
vidyashankar9-Jul-06 23:41
vidyashankar9-Jul-06 23:41 
AnswerRe: our team creating the hotel management project we want concepts of what accounts maintain hotel management Pin
Dave Kreskowiak10-Jul-06 5:14
mveDave Kreskowiak10-Jul-06 5:14 
Questionabout date format [modified] Pin
charan049-Jul-06 23:36
charan049-Jul-06 23:36 
AnswerRe: about date format Pin
Rizwan Bashir10-Jul-06 2:41
Rizwan Bashir10-Jul-06 2:41 
GeneralRe: about date format Pin
charan0410-Jul-06 16:37
charan0410-Jul-06 16:37 
QuestionExporting Emails from Outlook Pin
smarttom999-Jul-06 23:02
smarttom999-Jul-06 23:02 
QuestionVB.NET - How to Drag from Treeview and Drop into DataGrid ? Pin
jo_eylee9-Jul-06 22:00
jo_eylee9-Jul-06 22:00 
Questiontrying to get GC.Keepalive to work Pin
michaelellams9-Jul-06 21:03
michaelellams9-Jul-06 21:03 
Hi,

I've got some code thats unamanged/managed and have some callback handlers in it, linking some old .dll stuff.

I've searched for a while and I've read a bit on garbage collection and delegates and I've tried my best to apply the examples to my code, but to no avail. I still get either the following error message

"A callback was made on a garbage collected delegate of type 'WindowsApplication1!WindowsApplication1.MyConnectHandler::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called."

or, no error message (and the call back excutes successfully) but it then trashes the other bits of code from working properly that are run from the same .dll


This is the code, I've cut out the surplus bits, it has no errors or warnings...... just doesn't work Frown | :(

In this sample basically the user would select a comport from a drop down box and click connect. I know it actually connects as I can see status from the device its connecting to. However when the device tries to send the message back, it does one of the above errors.

Would greatly appreciate any help as I'm trying to implement this .dll to move away from an unstable module we are being forced to use at the moment.

Michael

imports System.Text

Public Delegate Sub MyConnectHandler(ByVal result As Integer)

Public Class Form1
Inherits System.Windows.Forms.Form
' Declare some variables

Public Declare Auto Sub register_connect_handler Lib "CBM.DLL" Alias "_register_connect_handler" (ByVal FPtr As MyConnectHandler)
Dim MyConnectPtr As MyConnectHandler = AddressOf Connect_Handler


Public Sub Connect_Handler(ByVal result As Integer)
If result = 0 Then
Me.StatusBar1.Text = "Fail"
Call close_com_port()
ElseIf result = 1 Then
Me.StatusBar1.Text = "Fail type 2"
Call close_com_port()
ElseIf result = 2 Then
' success so enable trackbar and inform user
connected = 1
Me.TrackBar1.Enabled = True
Me.StatusBar1.Text = "Connected"
End If
End Sub

' Initialise fields and vars
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, j As Int16
' Launch the connect handler
register_connect_handler(MyConnectPtr)
' Stop it from dying
GC.KeepAlive(MyConnectPtr)
End Sub

' Connect/disconnect as required

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Dim [ascii] As Encoding = Encoding.ASCII
Dim Port As Byte() = [ascii].GetBytes(Me.ComboBox1.Text)
Dim x As Integer

If connected = 0 Then
' connecting, inform user
Me.StatusBar1.Text = "Connecting ..."
' Open the selected comport (DLL call)
x = open_com_port(Port)
' check result of call for success
If x = 0 Then
' failure, inform user
Me.StatusBar1.Text = "Connect failed"
End If
Else
' disconnecting
connected = 0
' close the com port (DLL call)
Call close_com_port()
' inform the user
Me.StatusBar1.Text = "Disconnected"
Me.TrackBar1.Enabled = False
End If
End Sub



End Class


Questionprob in ipc WM_COPYDATA with code _ please solve Pin
K edar V9-Jul-06 21:01
K edar V9-Jul-06 21:01 
AnswerRe: prob in ipc WM_COPYDATA with code _ please solve Pin
Dave Kreskowiak10-Jul-06 4:45
mveDave Kreskowiak10-Jul-06 4:45 
GeneralRe: prob in ipc WM_COPYDATA with code _ please solve Pin
K edar V10-Jul-06 18:30
K edar V10-Jul-06 18:30 
GeneralRe: prob in ipc WM_COPYDATA with code _ please solve Pin
Dave Kreskowiak11-Jul-06 1:47
mveDave Kreskowiak11-Jul-06 1:47 
GeneralRe: prob in ipc WM_COPYDATA with code _ please solve Pin
K edar V11-Jul-06 2:34
K edar V11-Jul-06 2:34 
GeneralRe: prob in ipc WM_COPYDATA with code _ please solve Pin
Dave Kreskowiak11-Jul-06 3:07
mveDave Kreskowiak11-Jul-06 3:07 
GeneralRe: prob in ipc WM_COPYDATA with code _ please solve Pin
K edar V11-Jul-06 18:51
K edar V11-Jul-06 18:51 
QuestionReading/Writing Image Files Socket Problem. Pin
shelal9-Jul-06 20:42
shelal9-Jul-06 20:42 
AnswerRe: Reading/Writing Image Files Socket Problem. Pin
Dave Kreskowiak10-Jul-06 4:42
mveDave Kreskowiak10-Jul-06 4:42 
QuestionDivide Pin
Amarni9-Jul-06 20:02
Amarni9-Jul-06 20:02 
AnswerRe: Divide Pin
Dave Sexton9-Jul-06 21:19
Dave Sexton9-Jul-06 21:19 
GeneralRe: Divide Pin
Amarni9-Jul-06 21:23
Amarni9-Jul-06 21:23 
AnswerRe: Divide Pin
jo_eylee9-Jul-06 22:28
jo_eylee9-Jul-06 22:28 
Questionconnection declaration in class and also how to work with storedprocedures Pin
vengaqua9-Jul-06 19:19
vengaqua9-Jul-06 19:19 
AnswerRe: connection declaration in class and also how to work with storedprocedures Pin
Guffa9-Jul-06 19:33
Guffa9-Jul-06 19:33 
GeneralRe: connection declaration in class and also how to work with storedprocedures Pin
vengaqua9-Jul-06 20:05
vengaqua9-Jul-06 20:05 
AnswerRe: connection declaration in class and also how to work with storedprocedures Pin
Guffa9-Jul-06 23:26
Guffa9-Jul-06 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.