title: Node.js ffi access to dll
body: <p>We have a C# wrapper to a dll that I'd like to convert over to a node.js wrapper for enhancements.  I can't seem to get the irSAInitialize function to return a handle or successful response.  This is my first attempt at using ffi, but believe I have everything setup correctly (another function in the dll seems to work).</p>
  
  <p>Here is the simple POC code I have which attempts to initialize then log the results.</p>
  
  <pre><code>var ffi = require('ffi');
  var ref = require('ref');
  
  var intPtr = ref.refType('long');
  var dllPath = "O:\\ApiBin\\";
  var dllFile = dllPath + "irApiSA.dll";
  var randConnect = ffi.Library(dllFile, {
      'irSAInitialize': ['int', ['string', 'string', 'string', 'string', 'string', 'string', intPtr]],
      'irUnInitialize': ['int', []]
  });
  
  var handleOut = ref.alloc('long');
  var a=b=c=d=e=f="";
  
  var result = randConnect.irSAInitialize(a, b, c, d, e, f, handleOut);
  console.log("Init Result: " + result);
  handle = handleOut.deref();
  console.log("Init Handle: " + handle);
  
  var uninitResult = randConnect.irUnInitialize();
  console.log("Uninit Result: " + uninitResult);
  </code></pre>
  
  <p>And here are the results I see.  Notice the uninit function seems to return successfully (result 1), so it seems to have proper communication with the DLL.</p>
  
  <pre><code>Init Result: 0
  Init Handle: 0
  Uninit Result: 1
  </code></pre>
  
  <p>Here is the relevant part of the .h for reference</p>
  
  <pre><code>typedef long IR_HANDLE;
  typedef enum {IR_FAILURE_CODE = 0, IR_SUCCESS_CODE = 1} IR_ERROR_CODE;
  
  IR_DECL_SPEC IR_ERROR_CODE irSAInitialize                   (const char *szDARouterFilePath, const char *szDADataFilePath, const char *szRouterFilePath, const char *szMapFilePath, const char *szAdminFilePath, const char *szDataFilePath, IR_HANDLE *pirHandle);
  </code></pre>
  
  <p>Also for reference, here are the snippets of C# code that works in the existing solution</p>
  
  <pre><code>[DllImport("irApiSA.dll", CallingConvention = CallingConvention.Cdecl)]
  public static extern int irSAInitialize(String a, String b, String c, String d, String e, String f, ref int handle);
  ...
  char[] temp = new char[256];
  String s = new String(temp);
  int result = irSAInitialize(s, s, s, s, s, s, ref handle);
  </code></pre>
  
  <p>I've tried many variations of using int vs long, string vs buffer, etc... no luck.  Any help or suggestions appreciated!</p>