shared memory

R

Rick

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
 
P

Pavel Minaev

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
 
P

Pavel Minaev

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

Sorry, this should read:

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

Rick

"Pavel Minaev" <[email protected]> escribió en el mensaje
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 =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top