0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows 64bit Electronと32bit C言語DLLの共存

0
Last updated at Posted at 2026-07-18

はじめに

このたび、Client/ServerモデルでローカルPCに64bit Electronと 32bit DLL を共存できないか、研鑽しましたので、結果をご報告いたします。

背景

(1)AIが、そろそろElectronを64bitの最新LTSにしないかと、盛んに言ってくる。
(2)AIでユーザーインターフェースを作ることになる近い将来に備えて、フロントエンドはAI的に見て実績のある64bit最新NodeJS-LTSにしておきたい。
(3)そうは言っても、業務の現場では、レガシー32bit-DLLは安定しており捨てられない。

チャレンジしたシステム構成

64bitプロセスと32bitプロセスのクライアント・サーバーです。
32bit側で ffi-napiを使用します。
sys1.jpg

呼出例その1

device1_32.dll の関数 sm2cin

__declspec(dllexport) int sm2cin() {
  return 3;
}

Electron側のクライアントラッパー

let adll1 = await ffi_honji_cli(
              "./dll/device1_32.dll",
              {
	              "sm2cin": ["int",[]]
			   });
const value = await adll1.ffi_call( "sm2cin");
console.log( "sm2cin()=", value);   // 3

ffi-napiオンリーの場合は透過的に行けました。

呼出例その2

Client/Server間はJSON文字列のやりとりなので、DLL関数がOUTPUT引数をとる場合で困りました。ひねりました。
device2_32.dll の関数 sm2cin

__declspec(dllexport) int sm2cin(int a, int b, int* sum) {
  *sum = a + b;
  return 0;
}

Electron側のクライアントラッパー

let adll2 = await ffi_honji_cli(
              "./dll/device2_32.dll",
              {
	              "sm2cin": ["int",["int", "int", ["int",1]]]
			  });
const value = await adll2.ffi_call( "sm2cin", 1, 2, [0]);
console.log( "sm2cin()=", value);   // 0
console.log( "output arg[2]=", adll2.mojson.xotarg[2]);  // 3

第3引数のOUTPUTを、int型の長さ1の配列というように見立てて、
["int", 1] と表現しました。

無事、通信出来ているようです。

呼出例その3

DLL関数が構造体のIN/OUT引数をとる場合です。
device3_32.dll の関数 sm2cin

typedef struct { char a, short b, int c } TSTRUC;
__declspec(dllexport) int sm2cin( TSTRUC *s, int *sum) {
  *sum = s->a + s->b + s->c;
  return 0;
}

Electron側のクライアントラッパー

let adll3 = await ffi_honji_cli(
              "./dll/device3_32.dll",
              {
                "sm2cin": ["int",[{a:"char", b:"short", c:"int"},1], ["int", 1]]
              });
const value = await adll3.ffi_call( "sm2cin", [{a:1, b:2, c:4}], [0]);
console.log( "sm2cin()=", value);
console.log( "output arg[1]=", adll3.mojson.xotarg[1]);  // 7

第1引数の構造体ポインタ(IN)をオブジェクトの長さ1の配列で表現しました。
[オブジェクト,1]

MSVCのデフォルトアラインメントですが、構造体ポインタを渡せました。

ref-struct-napi, ref-array-napiを駆使されている場合

これ以上複雑なケースでは、ffi-napiサーバー側に独自実装して、クライアント・サーバー間のJSON文字列のやりとりを拡張して頂くことになりそうです。

ソースコード

ffi-napiクライアント・サーバーのソースコードを githubに置きました。先達のみなさまからの御意見・つっこみを歓迎いたします。

$ git clone https://github.com/yoike123/ffi_honji.git

Node.jsも中級者の入り口なのであまり凝ったことは出来ませんでした。
叱咤・激励、よろしく、お願いいたします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?