By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,462 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,462 IT Pros & Developers. It's quick & easy.

shared memory

P: n/a
Hy guys!!

I have an structures array, i need to send it memory address to a dll
function, is it possible in c# ?

my dll function gets a pointer to structures array

im using

[DllImport("hilodll.dll")] public static extern void main(INSTANCIA
*myInstance, int numInstanacia); <<< at this line compiler sends error,
why?

INSTANCIA [ ] id = new INSTANCIA[10];

main(id , 10);

but parameter myInstance never gets array address, compiler says it couldn't
get array address

Jul 4 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jul 4, 9:08*pm, "Rick" <zatank...@gmail.comwrote:
Hy guys!!

I have an structures array, i need to send it memory address to a dll
function, is it possible in c# ?

my dll function gets a pointer to structures array

im using

[DllImport("hilodll.dll")] public static extern void main(INSTANCIA
*myInstance, int numInstanacia); * <<< at this line compiler sends error,
why?

INSTANCIA [ ] id = new INSTANCIA[10];

main(id , 10);

but parameter myInstance never gets array address, compiler says it couldn't
get array address
If you really want to do it that way, you need three things:

1. Compile with /unsafe (you can't use pointers otherwise).
2. Declare the [DllImport] method as "unsafe".
3. Pin the array using "fixed" statement:

fixed (INSTANCIA [ ] id = new INSTANCIA[10]) { main(id, 10); }

Unlike C/C++, there is no implicit array->pointer conversion in C#; it
is only allowed within the scope of initializer expression in "fixed"
statement. Read the MSDN regarding the latter on a more detailed
explanation.

On the other hand, a simpler way to do the same is to declare the
imported method as taking an array rather than a pointer:

[DllImport("hilodll.dll")] public static extern void main([In, Out]
INSTANCIA[] myInstance, int numInstanacia);

Note the [In, Out] thingy - it indicates that the function will both
read from and write into the array. If this is not the case, you will
need to drop In or Out as needed.

MSDN articles with more info and examples:

http://msdn.microsoft.com/en-us/library/dd93y453.aspx
http://msdn.microsoft.com/en-us/library/hk9wyw21.aspx
Jul 4 '08 #2

P: n/a
On Jul 4, 9:52*pm, Pavel Minaev <int...@gmail.comwrote:
fixed (INSTANCIA [ ] id = new INSTANCIA[10]) { main(id, 10); }
Sorry, this should read:

fixed (INSTANCIA* id = new INSTANCIA[10]) { main(id, 10); }
Jul 4 '08 #3

P: n/a

"Pavel Minaev" <in****@gmail.comescribió en el mensaje
news:ee**********************************@79g2000h sk.googlegroups.com...
On Jul 4, 9:52 pm, Pavel Minaev <int...@gmail.comwrote:
fixed (INSTANCIA [ ] id = new INSTANCIA[10]) { main(id, 10); }
Sorry, this should read:

fixed (INSTANCIA* id = new INSTANCIA[10]) { main(id, 10); }
Thanks a lot Pavel, i'll start to read right now =)
Jul 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.