C#调用C++方法,C#使用c++方法返回类或结构体
2011-04-27 16:00
681 查看
C#调用C++方法,C#使用c++方法返回类或结构体。
1. 在c++中定义结构体,类,处理方法;要给c#调用的方法要加extern "C" __declspec(dllexport) 修饰
2. 在C#中使用C++方法,首先需要定义C++中的Foo(在c++中定义为类class),Bar(在C++中定义为struct)对应的数据结构struct;然后定义extern的方法,如下代码所示:
C++中的类或者结构在C#中的定义代码可以使用微软提供的工具(P/Invoke Interop Assistant)生成。
在运行C#程序前要把C++dll复制到C#程序运行目录下。
1. 在c++中定义结构体,类,处理方法;要给c#调用的方法要加extern "C" __declspec(dllexport) 修饰
// CppDll.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #define EXPORT_DLL extern "C" __declspec(dllexport) //暴露的方法调用 EXPORT_DLL int Add(int a,int b) { return a + b; } //返回数据结构定义 struct Bar { public : int id; char* name; }; //使用引用返回结构 EXPORT_DLL void GetBar(Bar& bar) { //Bar b; bar.id = 10; bar.name = "hi bar 中文了"; //bar = b; } //返回类 class Foo { public : int id; char* name; }; //引用返回类 EXPORT_DLL void GetFoo(Foo& foo) { foo.id = 100; foo.name = "I'm 傻瓜"; }
2. 在C#中使用C++方法,首先需要定义C++中的Foo(在c++中定义为类class),Bar(在C++中定义为struct)对应的数据结构struct;然后定义extern的方法,如下代码所示:
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace UseCppDll { [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct Bar { /// int public int id; /// char* [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] public string name; } [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct Foo { /// int public int id; /// char* [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] public string name; } class Program { [DllImport("CppDll.dll",EntryPoint="Add")] extern static int Add(int a, int b); [DllImport("CppDll.dll", EntryPoint = "GetBar",CharSet=CharSet.Ansi)] extern static void GetBar(ref Bar bar); [DllImport("CppDll.dll", EntryPoint = "GetFoo", CharSet = CharSet.Ansi)] extern static void GetFoo(ref Foo foo); static void Main(string[] args) { Console.WriteLine(Add(5,4)); Bar b = new Bar(); GetBar(ref b); Console.WriteLine("b's id is " + b.id); Console.WriteLine("b's name is " + b.name); Foo f = new Foo(); GetFoo(ref f); Console.WriteLine("f's id is " + f.id); Console.WriteLine("f's name is " + f.name); Console.Read(); } } }
C++中的类或者结构在C#中的定义代码可以使用微软提供的工具(P/Invoke Interop Assistant)生成。
在运行C#程序前要把C++dll复制到C#程序运行目录下。
相关文章推荐
- C#调用C++方法,C#使用c++方法返回类或结构体
- C#调用C++方法,C#使用c++方法返回类或结构体
- C#调用C++方法,C#使用c++方法返回类或结构体。
- C#调用C++方法,C#使用c++方法返回类或结构体
- C#调用C++方法,C#使用c++方法返回类或结构体
- C#调用C++方法,C#使用c++方法返回类或结构体
- C#调用C++方法,C#使用c++方法返回类或结构体
- C# 调用C++ dll 方法返回char* 乱码
- C#调用c++的dll 回调使用方法
- android调用C# 服务返回对象使用方法说明
- Android-使用C++实现调用本地方法返回字符串显示在界面上/NDK-JNI开发实例(八)
- C#调用C++ dll时,结构体引用传参的方法
- Unity使用C#调用C++dll传递指针参数、接收指针返回值
- 使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法
- C#中的类SqlCommand对象使用方法ExecuteNonQuery()调用SQLServer存储过程时,存储过程执行成功,数据发生改变,但是返回-1
- C#调用c++的dll 回调使用方法
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据
- 在C++创建DLL,以及使用C++或者C#分别调用DLL中的方法
- 转:使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法
- PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)