title: NodeJS: node-ffi, ref-struct, ref-array
body: <p>I'm writing PKCS11 cryptoki wrapper using nodejs plugins <code>ffi</code>, <code>ref</code>, <code>ref-struct</code> and <code>ref-array</code>. I have this code.</p>
  
  <pre><code>var hSession = this.session.handle;
  var hObject = this.handle;
  var $label = new (arrayType(cki.CK_UTF8CHAR))(80);
  
  var template = new (arrayType(cki.CK_ATTRIBUTE))(1);
  template[0] = new cki.CK_ATTRIBUTE({
      type:cki.CKA_LABEL, 
      pValue: $label.ref(), 
      ulValueLen: 80}) 
  var res = this.cki.C_GetAttributeValue(hSession, hObject, template.ref(), 1);
  if (res == cki.CKR_OK) {
      console.log("Ok");
  }
  else{
      console.log("Wrong "+res);
  }
  </code></pre>
  
  <p>When I call this function I have wrong results (CKR_ARGUMENTS_BAD, CKR_ATTRIBUTE_TYPE_INVALID). 
  Please, help me to find error.</p>
  
  <p>FFI function</p>
  
  <pre><code>"C_GetAttributeValue":[t.CK_RV, [t.CK_SESSION_HANDLE, t.CK_OBJECT_HANDLE, t.CK_ATTRIBUTE_PTR, t.CK_ULONG]],
  </code></pre>
  
  <p>Types</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>/* CK_ATTRIBUTE is a structure that includes the type, length
   * and value of an attribute */
  t.CK_ATTRIBUTE = struct({
    type: t.CK_ATTRIBUTE_TYPE,
    pValue: t.CK_VOID_PTR,
  
    /* ulValueLen went from CK_USHORT to CK_ULONG for v2.0 */
    ulValueLen: t.CK_ULONG  /* in bytes */
  });</code></pre>
  </div>
  </div>
  </p>