GithubHelp home page GithubHelp logo

wzdxy / electron-ffi-demo Goto Github PK

View Code? Open in Web Editor NEW
90.0 3.0 25.0 79 KB

在 Electron 项目中通过 node-ffi-napi 调用 c/c++的 dll 动态链接库

HTML 49.40% JavaScript 50.60%
electron ffi node-ffi node-addon

electron-ffi-demo's Introduction

简介

目前使用的Node版本是 12, Electron 版本 6.0.10 npm包为 [email protected], (最早使用Electron 1.7时用的是 ffi, 但 ffi 已停止支持最新版本的Node)

Quick Start

# dev
npm i
npm start

# build
npm run dist

Build

  • Use electron-builder to Build
  • Add Config:
        "extraFiles": [
            "dll"               // Where *.dll File
        ]

ScreenShot

MyDLL.dll Source Code

  • cpp
#include "stdafx.h"
#include "testdll.h"
#include <iostream>
using namespace std;
float Add(float plus1, float plus2)
{
	float add_result = plus1 + plus2;
	return add_result;
}

char *Hello()
{
	return "Hello This is Cpp Addon";
}

int StrLength(char * str)
{
	return strlen(str);
}
  • h
#pragma once
#ifndef TestDll_H_
#define TestDll_H_
#ifdef MYLIBDLL
#define MYLIBDLL extern "C" _declspec(dllimport)
#else
#define MYLIBDLL extern "C" _declspec(dllexport)
#endif
MYLIBDLL char* Hello();
MYLIBDLL float Add(float plus1, float plus2);
MYLIBDLL int StrLength(char * str);
//You can also write like this:
//extern "C" {
//_declspec(dllexport) int Add(int plus1, int plus2);
//};
#endif
  • def
LIBRARY "MyDLL"
EXPORTS
Add @1

Q&A

  • MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装 .NET Framework 2.0 SDK;2) 安装 Microsoft Visual Stu dio 2005;或 3) 如果将该组件安装到了其他位置,请将其位置添加到系统路径中。 [D:\electron-ffi-demo\node_modules\ref\build\binding.sln]
  • npm install --global windows-build-tools

electron-ffi-demo's People

Contributors

wzdxy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

electron-ffi-demo's Issues

运行后报错:ffi.Library Error: %1 is not a valid Win32 application.

ffi.Library Error: %1 is not a valid Win32 application.
\?\E:\code\electron-ffi-demo\node_modules\ref\build\Release\binding.node
at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:173:20)
at Object.Module._extensions..node (module.js:598:18)
at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:173:20)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at bindings (E:\code\electron-ffi-demo\node_modules\bindings\bindings.js:76:44)
at Object. (E:\code\electron-ffi-demo\node_modules\ref\lib\ref.js:5:47)

关于调用c++动态文件库

我通过node-gyp rebuild把ffi和ref编译成符合electron调用的版本,然后在vs里构建好我的dll文件,在js里调用的时候报了以下错误:
Uncaught Error: Dynamic Linking Error: Win32 error 193 at new DynamicLibrary
都是在x64下的。请问这个问题怎么解决?

打包后的项目:Uncaught Error: Dynamic Linking Error: Win32 error 126

你好请教你一下
electron:3.0.0 x64
node: 11.8.0 x64
dll x64
打的包也是64的,安装运行应用就报:Uncaught Error: Dynamic Linking Error: Win32 error 126;
本地跑起来一切正常,打包后就是不行,能否知道一下啊,谢谢!
return ffi.Library('../../../dll/MyDLL.dll', {
'Add': ['float', ['float', 'float']],
'Hello': ['string', []],
'StrLength': ['int', ['string']]
})
dll文件是放在项目根目录下的;如果按你的demo来build的话请问这里该怎么配置啊:
"extraFiles": [
"dll"
]

ffi.Library的时候, 加载方法的问题

加载对应的方法, 方法需要有出参入参('Add': ['float', ['float', 'float']]) , 第一个是返回的结果类型 , 不返回的时候需要写什么类型, 我试了空\null\undefined , 都是报错. 不知道js调用c++有没有对应的参数类型写法?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.