C# 编写COM

 一. 项目属性

     1.程序集信息:使COM可见

     2.为COM互操作注册

 

二. Code

 

using System; using System.Text; using System.Data; using System.IO; using System.Xml; using System.IO.Compression; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Runtime.Serialization.Formatters.Binary; namespace VintechLib { [ComVisible(true),InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IDataFormat { [DispId(1)] byte[] DataSerialization(DataSet ds); [DispId(2)] DataSet ByteToDataSet(byte[] buffer); [DispId(3)] string Test(); } [Guid("DCBE6561-5EC2-3B62-B664-AA1B7C33836C")] [ClassInterface(ClassInterfaceType.None), ComVisible(true)] public class DataFormat : IDataFormat, IObjectSafety { public DataFormat() { } public string Stata { get { return " I'm OK "; } } ///

/// 序列化 /// /// /// public byte[] DataSerialization(DataSet ds) { MemoryStream stream = null; byte[] result = null; try { using (stream = new MemoryStream()) { //DataSet 远程传输类型 ds.RemotingFormat = SerializationFormat.Binary; BinaryFormatter format = new BinaryFormatter(); //序列化数据 format.Serialize(stream, ds); byte[] buffer = stream.ToArray(); stream.Close(); result = Compress(buffer); return result; } } catch (Exception ex) { throw ex; } finally { if (stream != null) stream.Close(); } } /// /// 压缩数据 /// /// /// private byte[] Compress(byte[] data) { MemoryStream stream = null; Stream zipStream = null; byte[] result = null; try { using (stream = new MemoryStream()) { //压缩数据 using ( zipStream = new GZipStream(stream, CompressionMode.Compress, true)) { zipStream.Write(data, 0, data.Length); zipStream.Close(); int count = (int)stream.Length; result = new byte[count]; stream.Position = 0; stream.Read(result, 0, count); stream.Close(); return result; } } } catch (Exception ex) { throw ex; } finally { if (stream != null) stream.Close(); if (zipStream != null) zipStream.Close(); } } /// /// 二进制流转DataSet /// /// /// public DataSet ByteToDataSet(byte[] buffer) { DataSet ds = null; MemoryStream stream = null; byte[] temp = null; try { using (stream = new MemoryStream(buffer)) { using (Stream zipStream = new GZipStream(stream, CompressionMode.Decompress)) { int count = (int)buffer.Length; temp = Decompress(zipStream, count); } } BinaryFormatter ser = new BinaryFormatter(); ds = ser.Deserialize(new MemoryStream(temp)) as DataSet; } catch (Exception ex) { throw ex; } finally { } return ds; } /// /// 解压缩 /// /// /// /// private byte[] Decompress(Stream stream, int dataBlock) { byte[] data = null; int totalBytesRead = 0; try { while (true) { Array.Resize(ref data, totalBytesRead + dataBlock + 1); int bytesRead = stream.Read(data, totalBytesRead, dataBlock); if (bytesRead == 0) break; totalBytesRead += bytesRead; } Array.Resize(ref data, totalBytesRead); return data; } catch (Exception ex) { throw ex; } } public string Test() { return "Hi World! "; } public void GetInterfacceSafyOptions(int riid, out int pdwSupportedOptions, out int pdwEnabledOptions) { pdwSupportedOptions = 1; pdwEnabledOptions = 2; } public void SetInterfaceSafetyOptions(int riid, int dwOptionsSetMask, int dwEnabledOptions) { } } /// /// ActiveX必須提供的接口,GUID不能改變 /// [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IObjectSafety { void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions); void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions); } }

 

 

三.测试

 

Test None

 

四.   总结

   1. Com/ActiveX  原来就是这么简单,C#写的COM 缺点是客户端必须有  .net Framework  若是是VB6.0 或者 VC6写的话 就不存在这个问题了.

   2. 特别注意 调用时是 命名空间.类名

   3. 特定的接口,自定义接口不能少 

你可能感兴趣的:(DoNet,c#,dataset,stream,byte,exception,null)