请教高手: C#不可能直接连接C++你可以找C#中替代的方法或者用C#调用C++生成的DLL 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一:想学好C#首先要丢弃C++,C等等。(如果想调用C++的函数,将C++封装为动态链接库,或者com)第二:你的链接是指什么呢?C++的编译器有很多参数,你只需要启动它的时候传入相应的参数就可以编译你的C++程序了(C++编译器当然编译C++的程序啦,编译C#就别想了)。 示例在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 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: /LDint __declspec(dllexport) SampleMethod(int i){ return i*10;} 该示例使用两个文件 CM.cs 和 Cmdll.c 来说明 extern。C 文件是示例 2 中创建的外部 DLL,它从 C# 程序内调用。 复制代码 // cm.csusing 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 返回。 textbox控件能否改变选定文本的背景色? 【在线等】C#下载功能出了问题,请指教! listview不能透明,要同时显示多个图片,还能用什么控件。 ,窗体重绘 如何记语法(方法,属性,类,接口......)?? 网站访问出错 高手进,可能是清除内存的问题 C#服务(windows Service)如何启动桌面程序呀? 局域网共享的权限设置 问两个问题,请大家提供思路,能帮忙的另有重谢!回帖有分! 扫描枪获得数据的问题(跪求) 高手指点下~~在线等~~~`建立文件然后要用,他说另一进程正在使用
第二:你的链接是指什么呢?C++的编译器有很多参数,你只需要启动它的时候传入相应的参数就可以编译你的C++程序了(C++编译器当然编译C++的程序啦,编译C#就别想了)。
在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 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 返回。