如何从vbscript调用c#dll

时间:2013-07-01 10:26:39

标签: c# visual-studio-2010 com vbscript visual-studio-2005

您好我正在使用以下代码创建一个c#dll

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace imageexport
{
    [ComVisible(true)]
    public class ExportImage
    {
        [ComVisible(true)]
        public void exportPNG(String pDirectory,String svgFileName,String outputFileName) {
            String arguments= pDirectory+"res\\include\\highcharts-convert.js -infile "+pDirectory+"res\\graphs\\"+svgFileName+" -outfile "+pDirectory+"res\\graphs\\"+outputFileName +" -scale 2.5 -width 1088";
            /*using (StreamWriter writer = new StreamWriter("c:\\debug.txt", true))
            {
                writer.WriteLine("pDirectory=" +pDirectory);
                writer.WriteLine("arguments="+arguments);
            }*/
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = false;
            p.StartInfo.FileName = pDirectory+"res\\bin\\phantomjs.exe";
            p.StartInfo.Arguments = arguments;
            p.Start();
            p.WaitForExit();
        }
    }
}

为了创建DLL我在VS2005中进行如下配置

Application
     Assembly Name -> imageexport
      Default Namespace - > imageexport
     outputtype => Classlibrary
     startup object ->(Not Set)
   Assembly Information :
         a) Assembly Version ->1 0 0 0
         b)File Version ->1 0 0 0
         c) Make Assembly Com visible ->checked
 Build 
  Configuration -> Active (Debug)
  Platform ->Active (Any CPU)
  General :
    a)conditional Compilation Symbols -> blank
     b) Define Debug Constant ->checked
    c) Define Trace Constant -> checked
    d) platform Target -> Any CPU
    e) allow unsafe code -> not checked
    f) optimize code -> not checked
    g) warning lavel -> 4
    h) Supperess warning -> blank 
    i) Treat Warnign errors -> None

此后,我注册了imageexport.dll,其中包含.net 2.0的

现在当我查看注册表然后在HKEY_CLASSES_ROOT下我找到了imageexport.ExportImage。

现在在vbscript下面的代码用于创建对象并调用函数

Dim obj                        
Set obj = CreateObject("imageexport.ExportImage")
obj.exportPNG rvPAWZDirectoryPath&"\","SVGData_"&Session("export_time")&".svg","Export_" & export_time & ".png"

但是这会在VBSCript的createObject行中给出名为'UnknownException'的异常。请告诉我问题在哪里

1 个答案:

答案 0 :(得分:8)

我认为您应该将您的Dll注册为:

 C:\Windows\Microsoft.NET\Framework64\v4.0.30319>regasm yourDllPath /codebase

现在您可以创建Dll的对象。