创建一个数组并设置值

时间:2014-09-27 04:09:09

标签: firefox-addon jsctypes

我试图创建一个数组:

var rgsFilenamesType = ctypes.ArrayType(ctypes.char.ptr);
var rgsFilenames = new rgsFilenamesType(1);
rgsFilenames[0] = 'rawr'; //OS.Path.join(OS.Constants.Path.profileDir, 'parent.lock');

然而它一直告诉我:

Exception: expected type pointer, got "rawr"

我的目标是将它传递给这个功能:

dwError = RmRegisterResources(dwSession, rgsFilenames.length, rgsFilenames, 0, null, 0, null);

声明此功能:

/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa373663%28v=vs.85%29.aspx
* DWORD WINAPI RmRegisterResources(
*   __in_      DWORD dwSessionHandle,
*   __in_      UINT nFiles,
*   __in_opt_  LPCWSTR rgsFilenames[ ],
*   __in_      UINT nApplications,
*   __in_opt_  RM_UNIQUE_PROCESS rgApplications[ ],
*   __in_      UINT nServices,
*   __in_opt_  LPCWSTR rgsServiceNames[ ]
* );
*/
var RmRegisterResources = lib.rstrtmgr.declare('RmRegisterResources', ctypes.winapi_abi, ctypes.unsigned_long, // DWORD
    ctypes.uint32_t, // DWORD
    ctypes.unsigned_int, // UINT
    ctypes.ArrayType(ctypes.char.ptr, 1), // LPCWSTR
    ctypes.unsigned_int, // UINT
    ctypes.ArrayType(struct_RM_UNIQUE_PROCESS, 1), // RM_UNIQUE_PROCESS
    ctypes.unsigned_int, // UINT
    ctypes.ArrayType(ctypes.char.ptr, 1) // LPCWSTR
);

2 个答案:

答案 0 :(得分:0)

我没时间检查,但是" rawr"作业应该是

rgsFilenames[0] = ctypes.char.array()("rawr");

答案 1 :(得分:0)

谢谢@paa man。 @ John-Galt向我展示了如何让它发挥作用。

他这样做是为了制作阵列:

let strings = ['rawr', 'blah'].map(s => ctypes.char.array()(s));
let a = ctypes.char.ptr.array(strings.length)();
strings.forEach((s, i) => { a[i] = s; });

然后在函数声明中我们将ctypes.ArrayType(ctypes.char.ptr, 1)更改为ctypes.char.ptr.array()

它有效! :)

我很难用自定义类型做同样的事情。

就像我将arg声明从ctypes.ArrayType(struct_RM_UNIQUE_PROCESS, 1)更改为struct_RM_UNIQUE_PROCESS.ptr.array()但是我不知道如何创建一个填充了这些元素的数组。我试过这个:

let rgApplicationsNonCData = struct_RM_UNIQUE_PROCESS.array(2)();
rgApplicationsNonCData[0].dwProcessId = 123;
rgApplicationsNonCData[0].ProcessStartTime = BLAHOBJECT;

rgApplicationsNonCData[1].dwProcessId = 123;
rgApplicationsNonCData[1].ProcessStartTime = BLAHOBJECT;
console.log('rgApplicationsNonCData:', rgApplicationsNonCData)

let rgApplications = struct_RM_UNIQUE_PROCESS.ptr.array(rgApplicationsNonCData.length)();
console.log('rgApplications:', rgApplications)

它没有抛出任何错误,但这是正确的方法吗?如何检查其内容?

创建RM的实际代码UNIQUE_PROCESSS数组:

  let rgApplicationsNonCData = struct_RM_UNIQUE_PROCESS.array(2)();
  rgApplicationsNonCData[0].dwProcessId = 123;
  rgApplicationsNonCData[0].ProcessStartTime = new struct_FILETIME();
    rgApplicationsNonCData[0].ProcessStartTime.dwLowDateTime = 111;
    rgApplicationsNonCData[0].ProcessStartTime.dwHighDateTime = 222;

  rgApplicationsNonCData[1].dwProcessId = 123;
  rgApplicationsNonCData[1].ProcessStartTime = new struct_FILETIME();
    rgApplicationsNonCData[1].ProcessStartTime.dwLowDateTime = 111;
    rgApplicationsNonCData[1].ProcessStartTime.dwHighDateTime = 222;

  console.log('rgApplicationsNonCData:', rgApplicationsNonCData);

  let rgApplications = struct_RM_UNIQUE_PROCESS.ptr.array(rgApplicationsNonCData.length)();
  console.log('rgApplications:', rgApplications)

console.log('rgApplications:', rgApplications)console.log('rgApplicationsNonCData:', rgApplicationsNonCData);CData {length:2}

外不显示任何内容