2015-06-22 46 views
1

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

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抛出了同样的错误,我不知道为什么。

回答

1

我认为声明期望一个字符串,你传递它一个对象,具体而言,你传递它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是很酷,但我认为这将是更好的只有在绝对必要使用,因为我觉得PERF略差。所以不需要使用它来关注窗口。而只是做browserWindow..focus();

注意: 另外我看你混合Components.utils与Ci你是什么Ci定义?如果你是为什么不直接定义Cu,那么做Cu而不是Components.utils。

+0

这工作完美切换到hwndString。 – Resistance

+0

酷@TravisStauffer,但确保当你从数据库中读取,以确保你做'ctypes.voidptr_t(ctypes.UInt64'从表 – Noitidart

+0

读取值我使用ac#程序来读取存储的句柄,所以我转换它回到那里十六进制它现在工作完美 – Resistance