惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

博客园 - Kenny田

iOS 下 Podfile 使用方法 使用 NVM 管理不同的 Node.js 版本 将React Native集成至Android原生应用 Gradle在Windows环境与Linux上配置有哪些不同? 学习Android Studio里的Gradle 解决 Android Studio 乱码问题 不知道如何处理的Windows Phone异常 用命令行安装并启动Windows Phone 8 App 演示Microsoft Advertising SDK for Windows Phone 8.1 PLSQL Developer 初始化错误 [原]初次运用数据缓存机制 [原]执行存储过程后返回影响的行数 [原]可定义的英文小日历 可逆加密解密程序(ASP版) [原]有TreeNode”并不包含“Nodes”的定义困扰的朋友看过来 全文服务(Microsoft 搜索)不可用。系统管理员必须启动此服务 [转]解决sql2000挂起无法安装的问题 - Kenny田 [转]安装SQL Server 2000 时出现 command line option syntax error [原]如何将一个表中的某一列的数据全部复制到另一个表中 - Kenny田
在Windows Phone项目中调用C语言DLL
Kenny田 · 2014-07-11 · via 博客园 - Kenny田

在Windows Phone项目中调用C语言写的DLL

最近接到一个需求,需要在WP里调用一个C语言写的DLL,并且说Android和iOS都可以,问我WP是否可以这样? 我说我调研一下,就有了下面的文章。

在传统C# WinForm 里调用Win32 DLL都不容易(可能用惯了C#),要用P/Invoke,然后DllImport什么什么,那WP里不是更麻烦?

先看看网上有没有可用的文章,结果还真找到devdiv中的文章,但其中有两处错误,所以我fix bug并且整理一下,然后展示给大家。

1.1、建立"模拟"C语言生成DLL的工程

1.2、创建好project后, 就看到两个与之相同名称的文件

1.3、在.h文件里写入

#pragma once

extern "C" int _declspec(dllexport)Multiplication(int i, int j);

1.4、在.cpp文件里写入

#include "pch.h"
#include "CalculatorDynamicLinkLibrary.h"

int Multiplication(int i, int j)
{
    int calc = i * j;

    return calc;
}

1.5、编译这个project,在solution文件夹找到Debug,然后就能看到我们模拟生成的DLL

2.1、创建 C++ Windows Runtime Component 项目

2.2、创建好project后, 就看到两个与之相同名称的文件

2.3、在.h文件里写入

#pragma once
#include <collection.h>
#include <../CalculatorDynamicLinkLibrary/CalculatorDynamicLinkLibrary.h>

namespace CalculatorInvoke
{
    public ref class CalculatorInvoker sealed
    {
    public:
        CalculatorInvoker();

        int Mult(int i, int j);
    };
}

2.4、在.cpp文件里写入

#include "pch.h"
#include "CalculatorInvoke.h"

using namespace CalculatorInvoke;
using namespace Platform;

CalculatorInvoker::CalculatorInvoker()
{
}
int CalculatorInvoker::Mult(int i, int j)
{
    return Multiplication(i, j);
}

如果这时你着急编译,肯定会出错,不信就试试,呵呵!

2.5、在component project上,右键属性,找到设置Linker,在Additional Dependencies里填写第一个project的lib文件

devdiv网站是教的是设置.dll文件,我试了会报错

2.6、设置General,这里一定不能错,不然就会报找不到.lib文件。devdiv教的是指向绝对路径,如果把项目移到别的目录下还会报找不到.lib路径。

点Additional Library Directories 的下拉,再点Edit,就弹出如下窗口

Tips:关于类似”$(SolutionDir)“的用法,已经在链接3中给出了,列举比较详情,感谢作者!

3.1、创建 Windows Phone 项目,这里是大家最熟悉的部分了

 

 3.2、添加引用Windows Phone Component项目,或者后期引用Windows Phone Component生成出来的DLL也行。

3.3、添加Win32 DLL文件到Windows Phone项目中,并在属性里设置为conent, copy always。

3.4、添加WP里C#的代码

private void CalculateRsult(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (string.IsNullOrWhiteSpace(input1.Text) || string.IsNullOrWhiteSpace(input2.Text))
    {
        MessageBox.Show("输入框不能为空!", "友情提示", MessageBoxButton.OK);
        return;
    }

    CalculatorInvoker calculator = new CalculatorInvoker();

    int i = Convert.ToInt32(this.input1.Text);
    int j = Convert.ToInt32(this.input2.Text);
    int result = calculator.Mult(i, j);

    this.txtResult.Text = string.Format("{0}", result);
}

编译步骤:先CalculatorDynamicLinkLibrary,再CalculatorInvoker,最后CalculatorApp

3.5、计算结果

源代码下载

参考文档:

http://www.devdiv.com/forum.php?mod=viewthread&tid=135252

http://www.jarredcapellman.com/2012/11/03/how-to-get-c-winrt-in-a-windows-phone-8-application/

http://www.cnblogs.com/lidabo/archive/2012/05/29/2524170.html