创建COM组件 [英] Create com component

查看:77
本文介绍了创建COM组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中为给定代码创建COM组件.请您通过一个简单的步骤来帮助我.在这个com组件之后,我需要从powerbuilder调用

How to create the COM component in c# for the given code . Please can you help me with a simple step. After this this com component , I need to call from powerbuilder

私有无效button2_Click(对象发送者,EventArgs e)
{
试试
{

private void button2_Click(object sender, EventArgs e)
{
try
{

ExchangeService服务=新的ExchangeService();
字符串用户名= mycompany +"\\" +"usersid";
service.Credentials =新的WebCredentials(用户名,"myusername");  
service.AutodiscoverUrl("<​​a style="color:#008fd3; text-decoration:none; outline:none!important; margin:0px; padding:0px; border:0px; font-size:15px"> myname @ mycompany .com &;);
预约=新预约(服务);
约会.Subject =测试假期";
约会.开始=日期时间.现在;
约会.结束=约会.开始.添加小时(1);
约会.Save();
}

ExchangeService service = new ExchangeService();
string username = mycompany+ "\\" + "usersid";
service.Credentials = new WebCredentials(username, "myusername"); 
service.AutodiscoverUrl("myname@mycompany.com");
Appointment appointment = new Appointment(service);
appointment.Subject = "Testing Holiday";
appointment.Start = DateTime.Now;
appointment.End = appointment.Start.AddHours(1);
appointment.Save();
}

捕获(异常例外)
{

catch (Exception ex)
{

console.writeline("发生以下错误:" + ex.Message);
}
}

console.writeline("following error occurred: " + ex.Message);
}
}

polachan

推荐答案

嗨polachan,

Hi polachan,

这是详细步骤.

1.启动Microsoft Visual Studio .NET,并使用类库"模板创建一个新的Visual C#项目.在我们的示例中,我们将创建项目并将其命名为"COMTest".保存项目.

1.Launch Microsoft Visual Studio .NET and create a new Visual C# Project with the Class Library template. In our example we will create the project and name it "COMTest." Save the project.

2.Open Visual Studio开发人员命令提示符Studio ,然后导航到项目的文件夹.

2.Open Developer Command Prompt for Visual Studio and navigate to the project’s folder.

3.创建一个密钥对,该密钥对将用于使用强名称对.NET程序集进行签名.

3.Create a key pair that will be used to sign the .NET assembly with a strong name.

sn -k key.snk

4.向COMTest项目添加一个名为"COMObject"的类,该类将创建COMObject.cs.

4.Add a class called "COMObject" to the COMTest project, which will create COMObject.cs.

5在"Property"文件夹下,有"AssemblyInfo.cs".请在此文件末尾添加以下代码.

5 Under Property folder, there is AssemblyInfo.cs. Please add following code at the end of this file.

[assembly: System.Reflection.AssemblyKeyFileAttribute(@"..\..\key.snk")]

7.生成与COMObject一起使用的全局唯一标识符.
1).启动guidgen.exe(打开控制台窗口并在命令提示符下输入"guidgen"时,应该很容易做到).
2).在guidgen实用程序中选择注册表格式"选项,生成一个新的guid,然后复制该GUID.

7.Generate a globally unique identifier for use with COMObject.
1).Start guidgen.exe (this should be easily done when opening a console window and entering "guidgen" at the command prompt).
2).Select the Registry Format option in the guidgen utility, generate a new guid, and copy the GUID.

8.使用System.Runtime.InteropServices.GuidAttribute生成类COMObject的属性.将GUID作为字符串传递给GUIDAttribute,但是删除包围复制到剪贴板的GUID的花括号.

8.Use System.Runtime.InteropServices.GuidAttribute to generate an attribute for class COMObject. Pass the GUID as a string to GUIDAttribute, but remove the curly braces that surround the GUID that was copied onto the clipboard.

[System.Runtime.InteropServices.Guid("42DEB925-EF99-4C0D-BFD0-44313E649A32")]
public class COMObject
{
}

9.使用以下代码为COMObject创建一个名为"COMObjectFunction"的公共成员函数:

9.Create a public member function for COMObject that is named "COMObjectFunction" with the following code:

public string COMObjectFunction()
{
    //put your code here
}

10.构建解决方案.

10.Build the solution.

11.注册程序集. ["重载COMTest.dll/tlb:COMTest.tlb/代码库COMTest在命令提示符下,而在包含COMTest.dll的目录中] 您需要运行 以管理员身份运行Visual Studio的开发人员命令提示符 .

11.Register the Assembly. ["regasm COMTest.dll /tlb:COMTest.tlb /codebase COMTest" at the command prompt, while in the directory that contains COMTest.dll] You need to run Developer Command Prompt for Visual Studio as Administrator.

12.现在可以使用COM访问该程序集.

12.The assembly is now accessible using COM.

最好的问候,
王丽

Best Regards,
Li Wang


这篇关于创建COM组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆