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
198 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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