乙個簡單的Windows服務程式

2021-09-23 18:42:00 字數 1084 閱讀 7476

service_status servicestatus;

service_status_handle hservicestatushandle;

handle gettokenbyid(int pid)

void winapi servicehandler(dword fdwcontrol)

; if (!setservicestatus(hservicestatushandle, &servicestatus)) }

void winapi service_main(int argc, char** ar**)

if (!setservicestatus(hservicestatushandle, &servicestatus))

ofile << "service starting" << std::endl;

std::thread service_thread([&]() ;

si.lpdesktop = szdesl;

si.dwflags = startf_useshowwindow;

si.wshowwindow = sw_show;

process_information pi;

}} });

service_thread.detach();

}int main()

上面直接貼了**,方便以後參照。翻看之前的記錄,也有過相關的記錄,但是太複雜,不相干的**太多。

這裡的關鍵是createprocessasuser,這個api能以某個使用者的身份啟動乙個程序。為什麼這麼做,因為服務程序屬於system使用者,system使用者不能顯示ui。所以我們要把這個程序以能夠顯示ui的使用者啟動起來。

第乙個引數token就是代表這個有使用者的,所有的程序都歸屬於乙個使用者,比方服務程序都屬於system,管理員使用者啟動的程式屬於administrator,user使用者啟動的程序預設屬於user。這些不通過的角色都有不同的許可權,作用也就體現在程序能夠操作的許可權上,比如登錄檔,磁碟等等。

上面的例子我去的token就是我桌面程序的token,對應的就是我當前登入的使用者,因為我是用administrator登入的,所以這個程序就是由administrator啟動的。

建立乙個簡單的windows 服務

此服務主要用於開啟服務後定時往資料庫中插入資料,直到關閉服務 1.建立乙個windows service 專案 2.新增乙個service業務功能,用乙個計時器 timer 定時往資料庫中插入記錄 此處需要注意,在initializecomponent方法中,需要將 private system.w...

寫乙個Windows服務

做了兩個和windows服務有關的專案了,最開始的時候沒做過,不懂,現在明白了許多。需要注意的是,如果不想登入什麼的,最後在新增安裝程式的那裡選擇那個字長的右鍵屬性,把啟動方式改為local system 新建完畢之後,是這樣的,雙擊service1然後在中間單擊右鍵,新增安裝程式,會出來乙個pro...

windows下的乙個C socket伺服器

include 1 以後會用這種方式對特定 進行注釋 include include include include include include include using namespace std pragma comment lib,ws2 32.lib 2 1.include 大家會發現...