转自: ttp://blog.csdn.net/leonpengweicn/article/details/38759209
因为要在metro app 的工程中调用别人写好的C++静态库,所以研究了一下,最后的结果是c#是不能直接调用c++的lib文件的。
只能调用dll或者windows runtime component,特此把这个情况记录一下。
1,c++的lib必需要支持windows应用商店
2,创建windows runtime component(windows运行时组件)工程 ,名称为 WindowsRuntimeComponent2
3,把lib和.h文件拷贝到WindowsRuntimeComponent2的目录下
4,点击WindowsRuntimeComponent2工程名 -> 属性 -> 配置属性 -> c/c++ -> 常规 -> 附加包含目录 -> 选到.h所在的目录
5,添加代码
//---Class1.h-------------------------------
#pragma once
#pragma comment(lib,"Lib1.lib")
namespace WindowsRuntimeComponent2
{
public ref class Class1 sealed
{
public:
Class1();
double Class1::Add(double a, double b);
};
}
//--------- Class1.cpp---------------------
#include "pch.h"
#include "Class1.h"
#include "Lib1.h"
using namespace WindowsRuntimeComponent2;
using namespace Platform;
Class1::Class1()
{
}
double Class1::Add(double a, double b)
{
double retVal = 0;
retVal = sLibAdd(a, b);
return retVal;
}
6,新建windows metro app工程 App2
7,右键点击 App2 -> 添加现有项 -> 选中 WindowsRuntimeComponent2
8,右键点击App2-> 添加引用 -> 解决方案 -> 项目 -> 勾上 WindowsRuntimeComponent2
9,编辑MainPage.xaml.cs,添加代码
WindowsRuntimeComponent2.Class1 component = new WindowsRuntimeComponent2.Class1();
double r = component.Add(1, 2);
Debug.WriteLine("result : " + r);
10,编译运行
11,配置lib目录 点击WindowsRuntimeComponent2工程名 -> 属性 -> 配置属性 -> 链接器 -> 输入 -> 附加依赖项 -> lib的目录
参考:
1,演练:创建和使用静态库 (C++)
http://msdn.microsoft.com/zh-cn/library/ms235627.aspx#BKMK_CreateLibProject
http://blog.csdn.net/problc/article/details/20084483
3,与众不同 windows phone (48) - 8.0 其它: C# 调用 C++
http://www.cnblogs.com/webabcd/p/3526422.html