将用vc ++构建的dll导入C#代码

时间:2014-07-07 08:55:18

标签: c#

我的dll是用VC ++构建的,创建的dll用于一个Visual Basic应用程序。现在我想使用该DLL并将其导入我的c#应用程序。但是当我尝试添加该dll的引用时,我收到了错误

  无法添加

dll。请确保文件是可访问的,这是有效的程序集或COM组件。

我在c#apllication中实现了下面的代码,现在我想使用tssfor.dll。但是从哪里或如何添加该DLL?请帮帮我。

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Runtime.InteropServices;

命名空间DLLTest

{

public class Class1

{

    [DllImport("tssfor.dll")]

    public static extern int tssfor(string Infile, long l1, string tmpfile, long l2, string Lookfile, long l16, string RainFile,
        long l18, string affiliate, long l6, string Prodname, long l20, string version, long l7, string Vernum, long l19, string Country, long l17,
        string metric, long l15, string Projnam, Int32 l8, string Projnum, long l9, string projloc, long l10, string projco, long l11, string projeng,
        long l12, string projdate, long l13, string projnote, long l14, string Rainloc, long l3, string RainLat, long l4, string RainLong, long l5, Single elevft,
        Single areareal, long Varint, long Numsv, long ptstor, Single loadreal, Single sg, Single ps, Single pct, Single vs, Single stor, Single storq, Single split,
        Single dwf, Single intensv, Single pflow, Single ptss);

    public void tss(string Infile, long l1, string tmpfile, long l2, string Lookfile, long l16, string RainFile,
        long l18, string affiliate, long l6, string Prodname, long l20, string version, long l7, string Vernum, long l19, string Country, long l17,
        string metric, long l15, string Projnam, Int32 l8, string Projnum, long l9, string projloc, long l10, string projco, long l11, string projeng,
        long l12, string projdate, long l13, string projnote, long l14, string Rainloc, long l3, string RainLat, long l4, string RainLong, long l5, Single elevft,
        Single areareal, long Varint, long Numsv, long ptstor, Single loadreal, Single sg, Single ps, Single pct, Single vs, Single stor, Single storq, Single split,
        Single dwf, Single intensv, Single pflow, Single ptss)
    {
        tssfor(Infile, l1, tmpfile, l2, Lookfile, l16, RainFile, l18, affiliate, l6, Prodname, l20,version, l7, Vernum, l19, Country, l17, metric, l15,
            Projnam, l8, Projnum, l9, projloc, l10, projco, l11, projeng, l12, projdate, l13, projnote, l14, Rainloc, l3, RainLat, l4, RainLong, l5, elevft,
            areareal, Varint, Numsv, ptstor, loadreal, sg, ps, pct, vs, stor, storq, split, dwf, intensv, pflow, ptss);
    }

}

}

1 个答案:

答案 0 :(得分:1)

无法将非托管DLL添加为.net项目的引用。相反,你需要使用某种形式的互操作。常见的选择包括:

  1. P /调用
  2. COM
  3. C ++ / CLI