Commonly used functions library collection

xiaoxiao2021-03-06  38

Unsigned long resolv (char * host)

{

Struct hostent * hp;

Unsigned long host_ip;

Host_ip = inet_addr (host);

IF (Host_ip == INADDR_NONE)

{

HP = gethostbyname (Host);

IF (! hp)

{

// Printf ("/ Nerror: unable to resolve hostname (% s) / n", host);

Exit (1);

}

Else

Host_ip = * (u_long *) hp-> h_addr;

}

Return (Host_ip);

}

// Check and algorithm

Unsigned Short Checksum (unsigned short * buffer, int size)

{

Unsigned long cksum = 0;

While (size> 1)

{

CKSUM = * Buffer ;

Size- = SizeOf (unsigned short);

}

size

CKSUM = * (Uchar *) BUFFER;

CKSUM = (CKSUM >> 16) (CKSUM & 0xFFF);

CKSUM = (CKSUM >> 16);

Return (Unsigned Short) (~ CKSUM);

}

//

// obtain the full path according to the window handle.

//

//

// Parameter:

// hwnd

// Window handle

// lpszfilename

// Get buffers of the file full path

// nsize

// buffer length

//

// return value

// Successfully returns true, error returns false;

//

Bool getFileNameFromhwnd (HWND HWND, LPTSTSTSZFILENAME, DWORD NSIZE)

{

Bool Bresult = false;

// Get the process ID number from the handle

DWORD DWPROCESSID;

GetWindowThreadProcessId (HWND, & DWPROCESSID);

// Get system version information

OsversionInfo OsverInfo;

Osverinfo.dwosveionsinfosize = sizeof (OsversionInfo);

IF (! getversionex (& OsverInfo))

Return False;

// When the system is NT core

IF (OSVERINFO.DWPLATFORMID == Ver_Platform_Win32_NT)

{

// Call PSAPI

Bool (WinApi * LPFENUMPROCESSMODULES)

(Handle, HModule *, DWORD, LPDWORD);

DWORD (WinApi * lpfgetmoduleFileNameex)

Handle, HModule, LPTSTR, DWORD;

// Load PSAPI.dll

Hinstance hinstlib = loadingLibrary ("psapi.dll");

IF (hinstlib == null)

Return False;

// Positioning related functions

LPFENUMPROCESSMODULES = (Bool (WinApi *)

(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcaddress (Hinstlib, "EnumProcessModules);

LpfgetModuleFileNameex = (DWORD (WinApi *)

(Handle, HMODULE, LPTSTR, DWORD) GetProcAddress

Hinstlib, "getModuleFileNameExa";

LPFENUMPROCESSMODULES & LPFGETMODULENAMEEX)

{

// Open the specified process

Handle HPROCESS;

HProcess = OpenProcess

Process_Query_information | Process_vm_read,

False, dwprocessid;

IF (HProcess)

{

// Holding module

HModule HModule;

DWORD DWNEED;

IF (LPFENUMPROCESSMODULES (HProcess,

& HModule, SizeOf (HModule), & dwneed))

{

// Get file path

IF (lpfgetmodulefilenameex (hprocess, hmodule,

LPSZFILENAME, NSIZE))

BRESULT = True;

}

// Close the handle

CloseHandle (HPROCESS);

}

}

// Release PSAPI.dll

Freelibrary (Hinstlib);

}

// When the system is 9X

Else if (OsverInfo.dwplatformID == VER_PLATFORM_WIN32_WINDOWS)

{

// Take Toolhelp

Handle (WinApi * lpfcreatesnapshot) (DWORD, DWORD);

Bool (WinApi * lpfprocess32first) (Handle, LPPROCESSENTRY32);

Bool (WinApi * lpfProcess32Next) (Handle, LPPROCESSENTRY32);

// Acquisition of related functions

LPFCREATESNAPSHOT =

(Handle (WinApi *) (DWORD, DWORD)) GetProcaddress

GetModuleHandle ("kernel32.dll"),

"CREATOOLHELP32SNAPSHOT");

LPFPROCESS32FIRST =

(Bool (WinApi *) (Handle, LPPROCESSENTRY32)) GetProcaddress

GetModuleHandle ("kernel32.dll"),

"Process32First");

lpfProcess32Next =

(Bool (WinApi *) (Handle, LPPROCESSENTRY32)) GetProcaddress

GetModuleHandle ("kernel32.dll"),

"Process32Next");

IF (! lpfcreatesnapshot ||

! lpfprocess32first ||

LPFPROCESS32NEXT)

Return False;

// Use the TOOLHELP

Handle hsnapshot;

Hsnapshot = lpfcreatesnapshot (TH32CS_SNAPPROCESS, 0);

IF (HSnapshot! = (Handle) -1)

{

// From the first beginning

Processentry32 pe; pe.dwsize = sizeof (Processentry32);

IF (LPFPRocess32First (HSnapshot, & PE)

{

Do {

/ / If it is the process of this process, the process path is removed.

IF (pe.th32processid == dwprocessid)

{

Lstrcpy (lpszfilename, pe.szexefile);

BRESULT = True;

Break;

}

} while (lpfProcess32Next (HSnapshot, & PE));

}

// Close the handle

CloseHandle (HSnapshot);

}

}

Else

Return False;

Return BRESULT;

}

/ / Get an error message according to the error ID

Void ErrorTrace (Const Char * MSG, DWORD ERROR)

{

DWord Numwritten;

IF (Error)

{

LPTSTR LPMSGBUF;

FormatMessage

FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,

NULL,

Error,

Makelangid (lang_neutral, subslang_default), // default logage

(Lptstr) & lpmsgbuf,

0,

NULL

);

// free the buffer.

LocalFree (lpmsgbuf);

}

}

// Use the pipe to obtain the command line

BOOL __FASTCALL TFORM1 :: Runcmd (ANSISTRING CMD, TSTRINGLIST * STRINGLIST)

{

TMEMORYSTREAM * MEMSTREAM = New TMEMORYSTREAM ();

Ansistring rn = "// r // n";

PROCESS_INFORMATION PROC;

Startupinfo start;

Security_attributes sa;

Long Ret;

Unsigned long lngbytesread;

Handle Hreadpipe, HWritePipe;

Char * strbuff = (char *) Malloc (256);

IF (strbuff == null)

{

Return False;

}

Sa.nlength = sizeof (security_attributes);

Sa.binherithandle = true;

Sa.lpsecurityDescriptor = null;

Ret = CreatePipe (& Hreadpipe, & HwritePipe, & Sa, 0);

IF (RET == 0)

{

// Creating a pipe failed

Return False;

}

MEMSET (& Start, 0x00, SIZEOF (StartupInfo);

Start.cb = sizeof (startupinfo);

Start.dwflags = startf_useestdhandles | startf_useshowwindow;

Start.hstdputput = hwritepipe;

Start.hstderror = hwritepipe;

Bool Retc = CreateProcess (NULL, CMD.C_STR (), NULL, NULL, TRUE, 0, 0, NULL, & Start, & Proc;

IF (! RETC)

{

Return False;

}

CloseHandle (HWRITEPIPE);

Unsigned long LEN;

MemStream-> Position = 0;

While (True)

{

MEMSET (Strbuff, 0x00, 256);

GetFileSize (Hreadpipe, & len);

Ret = readfile (Hreadpipe, strbuff, 256, & lngbytesread, null);

IF (RET == 0)

{

Break;

}

Else

{

MemStream-> Write (strbuff, lngbytesread);

}

}

CloseHandle (Proc.hprocess);

CloseHandle (Proc.hthread);

CloseHandle (HREADPIPE);

MemStream-> Position = 0;

Free (strBuff);

StringList-> LoadFromstream (Memstream);

MemStream-> clear ();

Delete Memstream;

Return True;

}

// elevate process privileges BOOL EnablePrivilege (LPCTSTR lpSystemName, LPCTSTR lpName) {HANDLE hToken; BOOL fOk = FALSE; if (OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, & hToken)) {TOKEN_PRIVILEGES tp; tp.PrivilegeCount = 1; if (LookupPrivilegeValue! (lpSystemName, lpName, & tp.Privileges [0] .Luid)) printf ( "Can not lookup privilege value./n"); tp.Privileges [0] .Attributes = SE_PRIVILEGE_ENABLED; if (AdjustTokenPrivileges (hToken, FALSE,! & TP, SIZEOF (TP), NULL, NULL) PRINTF ("Can't Adjust Privilege Value./N"); fok = (GetLastError () == Error_Success); CloseHandle (HTOKEN);} returnif;

2004-11-29 7:00 PM |

Zwell

# 回:: Common Library Collection // Connection Specifies the IP Port, Return to Socket

Int conn (Char * IP, INT P)

{

Int sockfd;

Hrm.sin_family = af_INet;

HRM.SIN_ADDR.S_ADDR = INET_ADDR (IP);

HRM.SIN_PORT = HTONS (P);

Bzero (& (Hrm.sin_zero), 8);

Sockfd = Socket (AF_INET, SOCK_STREAM, 0);

IF ((Connect (Sockfd, Struct Sockaddr *) & HRM, SIZEOF (Struct SockAddr)) <0)

{

PERROR ("Connect");

exit (0);

}

Printf ("[x] connect to:% s on port% d. / n", IP, p);

Return sockfd;

}

2004-12-03 12:44 AM |

Zwell

# 回: Commonly used library collection // Install the specified program into services

Int InstallService

Char * SVR_NAME, CHAR * DISPLAY_NAME, CHAR * EXEPATH, INT STARTTYPE

{

SC_Handle Hserve; HscManager;

Hscmanager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);

IF (hscmanager == null) return 0;

HService = CreateService (HscManager, SVR_NAME, Display_Name,

Service_all_access, // desired access

Service_win32_oen_process, // service Type

StartType, // Service_Auto_Start

Service_ERROR_NORMAL, // Error Control Type

Exepath, // service's binary

Null, // no loading Ordering Group

Null, // no tag identifier

Null, // no dependency

Null, // localsystem account

NULL); // no Password

IF (hservice == null) return 0;

ClosESERVICEHANDE (HSERVICE);

Return 1;

}

// Delete the specified service

Int AutoLoad :: DeleteServiceByName (char * svr_name)

{

SC_Handle Hserve; HscManager;

Hscmanager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);

IF (hscmanager == null) return 0;

HService = OpenService (HScManager, SVR_NAME, Service_all_Access);

IF (hservice == null) return 0;

IF (deleteService) == 0) Return 0;

ClosESERVICEHANDLE (HSERVICE) == 0) Return 0;

Return 1;

}

转载请注明原文地址:https://www.9cbs.com/read-61862.html

New Post(0)