Win8 Metro C# 调用 C++代码
2014-02-28 00:15
267 查看
在写Win8 Store App 的时候,可能遇到需要调用C++的代码。
比如有个压缩算法,Lz77,有C++的实现,改成C#实现需要很多时间,所以通过C++/CX包装一下,C#就能直接调用C++的实现了。
首先创建C++静态库。必须使用extern “C”,同时使用#pragma once还避免重复include,用#ifndef __SOMEFILE_H__方式可能会报链接错误。
同时设置工程属性
staticLibrary Project Properties:
staticLibrary -> Properties -> C/C++ -> General -> Consume Windows Runtime Extension ->Yes (/ZW)
staticLibrary.h
staticLibrary.cpp,如果这里是.c后缀的话,需要设置staticLibrary.c
-> Properties -> C/C++ -> Advanced -> Compile As -> Compile
as C++ Code (/TP)
然后创建Windows RunTime Component:
wrc里面,Array<uint8>^ 类型,在C#里面能直接用byte[]对应。
'Class1.h'
'Class1.cpp'
比如有个压缩算法,Lz77,有C++的实现,改成C#实现需要很多时间,所以通过C++/CX包装一下,C#就能直接调用C++的实现了。
首先创建C++静态库。必须使用extern “C”,同时使用#pragma once还避免重复include,用#ifndef __SOMEFILE_H__方式可能会报链接错误。
同时设置工程属性
staticLibrary Project Properties:
staticLibrary -> Properties -> C/C++ -> General -> Consume Windows Runtime Extension ->Yes (/ZW)
staticLibrary.h
#pragma once #ifdef __cpluscplus extern "C" { #endif // Returns a + b double sLibAdd(double a, double b); #ifdef __cpluscplus } #endif
staticLibrary.cpp,如果这里是.c后缀的话,需要设置staticLibrary.c
-> Properties -> C/C++ -> Advanced -> Compile As -> Compile
as C++ Code (/TP)
#include "pch.h" #include "staticLibrary.h" double sLibAdd(double a, double b) { return a + b; }
然后创建Windows RunTime Component:
wrc里面,Array<uint8>^ 类型,在C#里面能直接用byte[]对应。
'Class1.h'
#pragma once namespace wrc { public ref class Class1 sealed { public: Class1(); double Class1::Add(double a, double b); }; }
'Class1.cpp'
#include "pch.h" #include "Class1.h" #include "staticLibrary.h" using namespace wrc; using namespace Platform; Class1::Class1() { } double Class1::Add(double a, double b) { double retVal = 0; retVal = sLibAdd(a, b); return retVal; }
相关文章推荐
- C# CLRInsideOut 托管代码与非托管代码互操作,产生相关调用代码的好工具 C++ 头文件转C# 的好工具(转
- C++通过DLL调用C#代码
- 用VS2008生成C++代码的dll及在C#中的调用及常见的类型转换
- C#托管代码与C++非托管代码互相调用1
- C#托管代码与C++非托管代码互相调用一(C#调用C++代码&.net 代码安全)
- C++通过DLL调用C#代码
- C++ 通过DLL调用C#代码
- C#托管代码与C++非托管代码互相调用
- C#托管代码与C++非托管代码互相调用一(C#调用C++代码&.net 代码安全)
- C#托管代码与C++非托管代码互相调用
- C++调用C#代码
- C++ 通过代码托管的方式调用c#的httpAgilityPack库
- C#托管代码调用C++非托管代码
- C#托管代码与C++非托管代码互相调用一(C#调用C++代码&.net 代码安全)
- C#调用C++写的代码(VS2015)
- C++通过DLL调用C#代码
- 用VS2008生成C++代码的dll及在C#中的调用及常见的类型转换
- C#通过pInvoke调用C++代码传值简单解析
- C++调用C#代码
- windows metro app c#工程调用c++写的静态库