c#.net 通过 c++.net 使用 windows API 及unmanaged code
2008-06-13 10:37
399 查看
为了跟随技术的潮流,我在一个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 的基类
使用方法 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; }
相关文章推荐
- 使用 Visual C# .NET 通过 XPath 表达式查询 XML
- 使用 Visual C# .NET 通过 XPath 表达式查询 XML
- 如何将传递节点设置为通过使用 Visual C#.NET 内联 XSLT 脚本函数
- 如何使用 AllowPartiallyTrustedCallers 属性来调用具有一个强名称从 Web 页通过使用 Visual C#.NET、 Visual C# 2005年或更高版本的程序集
- 通过代码学习C#&.NET——委托使用(通用类设计)
- 使用 Visual C# .NET 通过 XPath 表达式查询 XML
- 使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表
- 如何通过使用 ADO.NET 2005 和 Visual C# 2005 或使用 ADO.NET 和 Visual C# .NET 连接到数据库并运行命令
- C#使用 LumiSoft.Net.dll 通过IMAP 登陆QQ邮箱读取未读邮件
- 如何使用 Visual C# 2005 或 Visual C# .NET 通过 DataSet 对象更新数据库
- C#中通过使用ADO.NET读写BLOB数据
- 如何通过使用 Visual C#.net 将原始数据发送到打印机
- 通过代码学习C#&.NET——委托使用(线程、任务)
- 如何通过使用 DataReader GetSchemaTable 方法和 C#.net 检索列模式
- 通过代码学习C#&.NET——委托使用(通用类设计)
- 通过代码学习C#&.NET——委托使用(正则表达式替换)
- C#托管代码与C++非托管代码互相调使用一(C#调使用C++代码&.net 代码安全)
- c++使用ADO和C#使用ADO.net
- 使用 Visual C# .NET 通过 XPath 表达式查询 XML
- 在C#中使用C++的DLL,并且在DLL中包含有opencv的代码,通过“障眼法”操作