c#制作com组件供wincc调用
1,建立com组件
[ComVisible(true)]
[Guid("751452F7-4541-4483-83E0-22EDC1278F58")]
public interface IMxbLiabray
{
void Initialize();
void Dispose();
int Add(int x, int y);
}
[ComVisible(true)]
[Guid("258CBDAF-D4CD-41D4-9480-A81793846816")]
public class MxbLiabray : Form,IMxbLiabray
{
private static MxbLiabray Form1;
private int m_value;
private Button button1;
private ListBox listBox1;
public int Add(int x, int y)
{
MessageBox.Show($"x + y");
m_value = x + y;
return x + y;
}
public void Dispose()
{
}
public void Initialize()
{
button1 = new Button();
button1.Left = 200;
button1.Text = "Exit";
button1.Click += new EventHandler(button1_Click);
listBox1 = new ListBox();
this.Controls.Add(button1);
this.Controls.Add(listBox1);
Form1 = new MxbLiabray();
Application.Run(Form1);
}
private void button1_Click(object sender, System.EventArgs e)
{
int count = 1;
// Check to see whether the user wants to exit the application.
// If not, add a number to the list box.
while (MessageBox.Show("Exit application?", "",
MessageBoxButtons.YesNo) == DialogResult.No)
{
listBox1.Items.Add(count);
count += 1;
}
// The user wants to exit the application.
// Close everything down.
Application.Exit();
}1,代码中,使用接口,以及guid和comvisible=true;
2,在生成之中进行打勾
签名那里打勾---生产强程序集
3,生成com组件和注册dll到全局缓存
regasm .\MxbLiabray.dll
gacutil.exe /i .\MxbLiabray.dll
4,在wincc中调用
Sub procedure1
Dim obj
Dim k
Set obj = CreateObject("MxbLiabray.MxbLiabray")
k=obj.Initialize
End Sub注意名称:
在注册表中查看注册的com组件名称:
5,使用OLEVIER查看计算机内的组件,但是查不到我注册的类..




浙公网安备 33010602011771号