Some general .NET array and interop questions

G

Guest

All

I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that I'm not sure are specific anomolies that I'm having, or if they are specific known issues I'm facing.

1. In VB.NET, are arrays stored on the stack or the heap? Why must arrays of value types be boxed
in order for them to be (effectively) passed by reference

2. In VB.NET, if objects are 'always' passed by reference, then what precisely is the difference between passing an object by value or by reference

3. I've noticed that there are many COM DLLs that just appear to be 'wrappers' to plain DLLs. If the functionality I really want is really in the DLL, am I better off using P/Invoke on the DLL, or am I better off using the ActiveX component? Can I, in the same code, mix the P/Invoke and COM calls (again in, VB.NET)

4. I have a COM component that accepts a pointer to a structure. The *only* way I can get it to work, however, is if I box an array of the same size as the structure, and pass that into the COM function. If I declare my own parallel structure (sometimes using StructLayout), I get an "Invalid memory" or "Null Reference" exception. Does this behaviour sound correct? What reason might there be for this behaviour

I have another COM component that takes a pointer to a structure, but the COM component itself does the memory allocation. How can I get this structure (ideally a shallow copy, but I'll settle for a deep copy --- I just want the data) back into .NET?
 
M

Mattias Sjögren

Ross,
1. In VB.NET, are arrays stored on the stack or the heap?

Arrays are reference types, so the array instance data (the array
elements) are on the heap. But you can of course have a local
reference to the array on the stack.

Why must arrays of value types be boxed
in order for them to be (effectively) passed by reference?

They don't. Arrays never have to be boxed, again because they are
reference types.

Also, if you have an array of a value type, the items don't have to be
boxed to be stored in the array.

The only situation where you have boxing involved is if you have an
array of Object and want to store value type instances in there.

2. In VB.NET, if objects are 'always' passed by reference, then what precisely is the difference between passing an object by value or by reference?

I suggest you read http://www.yoda.arachsys.com/csharp/parameters.html

It's written from a C# perspective, but the concepts are the same in
VB.NET.

am I better off using P/Invoke on the DLL, or am I better off using the ActiveX component?

That's hard to give a general answer to, it depends.

The COM wrapper may be significantly easier to use. Such as CAPICOM
versus Crypto API.

Can I, in the same code, mix the P/Invoke and COM calls (again in, VB.NET).
Yes


The *only* way I can get it to work, however, is if I box an array
of the same size as the structure, and pass that into the COM function.

As I said above, you can't box arrays. So what are you really doing?

If I declare my own parallel structure (sometimes using StructLayout), I get an
"Invalid memory" or "Null Reference" exception. Does this behaviour sound correct?
What reason might there be for this behaviour?

No, it sounds like you declared something incorrectly.

I have another COM component that takes a pointer to a structure, but the COM component itself does the memory allocation.
How can I get this structure (ideally a shallow copy, but I'll settle for a deep copy --- I just want the data) back into .NET?

Try using System.Runtime.InteropServices.Marshal.PtrToStructure()



Mattias
 

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