Marshalling from unmanaged to managed code

V

Victor

Hi everybody,

in my C#-class Form1 there are following elements:

a)
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private class _data {
[MarshalAs(UnmanagedType.ByValArray,SizeConst=90000)]
public ushort[] buf;

public _data() {
buf = new ushort[90000];
}
}

_data data;

b)
[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal unsafe struct _pult {
public byte instr;
public void* pointer;
}

_pult pult;

b)
A thread with following contents (partially omitted):

private unsafe void threadFunc() {
data = new _data;
fixed (_pult* ptr = &pult) {
ptr->instr = 1;
IntPtr point = Marshal.AllocHGlobal(90000);
ptr->pointer = (void*)point;
while (ptr->instr) {
Marshal.PtrToStructure(point,dataMic);
Thread.Sleep(25);
}
Marshal.FreeHGlobal(point);
}
}

There is another thread coded inside a C++ DLL; this thread
writes a regularly updated array of 30,000 USHORT's to the
address ptr->pointer. I hope to be able to see those values
within my managed code as members of the array 'data.buf'
indexed from [0] thru [29999].
The reality is somehow different : I do see the first
16384 (!) array elements [0]...[16383], whereas the rest
remains rigidly 0, notwithstanding the real values in the
memory area on the address 'ptr->pointer'...

What could cause this behaviour?

Thank you in advance for your time and attention to my problem.

Regards
Victor
 
W

Willy Denoyette [MVP]

Hard to tell if you post incomplete code.
I just wonder who's synchronizing access to the shared memory block?
I also don't see how you pass the address of the bufer to unmanaged code.

Willy.
 
V

Victor

Thank you Willy!

I found my mistake myself; it was of quite different nature
and had nothing to do with the marshalling.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top