473,473 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

New to Delegates. Having Issues

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
3 5079
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
8
by: STom | last post by:
I have a C# Winforms app that has 5 Winforms, lets say A through E. A: Data entry. When data is entered here in any field, values are updated on forms C, D, E.(Not B) B: Data entry form. When...
2
by: Marcos Stefanakopolus | last post by:
In C# is there any way to use the concept of delegates to have multiple implementations of a particular block of code, so that you can choose between them without the overhead of possibly expensive...
14
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
4
by: douglass_davis | last post by:
I'm really not sure when I might have to use one. can any one give me an example of when they should be used? -- http://www.douglassdavis.com
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
10
by: Roger Frost | last post by:
Since we are currently on the subject of multi-threading in a different thread, I have a question about delegates. When I use delegates, I just create one for each different parameter set that I...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.