nodejs調用delphi編寫的dll

公司的業務須要,nodejs要讀取文件版本號。node

同事要求我用delphi編寫dll,以供nodejs調用,結果經過json返回。json

delphi代碼以下:測試

 1 function GetFileInfo(APath: PAnsiChar): PAnsiChar;  2 var
 3   sPath: string;  4  FInfoSize, Temp, Len: Cardinal;  5  InfoBuf: Pointer;  6  TranslationLength: Cardinal;  7  TranslationTable: Pointer;  8  LanguageID, CodePage, LookupString: String;  9  Value: PChar; 10  vJ: ISuperObject; 11 begin
12   sPath := Utf8ToAnsi(strPas(APath)); 13   FInfoSize := GetFileVersionInfoSize(PChar(sPath), Temp); 14   vJ := SO; 15   if FInfoSize > 0 then
16   begin
17     InfoBuf := AllocMem(FInfoSize); 18     try  // try
19       GetFileVersionInfo(PChar(sPath), 0, FInfoSize, InfoBuf); 20 
21       if VerQueryValue( InfoBuf, '\VarFileInfo\Translation', TranslationTable, TranslationLength ) then
22       begin
23         CodePage := Format( '%.4x', [ HiWord( PLongInt( TranslationTable )^ ) ] ); 24         LanguageID := Format( '%.4x', [ LoWord( PLongInt( TranslationTable )^ ) ] ); 25       end; 26       LookupString := 'StringFileInfo\' + LanguageID + CodePage + '\'; 27 
28       if VerQueryValue( InfoBuf, PChar( LookupString + 'CompanyName' ), Pointer( Value ), Len ) then
29         vJ.S['CompanyName'] := Value; 30       if VerQueryValue( InfoBuf, PChar( LookupString + 'FileDescription' ), Pointer( Value ), Len ) then
31         vJ.S['FileDescription'] := Value; 32       if VerQueryValue( InfoBuf, PChar( LookupString + 'FileVersion' ), Pointer( Value ), Len ) then
33         vJ.S['FileVersion'] := Value; 34       if VerQueryValue( InfoBuf, PChar( LookupString + 'InternalName' ), Pointer( Value ), Len ) then
35         vJ.S['InternalName'] := Value; 36       if VerQueryValue( InfoBuf, PChar( LookupString + 'LegalCopyright' ), Pointer( Value ), Len ) then
37         vJ.S['LegalCopyright'] := Value; 38       if VerQueryValue( InfoBuf, PChar( LookupString + 'LegalTrademarks' ), Pointer( Value ), Len ) then
39         vJ.S['LegalTrademarks'] := Value; 40       if VerQueryValue( InfoBuf, PChar( LookupString + 'OriginalFilename' ), Pointer( Value ), Len ) then
41         vJ.S['OriginalFilename'] := Value; 42       if VerQueryValue( InfoBuf, PChar( LookupString + 'ProductName' ), Pointer( Value ), Len ) then
43         vJ.S['ProductName'] := Value; 44       if VerQueryValue( InfoBuf, PChar( LookupString + 'ProductVersion' ), Pointer( Value ), Len ) then
45         vJ.S['ProductVersion'] := Value; 46       if VerQueryValue( InfoBuf, PChar( LookupString + 'Comments' ), Pointer( Value ), Len ) then
47         vJ.S['Comments'] := Value; 48     finally  // wrap up finally
49  FreeMem(InfoBuf, FInfoSize); 50     end;  // end try finally
51   end; 52   Result := PAnsiChar(AnsiString(vJ.AsJSon)); 53 end;

PS:ui

1. 用到的字符串可能較長,注意調用ShareMem編碼

2. 全部的string類型都須要定義成PAnsiCharspa

3. 此處nodejs使用的編碼是utf8,須要轉碼code

4. 返回值,需先經過AnsiString轉換,再轉換PAnsiChar,否則只能讀取到第一個字符orm

 

nodejs測試代碼以下:blog

 1 var ffi = require('ffi');  2 
 3 try {  4     var demo = ffi.Library('./fileInfo', {  5         'GetFileInfo': [ 'string', ['string'] ],  6  });  7 
 8  console.log(demo);  9     const info = demo.GetFileInfo('C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe');
10 console.log(JSON.parse(info)); 11 console.log('End Test'); 12 } catch(err) { 13 console.log(err); 14 }

測試結果以下:ip

相關文章
相關標籤/搜索