调用Dropbox的命名管道以获取状态:错误:读取EPIPE

时间:2016-12-22 16:04:09

标签: python node.js windows pipe

我有一个Python脚本,用于检查我尝试移植到nodejs的本地Windows Dropbox应用程序同步状态(在Electron中使用)。

问题是我得到了:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: read EPIPE
    at exports._errnoException (util.js:907:11)
    at Pipe.onread (net.js:557:26)

当我尝试我的节点脚本时。

这是python脚本:

import win32pipe, win32ts, win32api, pywintypes, struct, sys

yourUsername = "YourUsername"

def dropbox_path_status(pathname):
        return ['dropbox not running','not in dropbox','up to date','syncronising','sync problem'][dropbox_path_status_code(pathname)+1]

def dropbox_path_status_code(pathname):
        processid = win32api.GetCurrentProcessId()
        threadid = win32api.GetCurrentThreadId()
        request_type = 1
        wtf = 0x3048302
        pipename = r'\\.\pipe\DropboxPipe_' +  str(win32ts.ProcessIdToSessionId(processid))
        request = (struct.pack('LLLL', wtf, processid, threadid, request_type) + pathname.encode('utf-16le') + (chr(0)*540))[0:540]
        try:
                response = win32pipe.CallNamedPipe(pipename, request, 16382, 1000)
        except pywintypes.error, err:
                if err[0] == 2:
                        return -1
                else:
                        raise
        else:
                return int(response[4:-1])

print dropbox_path_status("C:\Users\"+yourUsername+"\Dropbox")

这是我的节点脚本

var net = require('net');
var ffi = require('ffi');
var ref = require('ref');
var BufferStream = require('node-bufferstream')

var PIPE_PATH = "\\\\.\\pipe\\DropboxPipe_1"
var UserName = "YourUsername"
var L = console.log;


var SessionId = ref.alloc("uint");

var kernel32 = new ffi.Library("kernel32",{
    GetLastError:['string',[]],
    GetCurrentProcessId: ['int',[]] ,
    GetCurrentThreadId: ['int',[]],
    ProcessIdToSessionId: ['bool',['int','uint *']],
    CallNamedPipeA:['bool',[]]

});



var RequestInfo = 0x3048302;
var ProcessId = kernel32.GetCurrentProcessId();
var ThreadId = kernel32.GetCurrentThreadId();
var RequestType =1;
var dropbox = ref.types.void;

if(!kernel32.ProcessIdToSessionId(ProcessId,SessionId )){
    console.log(kernel32.GetLastError());
}
else{
    console.log(SessionId);
    SessionId.type = ref.types.int;
    SessionIdInt = SessionId.deref();
    console.log(SessionIdInt);
}




var TestBufferIn = new Buffer(16+724);
TestBufferIn.writeUInt32LE(RequestInfo, 0);
TestBufferIn.writeUInt32LE(ProcessId, 4);
TestBufferIn.writeUInt32LE(ThreadId, 8);
TestBufferIn.writeUInt32LE(RequestType, 12);
//TestBufferIn.writeUInt32LE(1234567, 600);
console.log(TestBufferIn.length)
var mypath = "C:/Users/"+UserName+"/Dropbox/"

TestBufferIn.write(mypath,16,162,"utf-16le")
stream = new BufferStream(TestBufferIn);
var L = console.log;



var client = net.createConnection({ path: '\\\\.\\pipe\\DropboxPipe_1'}, function () { console.log('connected');

client.write(TestBufferIn, ()=>{
        console.log("write callback")
    });                                                                                     
})

client.on('data', function(data) {
    L('Client: on data:', data.toString());
    client.end('Thanks!');
});

client.on('end', function() {
    L('Client: on end');
})

知道错误出现的原因吗? 的修改
当我用字符串(而不是缓冲区)写时,我得到同样的错误。

var client = net.createConnection({ path: '\\\\.\\pipe\\DropboxPipe_1'}, function () { console.log('connected');
        client.write("hello", ()=>{
        console.log("write callback"); //this is written in console 
    });                                                                               

});

0 个答案:

没有答案