473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Referencing

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


Nov 16 '05 #1
3 1548
"Nico Gerber" <ng*****@telkomsa.net> wrote in message
news:cl**********@ctb-nnrp2.saix.net...
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
Nov 16 '05 #2
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" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:#q**************@TK2MSFTNGP10.phx.gbl...
"Nico Gerber" <ng*****@telkomsa.net> wrote in message
news:cl**********@ctb-nnrp2.saix.net...
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

Nov 16 '05 #3
"Nico Gerber" <ng*****@telkomsa.net> wrote in message
news:cl**********@ctb-nnrp2.saix.net...
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
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: xcomm | last post by:
Hi All, <?php $vars= array("_SERVER","_SERVER","_SERVER","_SERVER","_SERVER","_SERVER"); foreach($vars as $var) { if(isset($$var))echo("$var: ${$var}<br>\n"); } ?> php.net:
1
by: student | last post by:
while compiling on: SunOS 5.8 Generic_108528-27 sun4u sparc SUNW,Sun-Fire-280R I get the following error: Undefined first referenced symbol ...
2
by: sreddy | last post by:
I am trying to write a sql query on self referencing table. Just to brief ..Database is related to a Hiring department of the Qwest company. I need to generate a Report used by in HR...
2
by: Simon | last post by:
Am using the following code. <script language="JavaScript1.2"> function setquantity(productindex,productquantity) { //create the reference object irefname_none = eval("document." +...
6
by: jstaggs39 | last post by:
I want to create a Dcount and an If...Then...Else statement to count the number of records in a table based on the date that is entered to run the form. The If....Else statment comes in because if...
6
by: Mikey_Doc | last post by:
Hi We are running cms 2002, Framework 1.0 with Visual studio 2002. We have just upgraded to Framework 1.1 and visual studio 2003. All of our database connection strings are stored within the...
3
by: gary | last post by:
Hi, I am trying to reference an anchor in a user control with a url. This worked in 1.1 but no longer works in 2.0. The ascx control is located in a "/include" folder If you have a...
9
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of...
1
by: Tim F | last post by:
Problem: I'm receiving the error "File or assembly name XXXXX or one of its dependencies, was not found." when trying to execute code in an assmebly that has both a strong-name and has been...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.