公司的业务需要,nodejs要读取文件版本号。

同事要求我用delphi编写dll,以供nodejs调用,结果通过json返回。

delphi代码如下:

 function GetFileInfo(APath: PAnsiChar): PAnsiChar;
var
sPath: string;
FInfoSize, Temp, Len: Cardinal;
InfoBuf: Pointer;
TranslationLength: Cardinal;
TranslationTable: Pointer;
LanguageID, CodePage, LookupString: String;
Value: PChar;
vJ: ISuperObject;
begin
sPath := Utf8ToAnsi(strPas(APath));
FInfoSize := GetFileVersionInfoSize(PChar(sPath), Temp);
vJ := SO;
if FInfoSize > then
begin
InfoBuf := AllocMem(FInfoSize);
try // try
GetFileVersionInfo(PChar(sPath), , FInfoSize, InfoBuf); if VerQueryValue( InfoBuf, '\VarFileInfo\Translation', TranslationTable, TranslationLength ) then
begin
CodePage := Format( '%.4x', [ HiWord( PLongInt( TranslationTable )^ ) ] );
LanguageID := Format( '%.4x', [ LoWord( PLongInt( TranslationTable )^ ) ] );
end;
LookupString := 'StringFileInfo\' + LanguageID + CodePage + '\'; if VerQueryValue( InfoBuf, PChar( LookupString + 'CompanyName' ), Pointer( Value ), Len ) then
vJ.S['CompanyName'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'FileDescription' ), Pointer( Value ), Len ) then
vJ.S['FileDescription'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'FileVersion' ), Pointer( Value ), Len ) then
vJ.S['FileVersion'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'InternalName' ), Pointer( Value ), Len ) then
vJ.S['InternalName'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'LegalCopyright' ), Pointer( Value ), Len ) then
vJ.S['LegalCopyright'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'LegalTrademarks' ), Pointer( Value ), Len ) then
vJ.S['LegalTrademarks'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'OriginalFilename' ), Pointer( Value ), Len ) then
vJ.S['OriginalFilename'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'ProductName' ), Pointer( Value ), Len ) then
vJ.S['ProductName'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'ProductVersion' ), Pointer( Value ), Len ) then
vJ.S['ProductVersion'] := Value;
if VerQueryValue( InfoBuf, PChar( LookupString + 'Comments' ), Pointer( Value ), Len ) then
vJ.S['Comments'] := Value;
finally // wrap up finally
FreeMem(InfoBuf, FInfoSize);
end; // end try finally
end;
Result := PAnsiChar(AnsiString(vJ.AsJSon));
end;

PS:

1. 用到的字符串可能较长,注意调用ShareMem

2. 所有的string类型都需要定义成PAnsiChar

3. 此处nodejs使用的编码是utf8,需要转码

4. 返回值,需先通过AnsiString转换,再转换PAnsiChar,不然只能读取到第一个字符

nodejs测试代码如下:

 var ffi = require('ffi');

 try {
var demo = ffi.Library('./fileInfo', {
'GetFileInfo': [ 'string', ['string'] ],
}); console.log(demo);
const info = demo.GetFileInfo('C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe');
console.log(JSON.parse(info));
console.log('End Test');
} catch(err) {
console.log(err);
}

测试结果如下:

nodejs调用delphi编写的dll的更多相关文章

  1. C# 调用delphi编写的dll

    技术实现 如何逐步实现动态库的加载,类型的匹配,动态链接库函数导出的定义,参考下面宏定义即可: #define LIBEXPORT_API extern "C" __declspe ...

  2. [JNA系列]Java调用Delphi编写的Dll之JNA使用

    介绍 给大家介绍一个最新的访问本机代码的 Java 框架 —JNA . JNA(Java Native Access) 框架是一个开源的 Java 框架,是 SUN 公司主导开发的,建立在经典的 JN ...

  3. [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PWideChar

    Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessWide(pvData: P ...

  4. [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PAnsiChar

    Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessAnsi(pvData: P ...

  5. Go调用Delphi编写的DLL

    参数整数没有问题,但是如果是字符串,要注意几个细节. 记录如下: 1.Delphi定义函数的时候,字符串参数需要使用PChar类型 2.Go传递参数的时候要将字符串转成UTF16的指针,接收的时候采用 ...

  6. [JNA系列]Java调用Delphi编写的Dll之Delphi与JAVA基本数据类型对比

    Delphi与JAVA基本数据类型对比 类型 Delphi关键字 JAVA关键字 字节 备注 范围 整型 Shortint byte 1 有符号8位 -128..127 Byte 1 无符号8位 0 ...

  7. 分享一次C#调用Delphi编写Dll程序

    1.前言: 最近接手了一个项目需要和Delphi语言编写的一个系统进行一些接口的对接,数据在传输过程中采用Des加密方式,因为Delphi 平台的加密方式和C#平台的加密方式不互通,所以采用的方式是C ...

  8. C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)

        C#调用C++编写的DLL函数, 以及各种类型的参数传递 1. 如果函数只有传入参数,比如: C/C++ Code Copy Code To Clipboard //C++中的输出函数 int ...

  9. C#动态调用C++编写的DLL函数

    C#动态调用C++编写的DLL函数 动态加载DLL需要使用Windows API函数:LoadLibrary.GetProcAddress以及FreeLibrary.我们可以使用DllImport在C ...

随机推荐

  1. 记一次bash脚本报错原因

    准备部署上次写的 爬虫的定时任务,发现sh脚本 写为最简单的cd ,也报错 后来网上一波了解,原因竟是 : 额,格式问题,我为了图方便是在window里用notepad++写的,然后我再linux 系 ...

  2. ABAP动态生成经典应用之Dynamic SQL Excute 程序

    [转自http://blog.csdn.net/mysingle/article/details/678598]开发说明:在SAP的系统维护过程中,有时我们需要修改一些Table中的数据,可是很多Ta ...

  3. Struts详解

    1.什么是MVC? MVC是Model,View,Controller的缩写,MVC是Application开发的设计模式, 也就是大家所知道的Model2.在MVC的设计模式中,它包括三类对象:(1 ...

  4. Java并发之BlockingQueue

      一.Queue Queue是队列接口是 Collection的子接口.除了基本的 Collection操作外,队列还提供其他的插入.提取和检查操作.每个方法都存在两种形式:一种抛出异常(操作失败时 ...

  5. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  6. 爬虫 spider

    python 2.x # -*- coding: utf-8 -*-import reimport urllib url = 'http://tieba.baidu.com/p/4872795764' ...

  7. java-从这里开始认识

    <java是什么:>Programming language 程序语言Development environment 开发环境Application environment 应用环境Dep ...

  8. 算法(Algorithms)第4版 练习 1.3.3

    (a) 4 3 2 1 0 9 8 7 6 5 (b) 4 6 8 7 5 3 2 9 0 1 (c) 2 5 6 7 4 8 9 3 1 0 (d) 4 3 2 1 0 5 6 7 8 9 (e) ...

  9. 使用 sqoop 将mysql数据导入到hive表(import)

    Sqoop将mysql数据导入到hive表中 先在mysql创建表 CREATE TABLE `sqoop_test` ( `id` ) DEFAULT NULL, `name` varchar() ...

  10. SQLServer 一些有用的语句

    SET STATISTICS TIME ON 记录查询的相关数据 生成随机Guid SELECT NewID() 按照某一列排序并生成序号 select Row_Number() OVER (ORDE ...