New to Delegates. Having Issues

G

Guest

I have not used Delegates before and I am having trouble using them in my
app. Here is what I am doing.

1. I have created a C# InterOp Dll that I make calls to, that in turn calls
methods in a 3rd party WIN32 API dll. My InterOp complies fine with no
errors. Here is the lines of code where I use my Delegate in my InterOp:
a. public delegate int CallbackFunction([MarshalAs(UnmanagedType.LPWStr)]
string reason, [MarshalAs(UnmanagedType.LPWStr)] string phoneNumber,
[MarshalAs(UnmanagedType.LPWStr)] string columnNames,
[MarshalAs(UnmanagedType.LPWStr)] string fieldValues);

b. [DllImport(DLLName, EntryPoint = "Logon", CallingConvention =
CallingConvention.Cdecl, CharSet = CharSet.Auto, SetLastError = true)]
c. public static extern int Logon(string agentID, string stationID,
[MarshalAs(UnmanagedType.FunctionPtr)] CallbackFunction callbackFunction);

2. I have a VB.net App which I have added my C# InterOp reference to. Here
is the code snippet of the Function in my VB.net App that uses my InterOp:
a. Private Sub cmdLogon_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdLogon.Click

b. Dim CallBack As Interop.pinvoke.CallbackFunction

c. agentIDString = TextAgentID.Text
d. stationIDString = TextStationID.Text

e. CallBack = AddressOf CallBackfunction
f. currentResultInteger = Interop.pinvoke.Logon(agentIDString,
stationIDString, CallBack)

g. End Sub


Can anyone show me what I am doing wrong and how to correct the following
error when I click the cmdLogon button:

CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type
'Interop.!Interop.pinvoke+CallbackFunction::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.

I’m sure it’s something obvious that I am blind to that I am not seeing at
the moment.

Thanks,
 
D

David Madden

Please attach the necessary files, such as the .cs file, so that we can load
the program and attempt to understand it better. It is hard to read a cut
and paste for coding in a newsgroup. Sending the files and commenting on
the problem in the newsgroup will be easier for us.

Thanks.
 
C

Chris Dunaway

I have not used Delegates before and I am having trouble using them in my
app. Here is what I am doing.

1. I have created a C# InterOp Dll that I make calls to, that in turn calls
methods in a 3rd party WIN32 API dll. My InterOp complies fine with no
errors. Here is the lines of code where I use my Delegate in my InterOp:
a. public delegate int CallbackFunction([MarshalAs(UnmanagedType.LPWStr)]
string reason, [MarshalAs(UnmanagedType.LPWStr)] string phoneNumber,
[MarshalAs(UnmanagedType.LPWStr)] string columnNames,
[MarshalAs(UnmanagedType.LPWStr)] string fieldValues);

b. [DllImport(DLLName, EntryPoint = "Logon", CallingConvention =
CallingConvention.Cdecl, CharSet = CharSet.Auto, SetLastError = true)]
c. public static extern int Logon(string agentID, string stationID,
[MarshalAs(UnmanagedType.FunctionPtr)] CallbackFunction callbackFunction);

2. I have a VB.net App which I have added my C# InterOp reference to. Here
is the code snippet of the Function in my VB.net App that uses my InterOp:
a. Private Sub cmdLogon_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdLogon.Click

b. Dim CallBack As Interop.pinvoke.CallbackFunction

c. agentIDString = TextAgentID.Text
d. stationIDString = TextStationID.Text

e. CallBack = AddressOf CallBackfunction
f. currentResultInteger = Interop.pinvoke.Logon(agentIDString,
stationIDString, CallBack)

g. End Sub

Can anyone show me what I am doing wrong and how to correct the following
error when I click the cmdLogon button:

CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type
'Interop.!Interop.pinvoke+CallbackFunction::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.

I'm sure it's something obvious that I am blind to that I am not seeing at
the moment.

Thanks,

Just a shot in the dark, but, you have declared the CallBack variable
locally in the button click handler. After you call Logon, you exit
the button click so the CallBack variable is now out of scope, perhaps
even before it can be called. Maybe declaring the CallBack variable
at a more global scope would work.

Chris
 
G

Guest

Hi Chris,

That suggestion fixed the problem, meaning, my error went a way now that I
have a static reference to my Delegate. However, my CallbackFunction does
not return any thing. Not sure why.

Chris Dunaway said:
I have not used Delegates before and I am having trouble using them in my
app. Here is what I am doing.

1. I have created a C# InterOp Dll that I make calls to, that in turn calls
methods in a 3rd party WIN32 API dll. My InterOp complies fine with no
errors. Here is the lines of code where I use my Delegate in my InterOp:
a. public delegate int CallbackFunction([MarshalAs(UnmanagedType.LPWStr)]
string reason, [MarshalAs(UnmanagedType.LPWStr)] string phoneNumber,
[MarshalAs(UnmanagedType.LPWStr)] string columnNames,
[MarshalAs(UnmanagedType.LPWStr)] string fieldValues);

b. [DllImport(DLLName, EntryPoint = "Logon", CallingConvention =
CallingConvention.Cdecl, CharSet = CharSet.Auto, SetLastError = true)]
c. public static extern int Logon(string agentID, string stationID,
[MarshalAs(UnmanagedType.FunctionPtr)] CallbackFunction callbackFunction);

2. I have a VB.net App which I have added my C# InterOp reference to. Here
is the code snippet of the Function in my VB.net App that uses my InterOp:
a. Private Sub cmdLogon_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdLogon.Click

b. Dim CallBack As Interop.pinvoke.CallbackFunction

c. agentIDString = TextAgentID.Text
d. stationIDString = TextStationID.Text

e. CallBack = AddressOf CallBackfunction
f. currentResultInteger = Interop.pinvoke.Logon(agentIDString,
stationIDString, CallBack)

g. End Sub

Can anyone show me what I am doing wrong and how to correct the following
error when I click the cmdLogon button:

CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type
'Interop.!Interop.pinvoke+CallbackFunction::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.

I'm sure it's something obvious that I am blind to that I am not seeing at
the moment.

Thanks,

Just a shot in the dark, but, you have declared the CallBack variable
locally in the button click handler. After you call Logon, you exit
the button click so the CallBack variable is now out of scope, perhaps
even before it can be called. Maybe declaring the CallBack variable
at a more global scope would work.

Chris
 

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