?? udpclient.cpp
字號:
}
*/
bResult=true;
}
else
{
printf("AdjustTokenPrivileges error\n");
}
CloseHandle(hToken);
return bResult;
}
int ServiceStart()
{
WSADATA wsaData;
SOCKET sock;
int iResult;
int Svchostidarray[SVCHOSTMAX];
int i;
int j;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
if(!LocateNtdllEntry())
return (-1);
/*
sprintf(Logbuffer,"ZwQuerySystemInformation address: %d.\n",ZwQuerySystemInformation);
logprintf(Logbuffer);
*/
if(!EnablePrivilege (true))
{
sprintf(Logbuffer,"EnablePrivilege wrong\n");
logprintf(Logbuffer);
return(-1);
}
j=GetDNSProcessId(Svchostidarray,SVCHOSTMAX); //j is the count of svchost.
if (j>0)
{
for (i=0;i<j;i++)
{
sock = GetSocketFromId(Svchostidarray[i]);
if( sock!=NULL)
{
logprintf("GetSocketFromId succeed get a sock.\n");
//break;
}
}
}
if (sock==NULL)
{
logprintf("GetSocketFromId wrong can't get a sock.\n");
return(-1);
}
//getchar();
//WSACleanup();
return 0;
}
void WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv)
{
DWORD status = 0;
DWORD specificError;
debugloginit();
logprintf("service_main begin\n");
MyServiceStatus.dwServiceType = SERVICE_WIN32;
MyServiceStatus.dwCurrentState = SERVICE_START_PENDING;
MyServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
MyServiceStatus.dwWin32ExitCode = 0;
MyServiceStatus.dwServiceSpecificExitCode = 0;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
MyServiceStatusHandle = RegisterServiceCtrlHandler(SERVICENAME, MyServiceCtrlHandler);
if (MyServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
logprintf("RegisterServiceCtrlHandler error\n");
return;
}
/*
if (status != NO_ERROR)
{
MyServiceStatus.dwCurrentState = SERVICE_START_PENDING;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
MyServiceStatus.dwWin32ExitCode = status;
SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus);
//return;
}
*/
// Initialization complete - report running status.
MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
{
status = GetLastError();
sprintf(Logbuffer,"service_main SetServiceStatus error %d\n",status);
logprintf(Logbuffer);
}
// This is where the service does its work.
ServiceStart();
debuglogClose();
return;
}
VOID WINAPI MyServiceCtrlHandler (DWORD Opcode)
{
DWORD status;
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
MyServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
MyServiceStatus.dwWin32ExitCode = 0;
MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (MyServiceStatusHandle,
&MyServiceStatus))
{
status = GetLastError();
printf("MyServiceCtrlHandler SERVICE_CONTROL_STOP SetServiceStatus error %d\n",status);
}
return;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
// Send current status.
if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
{
//status = GetLastError();
printf("MyServiceCtrlHandler SetServiceStatus error\n");
}
return;
}
BOOL LaunchServices()
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
TCHAR szPath[MAX_PATH];
DWORD dwErrorCode;
bool res;
res=true;
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
{
printf("GetModuleFileName failed (%d)\n", GetLastError());
return FALSE;
}
schSCManager = OpenSCManager(NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (schSCManager == NULL)
{
printf("OpenSCManager error.\n");
return FALSE;
}
schService = CreateService(
schSCManager, // SCManager database
SERVICENAME, // name of service
SERVICENAME, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService==NULL)
{
dwErrorCode=GetLastError();
if (dwErrorCode!=ERROR_SERVICE_EXISTS)
{
printf("CreateService error %d\n",dwErrorCode);
goto Exit;
}
else
{
schService = OpenService(schSCManager, SERVICENAME, SERVICE_ALL_ACCESS);
if (schService == NULL)
{
printf("OpenService error %d\n",GetLastError());
res=false;
goto Exit;
}
else
{
printf("service exit. open service succeed.\n");
}
}
}
else
{
printf("CreateService succeed.\n");
}
if(!StartService(schService, 0, NULL) )
{
printf("fail to StarxtService");
res=false;
}
else
{
printf("Service has been started.\n");
}
Exit:
if (schService!=NULL)
{
CloseServiceHandle(schService);
}
if (schSCManager!=NULL)
{
CloseServiceHandle(schSCManager);
}
return res;
}
int SendMydata(SOCKET sock)
{
char testbuf[255];
sockaddr_in RecvAddr;
int nbyteSend=0;
//Change there value...
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(5555);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
strncpy(testbuf,"test",sizeof(testbuf));
nbyteSend=sendto(sock,
testbuf,
sizeof(testbuf),
0,
(SOCKADDR *) &RecvAddr,
sizeof(RecvAddr));
if(SOCKET_ERROR == nbyteSend)
{
sprintf(Logbuffer,"sendto wrong:%d\n", WSAGetLastError());
logprintf(Logbuffer);
nbyteSend=-1;
}
else
{
logprintf("[^^^^^^^^^^^^^^]send ok... Have fun, right? ^_^\n");
}
return(nbyteSend);
}
void main()
{
//debugloginit();
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ SERVICENAME , service_main},
{ NULL, NULL}
};
if (!StartServiceCtrlDispatcher( DispatchTable))
{
//printf("StartServiceCtrlDispatcher error %d\n",GetLastError());
}
LaunchServices();
//debuglogClose();
return;
}
/*
typedef enum _SYSTEM_INFORMATION_CLASS {
SystemBasicInformation,
SystemProcessorInformation, // obsolete...delete
SystemPerformanceInformation,
SystemTimeOfDayInformation,
SystemPathInformation,
SystemProcessInformation,
SystemCallCountInformation,
SystemDeviceInformation,
SystemProcessorPerformanceInformation,
SystemFlagsInformation,
SystemCallTimeInformation,
SystemModuleInformation,
SystemLocksInformation,
SystemStackTraceInformation,
SystemPagedPoolInformation,
SystemNonPagedPoolInformation,
SystemHandleInformation,
SystemObjectInformation,
SystemPageFileInformation,
SystemVdmInstemulInformation,
SystemVdmBopInformation,
SystemFileCacheInformation,
SystemPoolTagInformation,
SystemInterruptInformation,
SystemDpcBehaviorInformation,
SystemFullMemoryInformation,
SystemLoadGdiDriverInformation,
SystemUnloadGdiDriverInformation,
SystemTimeAdjustmentInformation,
SystemSummaryMemoryInformation,
SystemMirrorMemoryInformation,
SystemPerformanceTraceInformation,
SystemObsolete0,
SystemExceptionInformation,
SystemCrashDumpStateInformation,
SystemKernelDebuggerInformation,
SystemContextSwitchInformation,
SystemRegistryQuotaInformation,
SystemExtendServiceTableInformation,
SystemPrioritySeperation,
SystemVerifierAddDriverInformation,
SystemVerifierRemoveDriverInformation,
SystemProcessorIdleInformation,
SystemLegacyDriverInformation,
SystemCurrentTimeZoneInformation,
SystemLookasideInformation,
SystemTimeSlipNotification,
SystemSessionCreate,
SystemSessionDetach,
SystemSessionInformation,
SystemRangeStartInformation,
SystemVerifierInformation,
SystemVerifierThunkExtend,
SystemSessionProcessInformation,
SystemLoadGdiDriverInSystemSpace,
SystemNumaProcessorMap,
SystemPrefetcherInformation,
SystemExtendedProcessInformation,
SystemRecommendedSharedDataAlignment,
SystemComPlusPackage,
SystemNumaAvailableMemory,
SystemProcessorPowerInformation,
SystemEmulationBasicInformation,
SystemEmulationProcessorInformation,
SystemExtendedHandleInformation,
SystemLostDelayedWriteInformation,
SystemBigPoolInformation,
SystemSessionPoolTagInformation,
SystemSessionMappedViewInformation,
SystemHotpatchInformation,
SystemObjectSecurityMode,
SystemWatchdogTimerHandler,
SystemWatchdogTimerInformation,
SystemLogicalProcessorInformation,
SystemWow64SharedInformation,
SystemRegisterFirmwareTableInformationHandler,
SystemFirmwareTableInformation,
SystemModuleInformationEx,
SystemVerifierTriageInformation,
SystemSuperfetchInformation,
SystemMemoryListInformation,
SystemFileCacheInformationEx,
MaxSystemInfoClass // MaxSystemInfoClass should always be the last enum
} SYSTEM_INFORMATION_CLASS;
//
// System Information Structures.
//
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -