• 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;
  • 相关阅读:
    Iphone 启动图的尺寸
    Xcode 7真机测试详解
    android 设置textview中划线效果
    IOS应用在iPhone5和iPhone5s上不能全屏显示,应用画面上下各有1条黑色的解决方案
    配置ant编译时的jdk版本
    mac系统下配置aapt环境变量
    iOS中的2x,3x问题
    Android 字体设置-Typeface讲解
    android:json解析的两个工具:Gson和Jackson的使用小例子
    Android App监听软键盘按键的三种方式
  • 原文地址:https://www.cnblogs.com/xspace/p/3115194.html
Copyright © 2020-2023  润新知