473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Delegate struct

Hi,

I have the following code defined:

<StructLayout(LayoutKind.Sequential)> Public Structure Callbacks
<MarshalAs(UnmanagedType.FunctionPtr)> Public OnError As
OnErrorDelegate
<MarshalAs(UnmanagedType.FunctionPtr)> Public OnConnect As
OnConnectDelegate
End Structure

Dim pShuttleCallbacks As USTradeShuttleCallbacks
pCallbacks.OnConnect = New OnConnectDelegate(AddressOf OnConnect)
pCallbacks.OnError = New OnErrorDelegate(AddressOf OnError)
.....

and the "On..." fintion definitions.

when I call a function in my Unmanaged code in c++ with the callbacks
struct I get an NullReferenceException error, do I have to define
something to keep the delegate ?
Nov 21 '05 #1
2 1104
Hi,

Had a similar problem with a callback function. It would work the
first time and forget where to go for the second. I created a form level
variable of the delegate type and used that in the structure.

Ken
--------------
"Aviram Gilboa" <Av**********@gmail.com> wrote in message
news:dd**************************@posting.google.c om...
Hi,

I have the following code defined:

<StructLayout(LayoutKind.Sequential)> Public Structure Callbacks
<MarshalAs(UnmanagedType.FunctionPtr)> Public OnError As
OnErrorDelegate
<MarshalAs(UnmanagedType.FunctionPtr)> Public OnConnect As
OnConnectDelegate
End Structure

Dim pShuttleCallbacks As USTradeShuttleCallbacks
pCallbacks.OnConnect = New OnConnectDelegate(AddressOf OnConnect)
pCallbacks.OnError = New OnErrorDelegate(AddressOf OnError)
.....

and the "On..." fintion definitions.

when I call a function in my Unmanaged code in c++ with the callbacks
struct I get an NullReferenceException error, do I have to define
something to keep the delegate ?
Nov 21 '05 #2
This is because the delegate is actually a variable and when it goes out of
scope it is gone. It needs to have a longer life. Declare it as static in
the procedure or at the form level so that it does not go out of scope and
get GC'd

"Aviram Gilboa" <Av**********@gmail.com> wrote in message
news:dd**************************@posting.google.c om...
Hi,

I have the following code defined:

<StructLayout(LayoutKind.Sequential)> Public Structure Callbacks
<MarshalAs(UnmanagedType.FunctionPtr)> Public OnError As
OnErrorDelegate
<MarshalAs(UnmanagedType.FunctionPtr)> Public OnConnect As
OnConnectDelegate
End Structure

Dim pShuttleCallbacks As USTradeShuttleCallbacks
pCallbacks.OnConnect = New OnConnectDelegate(AddressOf OnConnect)
pCallbacks.OnError = New OnErrorDelegate(AddressOf OnError)
....

and the "On..." fintion definitions.

when I call a function in my Unmanaged code in c++ with the callbacks
struct I get an NullReferenceException error, do I have to define
something to keep the delegate ?

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
17
by: Bob Simoneau | last post by:
I am having a hard time with delegates, but here I must pass a delegate and a pointer to a structure. I am totally lost. Passing the pointer to rlistsys_resp_func and RLISTSYS_DETAIL is where I...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
9
by: Bob Altman | last post by:
I'm trying to call a function on a background thread in a C++/CLI (VS 2005) application compiled with /clr. I store a delegate reference in a module-level gcroot. When I call BeginInvoke on the...
7
by: Eric | last post by:
I have a number of static helper methods to read in data from an IDataReader; one of the services provided by this helper class is the ability to return a nullable type as needed, for instance:...
0
by: Gezan | last post by:
Hi all, I have a question. The struct below is used to handle the callback of a Unmannaged c++ dll. struct CALLBACK { typedef BOOL (*CALL_AP) (LPVOID pThis, GT_HANDLE hFrom, GT_ERROR...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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,...

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.