C#不可能直接连接C++
你可以找C#中替代的方法
或者用C#调用C++生成的DLL

解决方案 »

  1.   

    第一:想学好C#首先要丢弃C++,C等等。(如果想调用C++的函数,将C++封装为动态链接库,或者com)
    第二:你的链接是指什么呢?C++的编译器有很多参数,你只需要启动它的时候传入相应的参数就可以编译你的C++程序了(C++编译器当然编译C++的程序啦,编译C#就别想了)。
      

  2.   

    示例
    在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 User32.dll 库导入的 MessageBox 方法。  复制代码 
    using System;
    using System.Runtime.InteropServices;
    class MainClass 
    {
       [DllImport("User32.dll")]
       public static extern int MessageBox(int h, string m, string c, int type);   static int Main() 
       {
          string myString; 
          Console.Write("Enter your message: ");
          myString = Console.ReadLine();
          return MessageBox(0, myString, "My Message Box", 0);
       }
    }
     此示例使用 C 程序创建一个 DLL,在下一示例中将从 C# 程序调用该 DLL。  复制代码 
    // cmdll.c
    // compile with: /LD
    int __declspec(dllexport) SampleMethod(int i)
    {
       return i*10;
    }
     该示例使用两个文件 CM.cs 和 Cmdll.c 来说明 extern。C 文件是示例 2 中创建的外部 DLL,它从 C# 程序内调用。  复制代码 
    // cm.cs
    using System;
    using System.Runtime.InteropServices;
    public class MainClass 
    {
       [DllImport("Cmdll.dll")]
       public static extern int SampleMethod(int x);   static void Main() 
       {
          Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
       }
    }
     输出
      
    SampleMethod() returns 50.
     备注
    生成项目:使用 Visual C++ 命令行将 Cmdll.c 编译为 DLL:cl /LD Cmdll.c 使用命令行编译 CM.cs: csc CM.cs 这将创建可执行文件 CM.exe。运行此程序时,SampleMethod 将值 5 传递到 DLL 文件,该文件将此值乘以 10 返回。