Sending Control Requests to a Service

博客介绍了向服务发送控制请求的示例,使用ControlService函数向运行中的服务发送控制值,不同控制值对服务对象的访问级别要求不同,如发送SERVICE_CONTROL_STOP控制码需SERVICE_STOP访问权限,ControlService返回时,SERVICE_STATUS结构包含服务的最新状态信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sending Control Requests to a Service

The following example uses the ControlService function to send a control value to a running service. Different control values require different levels of access to the service object. For example, a service object handle must have SERVICE_STOP access to send the SERVICE_CONTROL_STOP control code. When ControlService returns, a SERVICE_STATUS structure contains the latest status information for the service.

BOOL ControlSampleService(DWORD fdwControl) 
{ 
    SERVICE_STATUS ssStatus; 
    DWORD fdwAccess; 
    DWORD dwStartTickCount, dwWaitTime;
 
    // The required service object access depends on the control. 
 
    switch (fdwControl) 
    { 
        case SERVICE_CONTROL_STOP: 
            fdwAccess = SERVICE_STOP; 
            break; 
 
        case SERVICE_CONTROL_PAUSE: 
        case SERVICE_CONTROL_CONTINUE: 
            fdwAccess = SERVICE_PAUSE_CONTINUE; 
            break; 
 
        case SERVICE_CONTROL_INTERROGATE: 
            fdwAccess = SERVICE_INTERROGATE; 
            break; 
 
        default: 
            fdwAccess = SERVICE_INTERROGATE; 
    } 
 
    // Open a handle to the service. 
 
    schService = OpenService( 
        schSCManager,        // SCManager database 
        TEXT("Sample_Srv"),  // name of service 
        fdwAccess);          // specify access 
    if (schService == NULL) 
    {
        printf("OpenService failed (%d)/n", GetLastError()); 
        return FALSE;
    }
 
    // Send a control value to the service. 
 
    if (! ControlService( 
            schService,   // handle to service 
            fdwControl,   // control value to send 
            &ssStatus) )  // address of status info 
    {
        printf("ControlService failed (%d)/n", GetLastError()); 
        return FALSE;
    }
 
    // Print the service status. 
 
    printf("/nStatus of Sample_Srv: /n");
    printf("  Service Type: 0x%x/n", ssStatus.dwServiceType); 
    printf("  Current State: 0x%x/n", ssStatus.dwCurrentState); 
    printf("  Controls Accepted: 0x%x/n", 
        ssStatus.dwControlsAccepted); 
    printf("  Exit Code: %d/n", ssStatus.dwWin32ExitCode); 
    printf("  Service Specific Exit Code: %d/n", 
        ssStatus.dwServiceSpecificExitCode); 
    printf("  Check Point: %d/n", ssStatus.dwCheckPoint); 
    printf("  Wait Hint: %d/n", ssStatus.dwWaitHint); 
 
    return TRUE; 
}
Strophe.Connection = function (service) { /* The path to the httpbind service. */ this.service = service; /* The connected JID. */ this.jid = ""; /* request id for body tags */ this.rid = Math.floor(Math.random() * 4294967295); /* The current session ID. */ this.sid = null; this.streamId = null; // SASL this.do_session = false; this.do_bind = false; // handler lists this.timedHandlers = []; this.handlers = []; this.removeTimeds = []; this.removeHandlers = []; this.addTimeds = []; this.addHandlers = []; this._idleTimeout = null; this._disconnectTimeout = null; this.authenticated = false; this.disconnecting = false; this.connected = false; this.errors = 0; this.paused = false; // default BOSH window this.window = 5; this._data = []; this._requests = []; this._uniqueId = Math.round(Math.random() * 10000); this._sasl_success_handler = null; this._sasl_failure_handler = null; this._sasl_challenge_handler = null; // setup onIdle callback every 1/10th of a second this._idleTimeout = setTimeout(this._onIdle.bind(this), 100); }; Strophe.Connection.prototype = { /** Function: reset * Reset the connection. * * This function should be called after a connection is disconnected * before that connection is reused. */ reset: function () { this.rid = Math.floor(Math.random() * 4294967295); this.sid = null; this.streamId = null; // SASL this.do_session = false; this.do_bind = false; // handler lists this.timedHandlers = []; this.handlers = []; this.removeTimeds = []; this.removeHandlers = []; this.addTimeds = []; this.addHandlers = []; this.authenticated = false; this.disconnecting = false; this.connected = false; this.errors = 0; this._requests = []; this._uniqueId = Math.round(Math.random()*10000); }, /** Function: pause * Pause the request manager. * * This will prevent Strophe from sending any more requests to the * server. This is very useful for temporarily pausing while a lot * of send() calls are happening quickly. This causes Strophe to * send the data in a single request, saving many request trips. */ pause: function () { this.paused = true; }, /** Function: resume * Resume the request manager. * * This resumes after pause() has been called. */ resume: function () { this.paused = false; }, /** Function: getUniqueId * Generate a unique ID for use in <iq/> elements. * * All <iq/> stanzas are required to have unique id attributes. This * function makes creating these easy. Each connection instance has * a counter which starts from zero, and the value of this counter * plus a colon followed by the suffix becomes the unique id. If no * suffix is supplied, the counter is used as the unique id. * * Suffixes are used to make debugging easier when reading the stream * data, and their use is recommended. The counter resets to 0 for * every new connection for the same reason. For connections to the * same server that authenticate the same way, all the ids should be * the same, which makes it easy to see changes. This is useful for * automated testing as well. * * Parameters: * (String) suffix - A optional suffix to append to the id. * * Returns: * A unique string to be used for the id attribute. */ getUniqueId: function (suffix) { if (typeof(suffix) == "string" || typeof(suffix) == "number") { return ++this._uniqueId + ":" + suffix; } else { return ++this._uniqueId + ""; } }, /** Function: connect * Starts the connection process. * * As the connection process proceeds, the user supplied callback will * be triggered multiple times with status updates. The callback * should take two arguments - the status code and the error condition. * * The status code will be one of the values in the Strophe.Status * constants. The error condition will be one of the conditions * defined in RFC 3920 or the condition 'strophe-parsererror'. * * Please see XEP 124 for a more detailed explanation of the optional * parameters below. * * Parameters: * (String) jid - The user's JID. This may be a bare JID, * or a full JID. If a node is not supplied, SASL ANONYMOUS * authentication will be attempted. * (String) pass - The user's password. * (Function) callback The connect callback function. * (Integer) wait - The optional HTTPBIND wait value. This is the * time the server will wait before returning an empty result for * a request. The default setting of 60 seconds is recommended. * Other settings will require tweaks to the Strophe.TIMEOUT value. * (Integer) hold - The optional HTTPBIND hold value. This is the * number of connections the server will hold at one time. This * should almost always be set to 1 (the default). * (Integer) wind - The optional HTTBIND window value. This is the * allowed range of request ids that are valid. The default is 5. */ connect: function (jid, pass, callback, wait, hold, wind) { this.jid = jid; this.pass = pass; this.connect_callback = callback; this.disconnecting = false; this.connected = false; this.authenticated = false; this.errors = 0; if (!wait) wait = 60; if (!hold) hold = 1; if (wind) this.window = wind; // parse jid for domain and resource this.domain = Strophe.getDomainFromJid(this.jid); // build the body tag var body = this._buildBody().attrs({ to: this.domain, "xml:lang": "en", wait: wait, hold: hold, window: this.window, content: "text/xml; charset=utf-8", ver: "1.6", "xmpp:version": "1.0", "xmlns:xmpp": Strophe.NS.BOSH }); this.connect_callback(Strophe.Status.CONNECTING, null); this._requests.push( new Strophe.Request(body.toString(), this._onRequestStateChange.bind(this) .prependArg(this._connect_cb.bind(this)), body.tree().getAttribute("rid"))); this._throttledRequestHandler(); },
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值