Hi Nicholas,
I hope this is what you mean:
====================================
Enumerators and Structures:
public enum EventType
{
evtState,
evtMesg,
evtTrans
}
public struct Event
{
public int timestamp;
public EventType event;
public short device;
public int status;
public int inputid;
public Event( int timestamp, EventType event, short device, int status,
int inputid )
{
this.timestamp = timestamp;
this.event = event;
this.device = device;
this.status = status;
this.inputid = inputid;
}
}
====================================
DriverUnit.cs:
public class DriverUnit
{
unsafe private void Message ( int msg )
{
Event myEvent = new Event();
while ( Get Event (myEvent) != 1 )
{
// do work
}
}
[DllImport("rDriver.dll", CharSet=CharSet.Auto)]
private static extern int GetEvent ( Event pEvent );
}
====================================
rDriver.cpp:
long GetEvent ( Event* pEvent )
{
dManager* pManager = dManager::GetInstance();
if ( pManager )
{
return pManager->GetEvent2( pEvent );
}
else
{
return -1;
}
}
====================================
long GetEvent2 (Event* pEvent)
{
return itsEventQueue.Remove( pEvent )
}
====================================
int virtualQueue<T>::Remove ( T* element )
{
// itsRemovePoint contains data retrived from hardware in the format of
struct type 'Event'
*element = *itsRemovePoint;
... // continue running code
// I get a 'System.NullReferenceException' at the above line
}
====================================
Notes:
The following is the process flow:
In 'Message' function of DriverUnit.cs a struct of type Event is created.
It is then passed through the DLLImport interface to the 'GetEvent' method
that resides in rDriver.cpp which is part of the rDriver.dll. When
'GetEvent' is run, the value of the pointer 'pEvent' is 0x00000000.
Eventually, 'Remove' is called which removes data retrieved from hardware
and assigns it to the location of element. However, I receive a
'System.NullReferenceException' when this line of code is run.
Thanks for your help,
Chris
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Chris,
Can you post the definition of the structure and the functions that
you are passing the structure to? This would help immensely.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com
"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl... Hi,
I think I'm having some problems here with garbage collection.
Currently, I have the following code:
public struct Event
{
public int timestamp;
public EventType event;
public short device;
public int status;
public int input;
}
and the following method using the above struct:
unsafe private void Message( int msg )
{
Event myEvent = new Event();
while ( GetEvent(myEvent) != -1 )
{
// do some work
}
}
GetEvent is a call to an external DLL written in C++. When I send the
myEvent reference to the DLL, I receive a
'System.NullReferenceException'. I believe this is because that myEvent gets garbage collected shortly
after the function call to the dll. Am I correct? How do I prevent the
struct (or reference to the struct) from being garbage collected until after
the function call is complete?
Thanks in advance,
Chris