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

New to Delegates. Having Issues

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

May 9 '07 #1
Share this Question
Share on Google+
3 Replies


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

"SAL" <SA*@discussions.microsoft.comwrote in message
news:3E**********************************@microsof t.com...
>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,
May 9 '07 #2

P: n/a
On May 9, 9:07 am, SAL <S...@discussions.microsoft.comwrote:
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

May 9 '07 #3

P: n/a
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" wrote:
On May 9, 9:07 am, SAL <S...@discussions.microsoft.comwrote:
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

May 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.