C#調用C++中帶結構體指標的方法,憋了3個小時!

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.