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,
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,