Referencing

N

Nico Gerber

I have an already developed C# application, that needs to pass a pointer to
an array of bytes (of size 50), to another already developed C++ application
hosted in a DLL. My array of bytes are hosted in an unsafe static class to
force the C# garbage collector not to move it around, as the C++ application
will be responsible to update the array during runtime. My structure is
defined as follows:

[StructLayout(LayoutKind.Sequential)]
public struct DATA
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=50)]
public statis byte[] myArray;
}

I attempt to pass the reference of the array to the C++ application in the
following manner:

fixed (byte *address = &MyStaticClass.DATA.myArray[0])
{
MyApplication.SetArrayReference(address);
}

Now, when I attempt to pass a reference to this array to the C++ application
(see insert above), the following exception occurs:
System.NullReferenceException >> "Object reference not set to an instance of
an object.". Any idea what I am doing wrong, and how this can be solved?

Regards
Nico Gerber
 
M

Michael Culley

Nico Gerber said:
Now, when I attempt to pass a reference to this array to the C++ application
(see insert above), the following exception occurs:
System.NullReferenceException >> "Object reference not set to an instance of
an object.". Any idea what I am doing wrong, and how this can be solved?

Has you array been created using new or some other method? If that's not the
problem, what is null and on what error does the line occur. What is your
declare statement for the API call?

myArray = new byte[50];

Michael Culley
 
N

Nico Gerber

My problem is that the array should actually 'live' inside the structure ...
by calling the 'new' operator I understand that the memory is allocated on
the heap somewhere, with only a reference to it from the structure? Are
there any way to ensure that the allocated memory is actually 'inside' the
structure?

Michael Culley said:
Nico Gerber said:
Now, when I attempt to pass a reference to this array to the C++ application
(see insert above), the following exception occurs:
System.NullReferenceException >> "Object reference not set to an
instance
of
an object.". Any idea what I am doing wrong, and how this can be solved?

Has you array been created using new or some other method? If that's not the
problem, what is null and on what error does the line occur. What is your
declare statement for the API call?

myArray = new byte[50];

Michael Culley
 
M

Michael Culley

Nico Gerber said:
My problem is that the array should actually 'live' inside the structure ....
by calling the 'new' operator I understand that the memory is allocated on
the heap somewhere, with only a reference to it from the structure? Are
there any way to ensure that the allocated memory is actually 'inside' the
structure?

Not sure about that one. If it's only a few elements in the array you could
declare them all as Value1, Value2 etc but that would be cumbersome over a
cerain size. You could play around with the FieldOffset attribute and the
StructLayout attribute. I'm not sure what happens if you leave a hole in
your struct, maybe that memory is free to use, eg

[StructLayout(LayoutKind.Explicit)]
private struct ABC
{
int SomeValue;
[FieldOffset(4)]
byte x;//this will be the array
[FieldOffset(100)]
int SomeOtherValue;
}

Michael Culley
 

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