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
195 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
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 85

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 79
Server Error 11 58
C#.NET and microsoft certification. 3 40
2 questions 10 25
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

15 Experts available now in Live!

Get 1:1 Help Now