c#.net 通过 c++.net 使用 windows API 及unmanaged code

为了跟随技术的潮流,我在一个windows application项目中放弃了 mfc,转入 c++.net winform. 虽然winform 运行速度无法与 mfc 相提并论,但设计效率大大提高,特别是有了自定义 control, 代码量强度大大降低, 而且很多底层代码 (unmanaged) 可以直接移植使用。当我进一步跳到 c# 是, 底层代码就有了问题,c# 没有 unmanaged code 一说。解决方案有两个: 1)在 c# 中声明 api , 这是流行的方法, 2)生成 .net dll 供c# 直接使用, 当dll 被加入c# reference 后,能立即看到各接口及属性。

使用方法 1 有时很烦琐,例如通过  TAPI 使用modem, 或使用 TCPIP.  第二种方法对不懂 c++ 的人来说是一种考验。 在  vs 2005 中产生一个c++.net 的dll project, 然后用 __gc class 重新封装。

_CConnection 与 CConnection 分别是unmanged 与 managed code 的基类

         #define EXT(func) /

	if (! m_bEnableException) /

		return func; /

	else if ((func)) /

			return true; /

		else /

			throw new Exception(); 

	
 
          public __gc class CConnection

	{

	protected:

		_CConnection*		m_pConnection;   //unmanaged class

		bool			m_bEnableException;



	public:

		__property bool get_EnableException() { return m_bEnableException; }

		__property void set_EnableException(bool bEnableException) { m_bEnableException = bEnableException; }



		virtual bool IsConnected() { return m_pConnection->IsConnected(); }

		virtual bool Read(unsigned char buffer __gc[], int length) 

		{	

			unsigned char __pin *pBuffer = &buffer[0];

			EXT(m_pConnection->Read((char*) pBuffer, length)); 

		}



		virtual bool Read(unsigned char buffer __gc[], int offset, int length) 

		{	

			unsigned char __pin *pBuffer = &buffer[offset];

			EXT(m_pConnection->Read((char*) pBuffer, length)); 

		}



		virtual bool Write(unsigned char buffer  __gc[], int length) 

		{ 

			unsigned char __pin *pBuffer = &buffer[0];

			EXT(m_pConnection->Write((char*) pBuffer, length)); 

		}

                  ...

  

 

	public __gc class CProtocol

	{

	protected:

		_CProtocol*			m_pProtocol;

		CConnection*		m_pConnection;

		bool				m_bEnableException;

		bool				m_bLastCommResult;

		bool				m_bChecErrorEnabled;



	public:

		__event EventHandler* OnCommunicationTimeoutStatusChanged;



	protected:

		bool CheckTimeout(bool bCommResult)

		{

			if (! m_bChecErrorEnabled) return bCommResult;

			bool b = !(!bCommResult && IsTimeout());

			if (m_bLastCommResult != b) {

				m_bLastCommResult = b;

				if (OnCommunicationTimeoutStatusChanged != NULL) {

					OnCommunicationTimeoutStatusChanged->Invoke(__box(b), NULL);

				}

			}

			return bCommResult;

		}



	public:

		__property bool get_EnableException() { return m_bEnableException; }

		__property void set_EnableException(bool bEnableException) { m_bEnableException = bEnableException; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值