Hi,
I just ask because I really didn't know what happens with my code.
I have a thread that polls hardware events in a loop calling an unmanaged
function. The event structure has unions with value and reference types and
to receive the event structure I use IntPtr: (non-blittable)
For instance:
int eventSize = 0;
IntPtr event;
while(threadActive)
{
event = Marshal.AllocHGlobal(256);
int result = StaticClass.UnmanagedFunction(IntPtr event, ref eventSize);
....
(parse IntPtr)
...
Marshal.FreeHGlobal(event);
}
Now, I want to parse the event variable asynchronously. So I have to
encapsulate it in some class and this class is passe in
ThreadPool.QueueUserWorkItem method:
{
....
event = Marshal.AllocHGlobal(256);
int result = StaticClass.UnmanagedFunction(IntPtr event, ref eventSize);
MyContainerClass c = new MyContainerClass(ref IntPtr); <----- What happes
here!
ThreadPool.QueueUserWorkItem(new WaitCallback(MyWaitCallBackMethod),
MyContainerClass);
....
}
void MyWaitCallBackMethod(object state)
{
MyContainerClass c = (MyContainerClass) state;
IntPtr event = c.event;
...
(parse IntPtr)
...
Marshal.FreeHGlobal(event);
}
That's why I ask about "ref" value parameters.
Thanks!
--
Andre Azevedo
Shailen Sukul said:
If your goal is to simply find a way to pass in integer pointers, then the
following code will work. I would like to point out that using pointers
and
unsafe code is NOT recommended and should be reserved for special case
scenarioes.
You will also need to check the "Allow unsafe code" option in the Build
tab
of your project properties.
unsafe class MyClass
{
public int* i;
public MyClass(int* I)
{
i = I;
}
}
class Program
{
unsafe static void Main(string[] args)
{
int i = 50;
MyClass c = new MyClass(&i);
Console.WriteLine(*c.i);
i = 10;
Console.WriteLine(*c.i);
Console.ReadLine();
}
}
HTH
--
Good luck!
Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(
http://www.ashlen.net.au)
Andre Azevedo said:
Hi all,
With the followin code:
class MyClass
{
public int i;
public MyClass(ref int I)
{
i = I;
}
}
class Program
{
static void Main(string[] args)
{
int i = 50;
MyClass c = new MyClass(ref i);
Console.WriteLine(c.i);
i = 10;
Console.WriteLine(c.i);
Console.ReadLine();
}
}
I´ve got:
50
50
The i field from c object didn´t change its value.
The i field doesn´t point to i address.
C# copies the value from the i variable to the object heap field i.
Is this correct? Heap object doesn´t point to stack variables?
Please help.
TIA,