delphi 调用 C# COM DLL 解析C#数据集
2013-06-03 13:07
489 查看
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Net.Sockets; using System.Net; using System.Data; using System.IO; using System.IO.Compression; using System.Runtime.Serialization.Formatters.Binary; namespace cstcp { public interface ICSTCPClass { string rtMsg(); bool SetHost(string host, string port); // bool SetDict1(string uid,ref string rMsg, IntPtr PData, int PDataLength); } [ClassInterface(ClassInterfaceType.None)] public class CSTCPClass : ICSTCPClass { public string rHost = ""; public string rPort = ""; public const int HeadBufferSize = 16; public const int StreamBufferSize = 65536; public string rtMsg() { return "this is a result string"; } public bool SetHost(string host, string port) { rHost = host; rPort = port; return true; } //BYTE数组转消息类(反序列) public static object byteToOb(byte[] t_byte) { object dsResult = null; try { if (t_byte == null) return null; MemoryStream mStream = new MemoryStream(t_byte); mStream.Seek(0, SeekOrigin.Begin); DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true); BinaryFormatter bFormatter = new BinaryFormatter(); dsResult = (object)bFormatter.Deserialize(unZipStream); //dsResult = (object)bFormatter.Deserialize(mStream); } catch { return null; } return dsResult; } public bool SetDict1(string uid, ref string rMsg, IntPtr PData, int PDataLength) { try { byte[] b = new byte[PDataLength]; Marshal.Copy(PData, b, 0, PDataLength); DataSet ds = (DataSet)byteToOb(b); if (ds != null) { DataTable dt = ds.Tables[0]; dt.WriteXml("C:\\111.xml"); rMsg = "解析成功"; return true; } rMsg = "解析失败"; return false; } catch(Exception ex) { rMsg= ex.Message.ToString(); return false; } } } }
编译时设置 属性->生成->输出->勾选 为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
uses tcpcom_TLB; procedure TForm1.Button1Click(Sender: TObject); var t:TCSTCPClass; begin t:= TCSTCPClass.Create(self); t.SetHost('127.0.0.1','8080') ; t.Free; end;
相关文章推荐
- C#的DLL注册为COM,Delphi来调用
- C#的DLL注册为COM,Delphi来调用
- C#的DLL注册为COM,Delphi来调用
- C# 调用C++的dll,通过DllImport方式。 from http://www.cnblogs.com/xiaokang088/archive/2011/04/08/2009673.html
- Delphi 调用 C# Dll 时一个编译问题 mscorlib_TLB.h Ambiguity between 'String' and 'System::String'
- C#调用Delphi的dll的两种方法
- C#的DLL注册为COM,VB来调用
- Delphi调用C#dll的问题
- Delphi调用C#动态链接库DLL(摘自网络)
- 关于delphi Com+调用C# DLL的一点说明
- c#调用Delphi 的DLL控件,传递字符串的解决
- C#调用C++编写的COM DLL
- C#调用Delphi的dll的两种方法
- C#调用Delphi编写的Dll,实现绘图的功能
- [轉]c#简单调用DELPHI DLL封装窗体
- C# 调用delphi生成Dll的相关问题
- C#调用Delphi DLL时,要注意的几个问题。
- C#调用Delphi Dll返回字符串的示例
- 黄聪:关于delphi Com+调用C# DLL的一点说明[转]
- C#调用C++编写的COM DLL