zoukankan      html  css  js  c++  java
  • delphi 调用 C# COM DLL 解析C#数据集

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Runtime.InteropServices;
     5 using System.Net.Sockets;
     6 using System.Net;
     7 using System.Data;
     8 using System.IO;
     9 using System.IO.Compression;
    10 using System.Runtime.Serialization.Formatters.Binary;
    11 
    12 namespace cstcp
    13 {
    14     public interface ICSTCPClass
    15     {
    16         string rtMsg();
    17         bool SetHost(string host, string port);
    18         //
    19         bool SetDict1(string uid,ref string rMsg, IntPtr PData, int PDataLength);
    20     }
    21 
    22     [ClassInterface(ClassInterfaceType.None)]
    23 
    24     public class CSTCPClass : ICSTCPClass
    25     {
    26         public string rHost = "";
    27         public string rPort = "";
    28         public const int HeadBufferSize = 16;
    29         public const int StreamBufferSize = 65536;
    30 
    31         public string rtMsg()
    32         {
    33             return "this is a result string";
    34         }
    35 
    36         public bool SetHost(string host, string port) 
    37         {
    38             rHost = host;
    39             rPort = port;
    40             return true;
    41         }
    42 
    43 
    44 
    45 
    46         //BYTE数组转消息类(反序列)
    47         public static object byteToOb(byte[] t_byte)
    48         {
    49             object dsResult = null;
    50             try
    51             {
    52                 if (t_byte == null) return null;
    53                 MemoryStream mStream = new MemoryStream(t_byte);
    54                 mStream.Seek(0, SeekOrigin.Begin);
    55                 DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);
    56                 BinaryFormatter bFormatter = new BinaryFormatter();
    57                 dsResult = (object)bFormatter.Deserialize(unZipStream);
    58                 //dsResult = (object)bFormatter.Deserialize(mStream);
    59             }
    60             catch
    61             {
    62                 return null;
    63             }
    64             return dsResult;
    65         }
    66         public bool SetDict1(string uid, ref string rMsg, IntPtr PData, int PDataLength)
    67         {
    68             try
    69             {
    70                 byte[] b = new byte[PDataLength];
    71                 Marshal.Copy(PData, b, 0, PDataLength);
    72                 DataSet ds = (DataSet)byteToOb(b);
    73   
    74                 if (ds != null) 
    75                 {   
    76                     DataTable dt = ds.Tables[0];
    77                     dt.WriteXml("C:\\111.xml");
    78                     rMsg = "解析成功";
    79                     return true;
    80                 }
    81                 rMsg = "解析失败";
    82                 return false;
    83             }
    84             catch(Exception ex)
    85             {
    86                 rMsg= ex.Message.ToString();
    87                 return false;
    88             }
    89         }
    90     }
    91 
    92 }

    编译时设置 属性->生成->输出->勾选 为COM Interop注册选项

    注册COM脚本:

    %WINdir%\Microsoft.NET\Framework\v2.0.50727\regasm.exe tcpcom.dll /tlb:tcpcom.tlb /codebase
    pause

    调用方法:Delphi7为例

    Project->Import Type Library->选择刚注册的tcpcom(com模块名)->Create Unit

    1 uses tcpcom_TLB;
    2 procedure TForm1.Button1Click(Sender: TObject);
    3 var
    4   t:TCSTCPClass;
    5 begin
    6   t:= TCSTCPClass.Create(self);
    7   t.SetHost('127.0.0.1','8080') ;
    8   t.Free;
    9 end;
  • 相关阅读:
    马拉车算法
    n皇后问题(回溯算法)
    求解最大升序子序列问题(动态规划)
    利用二进制进行快速乘法:俄罗斯农名乘法
    Redis、MySQL、Hive、Hbase的区别,数据库和数据仓库的区别
    MySQL数据库
    算法工程师的Bug与Debug
    复习KNN并实现
    文本领域数据增强技术
    Fasttext模型总结
  • 原文地址:https://www.cnblogs.com/xspace/p/3115194.html
Copyright © 2011-2022 走看看