C#的COM编程问题,急~
C#如何开发COM程序,从网上找了很多资料,很少。仿照一片文章写了一个测试程序,结果调用不了。对COM了解甚少,请大侠指点。下面是我写的COM代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace SynchronousSocketClient
{
// 类 接 口
[Guid( "44475602-F2E1-49d3-8CA6-22BF6DF89835 ")]
public interface Client_Interface
{
[DispId(1)]
Boolean StartClient(string strIP, string strPort);
[DispId(2)]
Boolean SendByteData(byte[] sendData);
[DispId(3)]
Boolean ReceiveByteData(out byte[] sendData);
}
// 事件接口
[Guid( "47C976E0-C208-4740-AC42-41212D3C34F0 "),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface Client_Events
{
}
// 接口实现
[Guid( "D0C200A0-5B4D-46b6-B2C0-748BE01F4691 "),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(Client_Events))]
public class SynchronousSocketClient : Client_Interface
{
private Socket remoteClient;
private string strServerIP;
private string strServerPort;
private const int recBufferCount = 2048;
public Boolean StartClient(string strIP, string strPort)
{
try
{
strServerIP = strIP;
strServerPort = strPort;
IPHostEntry ipHostInfo = Dns.Resolve(strServerIP);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, int.Parse(strServerPort));
remoteClient = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
remoteClient.Connect(remoteEP);
return true;
}
catch (ArgumentNullException ane) { return false; }
catch (SocketException se) { return false; }
catch (Exception e) { return false; }
}
catch (Exception ec) { return false; }
}
public Boolean SendByteData(byte[] sendData)
{
try
{
remoteClient.Send(sendData);
return true;
}
catch (ArgumentNullException ane)
{
return false;
}
catch (SocketException se)
{
return false;
}
}
public Boolean ReceiveByteData(out byte[] sendData)
{
try
{
byte[] tempData = new byte[recBufferCount];
int bytesRec = remoteClient.Receive(tempData);
sendData = tempData;
return true;
}
catch (ArgumentNullException ane)
{
sendData = null;
return false;
}
catch (SocketException se)
{
sendData = null;
return false;
}
}
}
}
从别的工程中调用,却不出现COM中的方法,
SynchronousSocketClient a = new SynchronousSocketClient();
a. 后面的方法出不来。
COM在什么情况下使用,如何使用?
[解决办法]
com对.net来说是一个太老的技术术语。
非托管程序调用托管程序难道相当大!组件化开发方向控件\Webservice\remoting。。。。。。
[解决办法]
C#直接写COM程序好像是不太容易,我都是直接用C++写COM,然后在C#里调,你可以试试
[解决办法]
regasm 了没