使用.NET写Managed用户自定义函数(UDF)C#/VB.NET

这是一个UDF例程。其中只有一个自定义函数MyFunction。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace MyUDF
{
    [Guid("41B55E1A-E91E-4024-BC41-DD5292FB8CC5")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class CSharpModule
    {
        public double MyFunction(int Width,int layes,float UponRubber,
            float ButtomRubber, float Length)
        {
            return Width / 1000 * (layes + (UponRubber + ButtomRubber) / 1.5) 
                * Length;
        }

        [ComRegisterFunctionAttribute]
        public static void RegisterFunction(Type type)
        {
            Registry.ClassesRoot.CreateSubKey(
                GetSubKeyName(type, "Programmable"));
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(
                GetSubKeyName(type, "InprocServer32"), true);
            key.SetValue("", System.Environment.SystemDirectory 
                + @"\mscoree.dll",RegistryValueKind.String);
        }


        [ComUnregisterFunctionAttribute]
        public static void UnregisterFunction(Type type)
        {
            Registry.ClassesRoot.DeleteSubKey(
                GetSubKeyName(type, "Programmable"), false);
        }


        private static string GetSubKeyName(Type type,string subKeyName)
        {
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            s.Append(@"CLSID\{");
            s.Append(type.GUID.ToString().ToUpper());
            s.Append(@"}\");
            s.Append(subKeyName);
            return s.ToString();
        }

    }
}
编译前现在项目的Properties中Builder->Register for COM Interop
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Text

<ClassInterface(ClassInterfaceType.AutoDual)>
<ComVisible(True)>
<Guid("207DF2C7-9773-4FB1-A50C-2A2515C3F8FA")>
Public Class VBModule
    Public Function MyFunction(ByVal Width As Integer, ByVal Layes As Integer, _
                               ByVal UponRubber As Double, _
                               ByVal ButtomRubber As Double, _
                               ByVal Length As Double) As Double
        MyFunction = Width / 1000 * (Layes + (UponRubber + ButtomRubber) _
                                     / 1.5) * Length
    End Function

    <ComRegisterFunctionAttribute()> Public Shared Sub RegisterFunction(ByVal Type As Type)
        Dim rk As RegistryKey

        Registry.ClassesRoot.CreateSubKey(GetSubKeyName(Type, "Programmable"))
        rk = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(Type, "InprocServer32"), True)
        rk.SetValue("", Environment.SystemDirectory + "\mscoree.dll", RegistryValueKind.String)
    End Sub

    <ComUnregisterFunctionAttribute()> Public Shared Sub UnregisterFunction(ByVal Type As Type)
        Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(Type, "Programmable"), False)
    End Sub

    Private Shared Function GetSubKeyName(ByVal Type As Type, ByVal SubKeyName As String) As String
        Dim b As StringBuilder = New StringBuilder

        b.Append("CLSID\{")
        b.Append(Type.GUID.ToString().ToUpper())
        b.Append("}\")
        b.Append(SubKeyName)
        GetSubKeyName = b.ToString()
    End Function
End Class

编译前先选中项目的Properties中Complie->Register for COM Interop


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值