Extern "C" _ declspec (dllexport) void sncagetaudioinput (lpaudioinput & lpaudioinput, unsigned int * pqty) <br/>{</P> <p> If (pqty = NULL) <br/> return; </P> <p> for (unsigned int I = 0; I <(* pqty); I ++) <br/>{< br/> lpaudioinput [I]. p3 = lpaudioinput [I]. p1 + lpaudioinput [I]. p2; <br/>}</P> <p>}After two hours, we finally got it done!
This is done using the pointer, because C ++ needs to dynamically open up the space, and then return the first address of the space to C #
C # The Code is as follows:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. runtime. interopservices; <br/> using system. collections; </P> <p> namespace mytestdll_loader <br/> {<br/> class Program <br/> {<br/> public struct audioinput <br/> {<br/> Public int P1; <br/> Public int P2; <br/> Public int P3; <br/>}</P> <p> [system. runtime. interopservices. dllimport (".. /.. /.. /debug/mytestdll. DLL ", charset = charset. ANSI)] <br/> Public static extern void sncagetaudioinput (ref intptr arr, ref uint32 size); </P> <p> static void main (string [] ARGs) <br/>{</P> <p> uint32 S = 0; </P> <p> array result = array. createinstance (typeof (audioinput), 100); </P> <p> intptr = marshal. unsafeaddrofpinnedarrayelement (result, 0); </P> <p> sncagetaudioinput (ref PTR, ref S); </P> <p> printresult (ref PTR, (INT) s); </P> <p> marshal. release (PTR); </P> <p> console. readkey (); <br/>}</P> <p> // <summary> <br/> // right-click the project and choose Properties> Generate> allow Insecure code <br/> // </Summary> <br/> // <Param name = "Address"> </param> <br/> // <Param name = "size"> </param> <br/> unsafe static private void printresult (ref intptr address, int size) <br/>{< br/> audioinput * presult = (audioinput *) address; </P> <p> for (INT I = 0; I <size; I ++) <br/>{< br/> audioinput OBJ = presult [I]; </P> <p> console. writeline ("p1 = {0}, P2 = {1}, P3 = {2}", obj. p1, obj. p2, obj. p3); <br/>}< br/>
The C ++ code is as follows:
Extern "C" _ declspec (dllexport) void sncagetaudioinput (lpaudioinput & lpaudioinput, unsigned int * pqty) <br/>{</P> <p> If (pqty = NULL) <br/> return; </P> <p> lpaudioinput = new audioinput [4]; <br/> * pqty = 4; </P> <p> for (unsigned int I = 0; I <(* pqty); I ++) <br/>{< br/> lpaudioinput [I]. p1 = I + 10; <br/> lpaudioinput [I]. p2 = I + 1; </P> <p> lpaudioinput [I]. p3 = lpaudioinput [I]. p1 + lpaudioinput [I]. p2; <br/>}</P> <p>}
............ Invincible split line ...............
............ Invincible split line ...............
This is code-safe.
C # The Code is as follows:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. runtime. interopservices; </P> <p> namespace mytestdll_loader <br/> {<br/> class Program <br/> {<br/> public struct audioinput <br/> {<br/> Public int P1; <br/> Public int P2; <br/> Public int P3; <br/>}</P> <p> [system. runtime. interopservices. dllimport (".. /.. /.. /debug/mytestdll. DLL ", charset = charset. ANSI)] <br/> Public static extern void sncagetaudioinput (ref intptr arr, ref uint32 size); </P> <p> static void main (string [] ARGs) <br/>{< br/> array P = array. createinstance (typeof (audioinput), 5); </P> <p> audioinput [] arr = new audioinput [5]; <br/> for (INT I = 0; I <5; I ++) <br/>{< br/> arr [I]. p1 = I; <br/> arr [I]. p2 = I + 1; </P> <p> P. setvalue (ARR [I], I); <br/>}</P> <p> uint32 S = (uint32) Arr. length; </P> <p> intptr = marshal. unsafeaddrofpinnedarrayelement (p, 0); <br/> sncagetaudioinput (ref PTR, ref S); </P> <p> for (INT I = 0; I <p. length; I ++) <br/>{< br/> audioinput OBJ = (audioinput) p. getvalue (I); </P> <p> console. writeline ("p1 = {0}, P2 = {1}, P3 = {2}", obj. p1, obj. p2, obj. p3); <br/>}</P> <p> console. readkey (); <br/>}< br/>
The C ++ Code only needs to replace the method in the above sncagetaudioinput as follows:
If (pqty = NULL) <br/> return; </P> <p> for (unsigned int I = 0; I <(* pqty); I ++) <br/>{< br/> lpaudioinput [I]. p3 = lpaudioinput [I]. p1 + lpaudioinput [I]. p2; <br/>}< br/>