C/C++ Program Technique Note

1.Explicit Key Word

2.Volatile Key Word

3.How to Convert C Pointer to Ref Implement Auto Release

4.C++ Exception

5.How to Implements delegate in C++

6.How to Deal with C++ CallBack in C#

6.1. C# Delegate Set to Native C++ Code for None UWP

  1. C# warp for C++ DLL
internal class VoiceCoreWrapper
{
	[DllImport("VoiceCore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
	public static extern bool SetSpeechCallback(TTSCallback callback);
}
  1. C# Delegate Define
public delegate void TTSCallback(RecognizeEvent evt, string text);
  1. Set C# CallBack to Native Dll
public bool SetCallback(TTSCallback callback)
{
     if (_selectEngine == SpeechEngine.VSI)
     {
         return VoiceCoreWrapper.SetSpeechCallback(callback);
     }
     return true;
}
  1. Native Code CallBack Define
typedef void(CALLBACK *TTSCallback)(int, char*);
  1. Delegate implement in C#
VoiceRecognizeController.Instance.SetSpeechCallback(delegate (RecognizeEvent a, string t)
{
	if (a == RecognizeEvent.speech_final)
	{
		if (isWakeupWordRepeat)
		{
			VoiceRecognizeController.Instance.RecognizeStart("click", 15000);
		}
		else
		{
			VoiceServiceController.Instance.OnVoiceTaskEndEvent(TaskEndEventType.WakeupAgain);
		}
	}
	else if (a == RecognizeEvent.speech_cancel)
	{
		//
	}
});

6.2. C# Delegate Set to C++ (C++/CX or C++/WINRT)

  1. C++/CX Declaration and Define
namespace VSISDK_RT
{
	public delegate void VSICALLBACK(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam);
    public ref class VSISDK sealed
    {
    public:
		VSISDK();
		event VSICALLBACK^ delegateCallBack;
		......
		void CXCallBack(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam);

	}
}
  1. The Implement in Native C++ Code
VSISDK^ pThis;
VSISDK::VSISDK()
{
	pThis = this;
}
void VSICB(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam)
{
	pThis->CXCallBack(message, hIns, hUserData, fstParam, sndParam, thdParam);
}

void VSISDK_RT::VSISDK::CXCallBack(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam)
{
	pThis->delegateCallBack(message, hIns, hUserData, fstParam, sndParam, thdParam);
}

The function VSICB is the C style function,In the Native Code it used as a CallBack function,it May Called by another function or another thread from Native Code.
4. The Implement in C#

namespace CXTestApp
{
    public delegate void VSICALLBACK(int message, UInt64 hIns, UInt64 hUserData, UInt64 fstParam, UInt64 sndParam, UInt64 thdParam);

    [TestClass]
    public class UnitTest1
    {

        [TestMethod]
        public void TestMethod1()
        {
            VSISDK rt = new VSISDK();
            rt.delegateCallBack += new VSISDK_RT.VSICALLBACK(VSICallBack);
            rt.VSISetOpt(0, "sss", "ssss");
        }
        void VSICallBack(int message, UInt64 hIns, UInt64 hUserData, UInt64 fstParam, UInt64 sndParam, UInt64 thdParam)
        {
            int i = 0;
            i = i + 10;
        }
    }
}

C# Muti-Thread In Universal Windows Platform

Task-based asynchronous programming detail reference:

Task task = new Task(() =>
{
      string path = @"E:\git-code\CUISDKrt\bin\x64\Debug\AppX\music5.wav";
      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      BinaryReader br = new BinaryReader(fs);
      VSISDKrt.VSISetOpt(handle, CONS_DEFINE.VSI_OPTION_POSTVOICE, "1");
      VSISDKrt.VSIPost(handle, CONS_DEFINE.L_MSG_REQUEST_STARTREC, 0, 0);
      while (true)
      {
            byte[] byteArray = br.ReadBytes(3200);
            IntPtr pBuffer = Marshal.AllocHGlobal(3200);
            Marshal.Copy(byteArray, 0, pBuffer, byteArray.Length);
            VSISDKrt.VSIPost(handle, 
            				 CONS_DEFINE.L_MSG_ACTION_POSTDATA, 
            				(UInt64)byteArray.Length, 
            				(UInt64)pBuffer.ToInt64());
            Marshal.FreeHGlobal(pBuffer);
            if (byteArray.Length < 3200)
            {
                VSISDKrt.VSIPost(handle, CONS_DEFINE.L_MSG_REQUEST_STOPREC, 0, 0);
                System.Diagnostics.Debug.WriteLine("The Post Voice Task Have Done.");
                break;
            }
            Task.Delay(100).Wait();
      }
});
task.Start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值