用C#创建COM对象
2024-07-21 02:20:07
供稿:网友
 
在本篇文章中,我们将讨论下面的问题: 
  ·使用c#创建一个简单的com对象(使用com的interop特性)。 
  ·从vc++客户端软件中访问com。客户端软件使用了typelibrary(.tlb文件)。 
  为了简单和方便开发人员使用、测试起见,我们使用了sqlserver数据库软件的缺省安装中的northwind数据库。 
  ·修改com对象中sqlserver的名字,与sqlserver连接。 
  ·我们已经创建了连接数据库用的分别为scott、tiger的用户名和口令,我们可以使用它或者其他现有的用户名和口令。 
  第一部分:用c#创建简单的com对象 
  com对象是classlibrary类,它生成dll文件。要在vs开发环境中创建一个简单的com对象,我们可以依次选择“文件”->;“新创建”->;“工程”->;“visualc#工程”->;“类库”,然后创建一个名字为database_comobject的工程。 
  需要注意的是:在com中调用vc#对象需要下面的条件: 
  ·类必须是public性质。 
  ·特性、方法和事件必须是public性质的。 
  ·特性和方法必须在类接口中定义。 
  ·事件必须在事件接口中定义。 
  不是在这些接口中定义的public性质的类成员不能被com访问,但它们可以被其他的.net framework对象访问。要让com能够访问特性和方法,我们必须在类接口中定义它们,使它们具有dispid属性,并在类中实现这些特性和方法。这些成员定义时的顺序也就是它们在com中顺序。要让com访问类中的事件,必须在事件接口中定义这些事件,并赋予它们dispid属性。事件接口不应当由类完成,类只实现类接口(它可以实现不止一个接口,但第一个接口是缺省接口),应当在缺省接口中实现需要让com访问的方法和特性,方法和特性必须被标识为public性质,并符合在类接口中的定义。需要让com访问的事件也在缺省的类接口中完成,它们也必须被标识为public性质,并符合事件接口中的定义。 
  在接口名字之前,每个接口需要一个guid特性。要生成变个唯一的guid,需要运行guidgen.exe工具软件,并选择“注册表格式” 下面是一个类界面: 
[guid(";694c1820-04b6-4988-928f-fd858b95c880";)] 
public interface dbcom_interface 
{ 
[dispid(1)] 
void init(string userid , string password); 
[dispid(2)] 
bool executeselectcommand(string selcommand); 
[dispid(3)] 
bool nextrow(); 
[dispid(4)] 
void executenonselectcommand(string inscommand); 
[dispid(5)] 
string getcolumndata(int pos); 
} 
  com事件接口: // 事件接口database_comobjectevents 
[guid(";47c976e0-c208-4740-ac42-41212d3c34f0";), 
interfacetype(cominterfacetype.interfaceisidispatch)] 
public interface dbcom_events 
{ 
} 
  下面是实际的类定义: 
[guid(";9e5e5fb2-219d-4ee7-ab27-e4dbed8e123e";), 
classinterface(classinterfacetype.none), 
comsourceinterfaces(typeof(dbcom_events))] 
public class dbcom_class : dbcom_interface 
{ 
  需要注意的是,在类的前面,需要设置下面的特性: 
classinterface(classinterfacetype.none), 
comsourceinterfaces(typeof(dbcom_events))] 
  classinterfacetype.none表示没有为该类生成类接口,如果没有明确地实现接口,类只能通过idispatch提供后期绑定访问。用户希望通过明确地由类实现的接口使外部对象能够访问类的功能,这也是推荐的classinterfaceattribute的设置。 
  comsourceinterfaces(typeof(dbcom_events))]确定许多作为com事件向外部对象提供的接口。在本文的例子中,我们不对外部对象开放任何事件。 
  下面是com对象完整的源代码: 
using system; 
using system.runtime.interopservices; 
using system.io; 
using system.text; 
using system.data.sqlclient; 
using system.windows.forms ; 
namespace database_comobject 
{ 
[guid(";694c1820-04b6-4988-928f-fd858b95c880";)] 
public interface dbcom_interface 
{ 
[dispid(1)] 
void init(string userid , string password); 
[dispid(2)] 
bool executeselectcommand(string selcommand); 
[dispid(3)] 
bool nextrow(); 
[dispid(4)] 
void executenonselectcommand(string inscommand); 
[dispid(5)] 
string getcolumndata(int pos); 
} 
// 事件接口database_comobjectevents 
[guid(";47c976e0-c208-4740-ac42-41212d3c34f0";), 
interfacetype(cominterfacetype.interfaceisidispatch)] 
public interface dbcom_events 
{ 
} 
[guid(";9e5e5fb2-219d-4ee7-ab27-e4dbed8e123e";), 
classinterface(classinterfacetype.none), 
comsourceinterfaces(typeof(dbcom_events))] 
public class dbcom_class : dbcom_interface 
{ 
private sqlconnection myconnection = null ; 
sqldatareader myreader = null ; 
public dbcom_class() 
{ 
} 
public void init(string userid , string password) 
{ 
try 
{ 
string myconnectstring = ";user id=";+userid+";;password=";+password+ 
";;database=northwind;server=skywalker;connect timeout=30";; 
myconnection = new sqlconnection(myconnectstring); 
myconnection.open(); 
messagebox.show(";connected";); 
} 
catch(exception e) 
{ 
messagebox.show(e.message); 
} 
} 
public bool executeselectcommand(string selcommand) 
{ 
if ( myreader != null ) 
myreader.close() ; 
sqlcommand mycommand = new sqlcommand(selcommand); 
mycommand.connection = myconnection; 
mycommand.executenonquery(); 
myreader = mycommand.executereader(); 
return true ; 
} 
public bool nextrow() 
{ 
if ( ! myreader.read() ) 
{ 
myreader.close(); 
return false ; 
} 
return true ; 
} 
public string getcolumndata(int pos) 
{ 
object obj = myreader.getvalue(pos); 
if ( obj == null ) return ";"; ; 
return obj.tostring() ; 
} 
public void executenonselectcommand(string inscommand) 
{ 
sqlcommand mycommand = new sqlcommand(inscommand , myconnection); 
int retrows = mycommand.executenonquery(); 
} 
} 
} 
  在创建com对象前,我们必须向com interop注册该对象。右击方案管理器中的工程名字,点击快捷菜单上的“属性”选项,然后再点击“配置”->;“创建”,扩展output小节,将register for com interop选项的值设置为true。这样,一个com对象就能够与可管理性应用程序进行交互。 
  为了使com对象能够被外部对象调用,类库组合必须有一个强名字。创建强名字需要用到sn.exe名字: sn -k database_com_key.snk 
打开assemblyinfo.cs,并修改下面一行的内容: 
[assembly: assemblykeyfile(";database_com_key.snk";)] 
  创建对象。创建对象会生成一个可以被导入到可管理性或非可管理性代码中的类库。 
  第二部分:使用visual c++创建访问com对象的客户端软件 
  ·使用vc++开发环境创建一个简单的工程。 
  ·使用#import directive导入类型库。 
  ·在界面中创建一个smart pointer,从接口中执行com类提供的功能。确保在应用程序加载时添加coinitialize()调用: coinitialize(null); 
database_comobject::dbcom_interfaceptr p(__uuidof(database_comobject::dbcom_class)); 
db_com_ptr = p ; 
db_com_ptr->;init(";scott"; , ";tiger";); 
  下面的代码对customers数据库表执行一个sql命令,返回给定id的客户的信息: 
char cmd[1024]; 
sprintf(cmd , ";select companyname , contactname , 
contacttitle , address from customers where customerid = '%s'"; , m_id ); 
const char *p ; 
bool ret = db_com_ptr->;executeselectcommand(cmd); 
if ( ! db_com_ptr->;nextrow() ) return ; 
_bstr_t mdata = db_com_ptr->;getcolumndata(3); 
p = mdata ; 
m_address = (cstring)p ;