【问题标题】:Variant param values from classic asp calling .NET component method来自经典 asp 调用 .NET 组件方法的变体参数值
【发布时间】:2013-02-21 20:45:33
【问题描述】:

我使用 PDFsharp 项目将许多 pdf 文档合并为一个文件,该文件完美流畅。 但我还需要从经典的 ASP 服务器页面调用此方法。

也可以,但奇怪的是通过调用方法来处理参数值。

C#定义:

public void MergeMultiplePDF(object[] files, string outFile)
{
  // note: get an array from vbscript, so files need to be a object array, not string array.

  // Open the output document
  PdfDocument outputDocument = new PdfDocument();

  // Iterate files
  foreach (string file in files)
  {
    // Open the document to import pages from it.
    PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

    // Iterate pages
    int count = inputDocument.PageCount;
    for (int idx = 0; idx < count; idx++)
    {
      // Get the page from the external document...
      PdfSharp.Pdf.PdfPage page = inputDocument.Pages[idx];
      // ...and add it to the output document.
      outputDocument.AddPage(page);
    }
  }

  // Save the document...
  outputDocument.Save(outFile);
  outputDocument.Dispose();
}

从经典 ASP 调用:

Dim l_sPath : l_sPath = "D:\test\"
oPDF.MergeMultiplePDF Array(l_sPath & "sample1.pdf", l_sPath & "sample2.pdf", l_sPath & "sample3.pdf" ), l_sPath & "output.pdf"

工作正常,因为数组是一个对象 VARIANT,我在 .NET 类中处理数组。

但是,如果我在经典 ASP 中有一个“动态”数组,我会得到通常的错误,即参数不正确,就像您可以在此处的许多帖子中找到的那样...

示例:

Dim myFiles(10)
For i = 0 To UBound(myFiles)
  myFiles(i) = "test" & i & ".pdf"
Next
oPDF.MergeMultiplePDF myFiles, l_sPath & "output.pdf"

这会遇到参数错误。

我的解决方法:

oPDF.MergeMultiplePDF Split(Join(myFiles,","),","), l_sPath & "output.pdf"

然后就可以了。

两者都是 Array() 类型的对象。

所以有人知道为什么处理方式不同吗?

【问题讨论】:

  • 投反对票的人能告诉我为什么吗?
  • 您发布的代码应该可以工作 VarType(Array...)=VarType(myFiles)=VarType(Split...)=8204 (array of variant) 。您确定问题不是来自您的 C# 实现吗?
  • @SimonMourier VarType() myFiles 和 Split/Join 的 myFiles 都是 8204,这是正确的。但是像我在示例中写的那样将 myFiles 设置为参数,我得到一个错误 800a0005(无效参数)。这就是为什么我发布我的问题以找到这种奇怪情况的答案。在 c# 中它只是定义为 object[],所以这无关紧要,调用失败,而不是方法内的代码。
  • 我明白你的意思,但我已经用一个 .VBS 文件对其进行了测试,它对我有用(看看这个:pastebin.com/q4eZsKbU),我不明白为什么它应该吨。那么显示的代码真的是失败的代码吗?
  • 我添加了我的 C# 函数,也许这有帮助。但是如果代码产生错误,我会得到一个异常,而不是一个无效的参数错误。我也将我的代码从 asp classic 移到了 vbscript.vbs 文件,同样的错误在那里。我试图减少我的代码(拿你的样本)来调查......

标签: c# .net asp-classic


【解决方案1】:

您发布的代码应该像在 VBScript 中一样工作

VarType(Array(...)) = VarType(myFiles) = VarType(Split(...)) = 8204

8204 = 0x200C,即 VT_ARRAY | VT_VARIANT,在 .NET 中确实转换为 object[]

因此,实际代码与此处显示的示例不同。

【讨论】:

  • 提示:正如您在我的回答中所读到的,问题是数组的创建。使用 ReDim 初始化的数组似乎无法将其值传递给 .NET 方法。所以我的解决方法仍然是必要的,但我知道为什么。
【解决方案2】:

在 ASP 中定义一个动态数组,例如 ReDim myFiles(max_count),其中 max_count 是一个数值,会导致问题。 Dim myFiles(10) 作为测试,例如 Simon 测试的工作。

@Simon,请将您的 cmets 设置为答案,以便我接受。

【讨论】:

    猜你喜欢
    • 2019-01-29
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多