炫意html5
最早CSS3和HTML5移动技术网站之一

C++ 怎么才能拥有回调函数的对象?像C#一样

C#代码

        private void RealPlayAndPTZDemo_Load(object sender, EventArgs e)
{
m_DisConnectCallBack = new fDisConnectCallBack(DisConnectCallBack);
m_ReConnectCallBack = new fHaveReConnectCallBack(ReConnectCallBack);
m_RealDataCallBackEx2 = new fRealDataCallBackEx2(RealDataCallBackEx);
m_SnapRevCallBack = new fSnapRevCallBack(SnapRevCallBack);
try
{
NETClient.Init(m_DisConnectCallBack, IntPtr.Zero, null);
NETClient.SetAutoReconnect(m_ReConnectCallBack, IntPtr.Zero);
NETClient.SetSnapRevCallBack(m_SnapRevCallBack, IntPtr.Zero);
InitOrLogoutUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Process.GetCurrentProcess().Kill();
}
}
#region CallBack 回调
private void DisConnectCallBack(IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
{
this.BeginInvoke((Action)UpdateDisConnectUI);
}
private void UpdateDisConnectUI()
{
this.Text = "RealPlayAndPTZDemo(实时预览与云台Demo) --- Offline(离线)";
}
private void ReConnectCallBack(IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
{
this.BeginInvoke((Action)UpdateReConnectUI);
}
private void UpdateReConnectUI()
{
this.Text = "RealPlayAndPTZDemo(实时预览与云台Demo) --- Online(在线)";
}
private void RealDataCallBackEx(IntPtr lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr param, IntPtr dwUser)
{
//do something such as save data,send data,change to YUV. 比如保存数据,发送数据,转成YUV等.
}
private void SnapRevCallBack(IntPtr lLoginID, IntPtr pBuf, uint RevLen, uint EncodeType, uint CmdSerial, IntPtr dwUser)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "capture";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (EncodeType == 10) //.jpg
{
DateTime now = DateTime.Now;
string fileName = "async"+ CmdSerial.ToString()+ ".jpg";
string filePath = path + "\\" + fileName;
byte[] data = new byte[RevLen];
Marshal.Copy(pBuf, data, 0, (int)RevLen);
using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate))
{
stream.Write(data, 0, (int)RevLen);
stream.Flush();
stream.Dispose();
}
}
}
#endregion

回答

给你个示例,自己实现。

#include <iostream>

void callback0()
{
	puts("this is a callback!");
}

void callback1()
{
	puts("hello the callback!");
}

void test(intptr_t callback)
{
	_asm {
		call callback;
	}
}

int main(int argc, char* argv[])
{
	test((intptr_t)callback0);

	system("pause");
	return 0;
}

炫意HTML5 » C++ 怎么才能拥有回调函数的对象?像C#一样

Java基础教程Android基础教程