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
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Expert Comment

by:bramsquad
ID: 17777972
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
ID: 17778070
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 86

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 17778813
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
ID: 17778977
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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