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("rDri ver.dll", CharSet=CharSet .Auto)]
private static extern int GetEvent ( Event pEvent );
}
=============== =============== ======
rDriver.cpp:
long GetEvent ( Event* pEvent )
{
dManager* pManager = dManager::GetIn stance();
if ( pManager )
{
return pManager->GetEvent2( pEvent );
}
else
{
return -1;
}
}
=============== =============== ======
long GetEvent2 (Event* pEvent)
{
return itsEventQueue.R emove( 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.NullRef erenceException ' 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.NullRef erenceException ' when this line of code is run.
Thanks for your help,
Chris
"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote
in message news:%2******** ********@TK2MSF TNGP12.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************* *@exisconsultin g.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******** ******@TK2MSFTN GP10.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(myEven t) != -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.NullRef erenceException '. 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