Firefox 扩展中的 SQLite3 错误

SQLite3 Error in Firefox Extension

我正在编写一个 Firefox 扩展,它将获取当前浏览器的句柄并将其写入 sqlite 数据库。我的整个代码是:

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");

let file = FileUtils.getFile("ProfD", ["testing.sqlite"]);
let dbConn = Services.storage.openDatabase(file);

var browserWindow = Services.wm.getMostRecentWindow('navigator:browser');
if (!browserWindow) {
    throw new Error('No browser window found');
}

var baseWindow = browserWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIWebNavigation)
                              .QueryInterface(Ci.nsIDocShellTreeItem)
                              .treeOwner
                              .QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIBaseWindow);

var hwndString = baseWindow.nativeHandle;

Components.utils.import('resource://gre/modules/ctypes.jsm');

var user32 = ctypes.open('user32.dll');

/* http://msdn.microsoft.com/en-us/library/ms633539%28v=vs.85%29.aspx
 * BOOL WINAPI SetForegroundWindow(
 *   __in_ HWND hWnd
 * );
 */
var SetForegroundWindow = user32.declare('SetForegroundWindow', ctypes.winapi_abi,
    ctypes.bool, // return BOOL
    ctypes.voidptr_t // HWND
);

var hwnd 
hwnd = ctypes.voidptr_t(ctypes.UInt64(hwndString));
var rez_SetForegroundWindow = SetForegroundWindow(hwnd);

console.log('hwnd: ', hwnd.toString());
dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwnd +")");
//dbConn.executeSimpleSQL("INSERT INTO tblHandles(handle) VALUES("+ hwnd+")");

user32.close();

我遇到错误:

NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]

行:

dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwnd +")");

createStatement 和 executeSimpleSQL 都抛出相同的错误,我不知道为什么。

我认为该语句需要一个字符串,您正在向它传递一个对象,特别是您正在传递它 CData { },这就是行 hwnd = ctypes.voidptr_t(ctypes.UInt64(hwndString)); 所做的。所以不要 dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwnd +")"); 这样做:

dbConn.createStatement("INSERT INTO tblHandles(handle) VALUES("+ hwndString +")");

jsctypes 也很酷,但我认为只有在绝对必要时才使用会更好,因为我认为性能稍差。所以没有必要用它来聚焦window。而只是做 browserWindow..focus();

注意: 我还看到您将 Components.utils 与 Ci 混合在一起,您在定义 Ci 是什么吗?如果你是为什么不也在那里定义 Cu 并做 Cu 而不是 Components.utils.