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>} 憋了2個小時,終於搞定!
這個是使用的指標搞定的,因為需要C++動態開闢空間,然後將空間首地址返回給C#
C#代碼如下:
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 ptr = 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 /> /// 工程右鍵->屬性->產生->允許不安全的程式碼<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 /> }<br /> }<br />}<br />
C++代碼如下:
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>}
…………無敵分割線……………
…………無敵分割線……………
這個是代碼安全的
C#代碼如下:
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 ptr = 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 /> }<br />}<br />
C++代碼只需替換以上sncaGetAudioInput中的方法如下:
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 />