佚名通过本文主要向大家介绍了读书究竟有何用,人生究竟有何不同,卑微究竟有何力量,霍金究竟知道什么,究竟的近义词等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: 我编写的windows服务究竟有何问题?请不栗次教!
描述:
解决方案1:
描述:
服务可以成功运行
服务管理器里面可以启动,停止 服务。
void ServiceMain(int argc, char** argv)
{
//加了我自己的函数 主要是RegSetValueEx 写注册表
}
调试发现(RegSetValueEx函数执行没有问题。可是注册表就是不变!!
而且这段修改注册表代码我独测试也是没有问题的。
现在问题就在与为什么放在服务里面,他工作就异常了呢?
解决方案1:
HKEY_CURRENT_USER是用户相关的,而服务一般是属于System用户组的,所以不能访问
要访问需要模仿一个用户令牌
LZ可以通过访问访问HKEY_USERS下用户对应的SID下面的实现,HKEY_CURRENT_USER是其中的映射罢了。
要获得用户态的SID,可以从Explorer.exe模拟一个Token过来
解决方案3:
/*---------------------------------------------------------------------
Author:Tr0j4n
Blog:http://hi.baidu.com/tr0j4n
Function:Simulate Windows Sysinternals's PsGetSID.exe
Get the SID of Current User
-----------------------------------------------------------------------*/
#include <windows.h>
#include <Sddl.h>
#include <iostream>
using namespace std;
int main(void)
{
PSID pSID(NULL); //It is the protagonist
HANDLE hCurrent(NULL),hToken(NULL);
PTOKEN_USER pTokenUser(NULL);
DWORD dwReturnLength(0),dwLength(0);
SID_IDENTIFIER_AUTHORITY SNSA =
SECURITY_NULL_SID_AUTHORITY;
//Choose which process?As you will
hCurrent=GetCurrentProcess();
if(!hCurrent)
{
cout<<"Can't Open Process"<<endl;
goto exit;
}
//Get the Token
if(!OpenProcessToken(hCurrent,TOKEN_QUERY,&hToken))
{
cout<<"Can't Get Own Token"<<endl;
goto exit;
}
//Get the token information,for the length
if(!GetTokenInformation(hToken,
TokenUser,
(LPVOID)pTokenUser,
0,
&dwReturnLength))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
goto exit;
pTokenUser= (PTOKEN_USER)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, dwReturnLength);
if (pTokenUser == NULL)
goto exit;
}
//The real get information .Tr0j4n ^_^
if(!GetTokenInformation(hToken,
TokenUser,
(LPVOID)pTokenUser,
dwReturnLength,
&dwReturnLength))
{
cout<<"Get Token of Explorer.exe Failed!"<<endl;
goto exit;
}
dwLength=GetLengthSid(pTokenUser->User.Sid);
//Allocate a Null SID
if(!AllocateAndInitializeSid(&SNSA, 1, SECURITY_NULL_RID,0,0,0,0,0,0,0,&pSID))
{
cout<<"Allocate SID Failed!"<<endl;
goto exit;
}
//Next to Copy SID
if (!CopySid(dwLength, pSID, pTokenUser->User.Sid))
{
cout<<"Copy SID Failed"<<endl;
HeapFree(GetProcessHeap(), 0,pTokenUser);
goto exit;
}
LPTSTR lpSidString(NULL);
/*Attention here ! You can choose ANSI or Unicode .if Character Union is ANIS ,
change lpSidString to LPSTR,and use ConvertSidToStringSidA
then 'COUT' it,not 'WCOUT'
*/
BOOL bRet=ConvertSidToStringSid(pSID,&lpSidString);
if(bRet)
wcout<<lpSidString<<endl;
exit:
if(pSID)
FreeSid(pSID);
return 0;
}
下载个例子研究一下
解决方案4:服务里面不会默认加载HKEY_CURRENT_USER
解决方案5: 系统中的每个用户都有自己的HKEY_CURRENT_USER主键,服务程序默认是system用户身份,与登录用户不同,解决方法大概有三种:
1、安装服务指定用户名和密码;
2、访问HKEY_USERS下用户对应的主键;
3、创建一个用户身份的进程