Solved

How do I create a 2-dimensional Array in a fixed location (on the stack?) so it can be passed to a DLL in "unsafe" mode.

Posted on 2006-10-20
4
193 Views
Last Modified: 2010-04-16
I need to create a 2-dimensional array of bytes (byte[,] bfrArray) in my main C# project but I need it in a fixed location so I can pass it to a DLL that is written in C++ and is built as "unsafe". So I guess there are two questions: (a) How do I create the array, (b) how do I get it marshalled through tho the unsafe DLL.

I am writing the main project in C# and the DLL is in C++ (it is legacy code).  Both projects are devloped in Visual Studio 2005.
0
Comment
Question by:Finch
4 Comments
 
LVL 8

Expert Comment

by:bramsquad
Comment Utility
i dont know if creating your project in its own location is what you want.  check this post out...

http://archives.neohapsis.com/archives/microsoft/various/dcom/2001-q3/0279.html

pretty much what it says, is that every instance of the com object that is created is created in its own address space.  therefore you wont have any of your global/static variables being accessed by more than one thread.

how do you do this?

not quite sure, but this should get you started (or other experts thinking)

~b
0
 

Author Comment

by:Finch
Comment Utility
I'm not trying to create a project in it's own space.  I'm trying to create an array in a fixed location that does not get moved around do to garbage collection or any other OS function.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
Comment Utility
This is not my forte...

But I'm pretty sure you need to "Pin" the array so it doesn't get moved around:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21097104.html

MSDN Reference:
http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.aspx

Don't forget to call Free() on your GCHandle when you are done with it...

0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
Comment Utility
There are three ways to do this:
1) Using fixed keyword
2) Using pinned object - see Idle_Mind's post
3) Using Marshal class: see Marshal.AllocHGlobal and Marshal.Copy methods.

Writing interoperability code, it is necessary to see unmanaged function declaration. Please post it and describe what data does it excppect.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now