C# Com 和Com+
我想问的问题
1、C# 怎么写Com组件(不是Com+),然后怎么注册呢,怎么在C#中再去调用这个Com组件
2、这是我写的代码
类库
[ComVisible(true),
Guid("50C780E0-74FC-41d3-9BDB-7674DFDCC5A3"))
]
public interface IBanlance
{
string Name
{
get;
}
Stream Read();
DateTime GetTime();
}
[ComVisible(true),
Guid("93BA6E50-599E-4f58-BBF7-D6BED7935C43")
]
public class Banlance : ServicedComponent, IBanlance {
public Banlance() {
}
public string Name {
get {
return "布拉本天平";
}
}
public Stream Read() {
Stream s = null;
try
{
s = File.OpenRead(@"D:\111.txt");
}
catch (Exception e) {
Console.WriteLine(e.Message+"=="+e.StackTrace);
}
return s;
}
public DateTime GetTime() {
return DateTime.Now;
}
然后产生了个密匙
在Com+组件里有这个组件,注册表也有这两个Guid
客户端
static void Main(string[] args)
{
try
{
new A();
//在这个地方报错
在 ComAddTest.Program.Main(String[] args) 位置 D:\study\ComAddTest\ComAddTest
\Program.cs:行号 27===无法将类型为“ComAddComponent.Banlance”的对象强制转换为类
型“A”。
}
catch (Exception e) {
Console.WriteLine(e.StackTrace+"==="+e.Message);
}
Console.ReadLine();
}
[Guid("50C780E0-74FC-41d3-9BDB-7674DFDCC5A3"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IBanlance{
string Name{
get;
}
Stream Read();
DateTime GetTime();
}
[ComImport,Guid("93BA6E50-599E-4f58-BBF7-D6BED7935C43")]
public class A{
}
请各位大侠看看,谢谢
[解决办法]
利用 COM Admin 1.0 来对我的dll进行注册到COM+ table.
- C# code
//========================================================================================================== // // //This method is to create a COM+ application through C# coding. //including Security settings. //you also can do it by go through the Component Services program under Administration Tools. private void load() { try { //The ICOMAdminCatalog2 is the COM Admin Library 1.5 which has more functions than the older version. //The ICOMAdminCatalog is the COM Admin Library 1.0 COMAdmin.ICOMAdminCatalog2 cac = new COMAdmin.COMAdminCatalogClass(); //Connect to the Server COM+ Services. object root = cac.Connect("127.0.0.1"); // //Get the Applications List in the Component Services. COMAdmin.ICatalogCollection appList = (COMAdmin.COMAdminCatalogCollection)cac.GetCollection("Applications"); //Get a new Application from the Add() Method. COMAdmin.ICatalogObject caco = (COMAdmin.ICatalogObject)appList.Add(); //Set the properties of the Application. caco.set_Value("Name", "My_Functions"); caco.set_Value("Description", "The Function DLL contrains two Components, one is for user level and the other one is for Administrator level"); //save changes to the applications list. appList.SaveChanges(); // //Install the Component. //I don't know why, Microsoft says that MyFunctions.tlb should be the MyFunctions.dll file. //but it doesn't work for me, I have to put MyFunctions.tlb and MyFunctions.dll in the same folder, then //install the MyFunctions.tlb file. cac.InstallComponent("My_Functions", "MyFunctions.tlb", "", ""); // // // //get the Roles collection from the new COM+ Application. COMAdmin.ICatalogCollection roleList = (COMAdmin.COMAdminCatalogCollection)appList.GetCollection("Roles", caco.Key); // //set up two new roles. COMAdmin.ICatalogObject user = (COMAdmin.ICatalogObject)roleList.Add(); COMAdmin.ICatalogObject admin = (COMAdmin.ICatalogObject)roleList.Add(); //set properties of these two roles. admin.set_Value("Name", "Admin"); user.set_Value("Name", "User"); // //save changes to the roles collection. roleList.SaveChanges(); //Get the Component Collection of the new COM+ application object. COMAdmin.ICatalogCollection comList = (COMAdmin.COMAdminCatalogCollection)appList.GetCollection("Components", caco.Key); //populate the collection. //this is a must action. //to get all the objects inside the Component Collection. comList.Populate(); // //loop through the collection to set some properties. foreach (COMAdmin.ICatalogObject tmpAdmin in comList) { if (tmpAdmin.Name.ToString().ToLower() == "my_functions") { tmpAdmin.set_Value("ComponentAccessChecksEnabled", true); comList.SaveChanges(); // COMAdmin.ICatalogCollection adminList = (COMAdmin.COMAdminCatalogCollection)comList.GetCollection("RolesForComponent", tmpAdmin.Key); COMAdmin.ICatalogObject userObject = (COMAdmin.ICatalogObject)adminList.Add(); userObject.set_Value("Name", "User"); adminList.SaveChanges(); // break; } } // // //set up some properties. caco.set_Value("ApplicationAccessChecksEnabled", true); caco.set_Value("AccessChecksLevel", COMAdmin.COMAdminAccessChecksLevelOptions.COMAdminAccessChecksApplicationComponentLevel); appList.SaveChanges(); //Export this COM+ application to the MSI excutable file. //optional. cac.ExportApplication("My_Functions", @"D:\temp\MyFunctions.msi", 4); //cac.InstallComponent("My_Functions", "MyFunctions.tlb", "", ""); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
[解决办法]
[解决办法]
[解决办法]