windows win32 c++/c services服务编写sample demo

本文详细解析了Windows服务程序的开发规范,强调了与SCM交互的重要性,通过两个示例样例,阐述了如何使用C++进行基础服务编程,以及服务安装、启动和事件处理函数的实现。

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

windows的服务程序不是简单的写一个console控制台程序然后加入服务就能运行成功的,需要遵循一定的代码规范,程序需要与系统的 SCM 交互,否则就会刚执行就被系统停止

两个windows 服务程序的sample demo
the-complete-service-sample–Microsoft
使用 C++ 编写的基础 Windows 服务

可以对照以上使用windows api编写,以下简单总结windows服务程序

服务程序总体看就是一个console控制台程序,服务 install (CreateService,含使用GetModuleFileName获取的当前应用程序文件路径,可以加启动参数,路径有空格需要 引号 包起来) 到 SCM (service control manager database)后开始启动服务,每次服务开始启动,都会从main开始(无参数)执行,然后调用 StartServiceCtrlDispatcher 去开始找到服务entry point执行入口开始函数

所以一般都加个 在main中处理类似install参数,去安装或卸载服务。

服务程序是由SCM管理,main 里面调用 StartServiceCtrlDispatcher 之后,SCM 才能知道你对应的事件(Start, Stop, Pause, Continue, and Shutdown)的执行函数

### Java RPC Frameworks Overview Remote Procedure Call (RPC) allows a computer program to cause a procedure or subroutine to execute in another address space, such as on a different server or even across the internet without requiring detailed knowledge of network protocols. In Java, several popular RPC frameworks exist that facilitate this process. #### gRPC gRPC is an open-source remote procedure call system developed by Google. It uses HTTP/2 for transport and Protocol Buffers as the interface definition language. This framework supports bidirectional streaming and offers efficient serialization with strong typing[^1]. ```java // Example of defining service using protocol buffers service HelloService { rpc SayHello (HelloRequest) returns (HelloResponse); } ``` #### Apache Thrift Apache Thrift enables scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml, Delphi, and more languages. The core feature lies in its ability to generate client and server stubs from one common IDL file[^2]. ```cpp // An example thrift idl snippet struct Item { 1: i32 id, 2: string name, } service InventoryManager { void addItem(1:Item item), list<Item> getItems(), } ``` #### Dubbo Dubbo is a high-performance distributed computing RPC framework widely used within Alibaba Group. With rich extension points and pluggable architecture design patterns, it provides robust solutions for large-scale microservices applications. Key features include load balancing, failover support, cluster management, etc. ```xml <!-- Configuration sample --> <dubbo:application name="demo-provider"/> <dubbo:registry address="multicast://224.5.6.7:1234"/> <dubbo:protocol name="dubbo" port="20880"/> ``` #### Comparison Summary Table | Feature/Framework | Language Support | Transport Layer | Interface Definition | |----|-----------------|----------------------| | gRPC | Multiple | HTTP/2 | ProtoBuf | | Apache Thrift | Extensive | Custom TCP | .thrift files | | Dubbo | Primarily Java | Netty | XML configuration |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值