#include <Windows.h>
#include <iostream>
#include <strsafe.h>
#include <STDLIB.H>
using namespace std;
#pragma comment(linker, "/BASE:0x400000")
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WCHAR RouteBuffer[ 60 ] = { 0 } ;
DWORD res = GetModuleFileName ( NULL , RouteBuffer, sizeof ( RouteBuffer) ) ;
WCHAR AfterVersion[ 130 ] = { 0 } ;
HMODULE EBAddress = GetModuleHandle ( TEXT ( "NpCxyFW.exe" ) ) ;
StringCchPrintf ( AfterVersion, sizeof ( AfterVersion) / 2 ,
TEXT ( "程序全路径: %s
hInstance获取到的程序基址: %x
GetModuleHandle获取到的程序基址: %x" ) ,
RouteBuffer, hInstance, EBAddress
) ;
INT Box_1 = MessageBox ( NULL , AfterVersion, TEXT ( "测试" ) , MB_OKCANCEL) ;
return 0 ;
}
通过 GetCommandLine() 获取命令行参数
#include <Windows.h>
#include <iostream>
#include <strsafe.h>
using namespace std;
int main ( int argc, char * argv[ ] )
{
INT CmdCount = NULL ;
PWSTR * CmdLine = CommandLineToArgvW ( GetCommandLine ( ) , & CmdCount) ;
for ( size_t i = 1 ; i < CmdCount; i++ )
{
WCHAR * BeforeConversion = CmdLine[ i] ; CHAR AfterConversion[ 20 ] = { 0 } ;
int res = WideCharToMultiByte ( CP_UTF8, NULL , BeforeConversion, - 1 , AfterConversion, sizeof ( AfterConversion) , NULL , NULL ) ;
cout << "参数 " << i << ": " << AfterConversion << endl;
}
return 0 ;
}
#include <Windows.h>
#include <iostream>
#include <wcschr.h>
#include <strsafe.h>
using namespace std;
int main ( int argc, char * argv[ ] )
{
PTSTR PEnvBlock = GetEnvironmentStrings ( ) ;
TCHAR SzName[ MAX_PATH] ; TCHAR SzValue[ MAX_PATH] ;
PTSTR PszCurrent = PEnvBlock;
HRESULT hr = S_OK; PCTSTR PszPos = NULL ; int current = 0 ;
while ( PszCurrent != NULL ) {
if ( * PszCurrent != TEXT ( '=' ) ) {
PszPos = _tcschr ( PszCurrent, TEXT ( '=' ) ) ; PszPos++ ;
size_t cbNameLength = ( size_t) PszPos - ( size_t) PszCurrent - sizeof ( TCHAR) ;
hr = StringCbCopyN ( SzName, MAX_PATH, PszCurrent, cbNameLength) ;
if ( FAILED ( hr) ) break ;
hr = StringCchCopyN ( SzValue, MAX_PATH, PszPos, _tcslen ( PszPos) + 1 ) ;
if ( SUCCEEDED ( hr) )
{
_tprintf ( TEXT ( "[%u] %s=%s
" ) , current, SzName, SzValue) ;
}
else
{
if ( hr == STRSAFE_E_INSUFFICIENT_BUFFER)
_tprintf ( TEXT ( "[%u] %s=%s...
" ) , current, SzName, SzValue) ;
else
_tprintf ( TEXT ( "[%u] %s=???
" ) , current, SzName) ; break ;
}
}
else {
_tprintf ( TEXT ( "[%u] %s
" ) , current, PszCurrent) ;
}
current++ ;
while ( * PszCurrent != TEXT ( ' ' ) ) PszCurrent++ ;
PszCurrent++ ;
if ( * PszCurrent == TEXT ( ' ' ) ) break ;
} ;
FreeEnvironmentStrings ( PEnvBlock) ;
return 0 ;
}
#include <Windows.h>
#include <iostream>
#include <strsafe.h>
#include <stdio.h>
using namespace std;
int main ( TCHAR * env[ ] )
{
PWSTR lpBuffer = NULL ;
DWORD Size = GetEnvironmentVariable ( TEXT ( "PATH" ) , lpBuffer, 0 ) ;
if ( Size != 0 )
{
DWORD BufferSize = Size * sizeof ( TCHAR) ;
lpBuffer = ( PWSTR) malloc ( BufferSize) ;
GetEnvironmentVariable ( TEXT ( "PATH" ) , lpBuffer, BufferSize) ;
printf ( "%ls" , lpBuffer) ;
}
return 0 ;
}
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <versionhelpers.h>
using namespace std;
BOOL CompareOS ( INT dwMajorVersion, INT dwMinorVersion) ;
int main ( TCHAR * env[ ] )
{
TCHAR Buffer_1[ MAX_PATH] = { 0 } ;
DWORD DirLength = GetCurrentDirectory ( MAX_PATH, Buffer_1) ;
printf ( "目录: %ls
" , Buffer_1) ;
TCHAR Buffer_2[ MAX_PATH] ;
DWORD PathLength = GetFullPathName ( TEXT ( "C:" ) , MAX_PATH, Buffer_2, NULL ) ;
printf ( "目录: %ls
" , Buffer_2) ;
return 0 ;
}
VOID JudgeOS ( )
{
if ( ! CompareOS ( 10 , 0 ) )
if ( ! CompareOS ( 6 , 3 ) )
if ( ! CompareOS ( 6 , 2 ) )
if ( ! CompareOS ( 6 , 1 ) )
if ( ! CompareOS ( 6 , 0 ) )
if ( ! CompareOS ( 5 , 2 ) )
if ( ! CompareOS ( 5 , 1 ) )
if ( ! CompareOS ( 5 , 0 ) )
cout << "未知的操作系统版本" << endl;
else
cout << "版本在 Window2000 以上" << endl;
else
cout << "版本在 Windows XP 以上" << endl;
else
cout << "版本在 Windows XP Professional x64 Edition 以上" << endl;
else
cout << "版本在 Windows Vista 或者 Windows Server 2008 以上" << endl;
else
cout << "版本在 Windows 7 或者 Windows Server 2008 R2 以上" << endl;
else
cout << "版本在 Windows 8 或者 Windows Server 2012 以上" << endl;
else
cout << "版本在 Windows 8.1 或者 Windows Server 2012 R2 以上" << endl;
else
cout << "版本在 Windows Server 2016 或者 Windows 10 以上" << endl;
}
BOOL CompareOS ( INT MajorVersion, INT MinorVersion)
{
OSVERSIONINFOEX OSinfo = { 0 } ;
OSinfo. dwOSVersionInfoSize = sizeof ( OSinfo) ;
OSinfo. dwMajorVersion = MajorVersion; OSinfo. dwMinorVersion = MinorVersion;
DWORDLONG dwlConditionMask = 0 ;
VER_SET_CONDITION ( dwlConditionMask, VER_MAJORVERSION, VER_EQUAL) ;
VER_SET_CONDITION ( dwlConditionMask, VER_MINORVERSION, VER_EQUAL) ;
BOOL Res = VerifyVersionInfo ( & OSinfo, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask) ;
return Res;
}
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <versionhelpers.h>
using namespace std;
INT JudgeOS ( ) ;
int main ( TCHAR * env[ ] )
{
JudgeOS ( ) ;
return 0 ;
}
INT JudgeOS ( )
{
if ( IsWindowsServer ( ) )
cout << "操作系统是服务器" << endl;
else
if ( ! IsWindows10OrGreater ( ) )
if ( ! IsWindows8OrGreater ( ) )
if ( ! IsWindows7SP1OrGreater ( ) )
if ( ! IsWindows7OrGreater ( ) )
if ( ! IsWindowsVistaSP2OrGreater ( ) )
if ( ! IsWindowsVistaSP1OrGreater ( ) )
if ( ! IsWindowsVistaOrGreater ( ) )
if ( ! IsWindowsXPSP3OrGreater ( ) )
if ( ! IsWindowsXPSP2OrGreater ( ) )
if ( ! IsWindowsXPSP1OrGreater ( ) )
if ( ! IsWindowsXPOrGreater ( ) )
cout << "未知的操作系统版本" << endl;
else
cout << "当前操作系统版本是否与Windows XP版本匹配或大于Windows XP版本 " << endl;
else
cout << "当前操作系统版本是否与Windows XP Service Pack 1(SP1)版本匹配或大于" << endl;
else
cout << "当前操作系统版本是否与Windows XP Service Pack 2(SP2)版本匹配或大于" << endl;
else
cout << "当前操作系统版本是否与Windows XP Service Pack 3(SP3)版本匹配或大于" << endl;
else
cout << "当前操作系统版本是否与Windows Vista版本匹配或大于Windows Vista版本" << endl;
else
cout << "当前操作系统版本是否与Windows Vista Service Pack 1(SP1)版本匹配或大于" << endl;
else
cout << "当前操作系统版本是否与Windows Vista Service Pack 2(SP2)版本匹配或大于" << endl;
else
cout << "当前操作系统版本是否与Windows 7版本匹配或大于Windows 7版本" << endl;
else
cout << "当前操作系统版本是否与Windows 7 Service Pack 1(SP1)版本匹配或大于" << endl;
else
cout << "当前操作系统版本是否与Windows 8版本匹配或大于Windows 8版本" << endl;
else
cout << "指示当前操作系统版本是否与Windows 10版本匹配或大于Windows 10版本" << endl;
return 0 ;
}
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <versionhelpers.h>
using namespace std;
int main ( TCHAR * env[ ] )
{
DWORD ExitCode = NULL ;
SECURITY_ATTRIBUTES ProcessSec = { 0 } ; SECURITY_ATTRIBUTES ThreadSec = { 0 } ;
ProcessSec. nLength = sizeof ( ProcessSec) ; ThreadSec. nLength = sizeof ( ThreadSec) ;
ProcessSec. bInheritHandle = TRUE; ThreadSec. bInheritHandle = TRUE;
STARTUPINFO StartInfo = { sizeof ( STARTUPINFO) } ;
StartInfo. cb = sizeof ( StartInfo) ;
StartInfo. dwFlags = STARTF_USEPOSITION;
StartInfo. dwX = 400 ; StartInfo. dwY = 400 ;
PROCESS_INFORMATION ProcessInfo = { 0 } ;
TCHAR CmdLine[ ] = L"cmd.exe" ;
BOOL res = CreateProcess ( NULL , CmdLine, & ProcessSec, & ThreadSec, TRUE, CREATE_NEW_CONSOLE, NULL , NULL , & StartInfo, & ProcessInfo) ;
cout << "创建的新进程 ID: " << ProcessInfo. dwProcessId << endl;
cout << "创建的新线程 ID: " << ProcessInfo. dwThreadId << endl;
WaitForSingleObject ( ProcessInfo. hThread, INFINITE) ;
CloseHandle ( ProcessInfo. hThread) ;
WaitForSingleObject ( ProcessInfo. hProcess, INFINITE) ;
GetExitCodeProcess ( ProcessInfo. hProcess, & ExitCode) ;
cout << ExitCode << endl;
CloseHandle ( ProcessInfo. hProcess) ;
return 0 ;
}
#include <Windows.h>
#include <iostream>
#include <strsafe.h>
using namespace std;
void ErrorExit ( LPTSTR lpszFunction, DWORD LastError) ;
int main ( int argc, char * argv[ ] , TCHAR * env[ ] )
{
SHELLEXECUTEINFOW ExeInfo = { sizeof ( ExeInfo) } ;
ExeInfo. lpVerb = TEXT ( "runas" ) ; ExeInfo. lpFile = TEXT ( "cmd.exe" ) ; ExeInfo. nShow = SW_SHOWNORMAL;
if ( ! ShellExecuteEx ( & ExeInfo) )
{
DWORD LastError = GetLastError ( ) ;
TCHAR ErrString[ 10 ] = L"错误代码" ;
ErrorExit ( ErrString, LastError) ;
}
else
{
cout << "调用成功" << endl;
}
return 0 ;
}
void ErrorExit ( LPTSTR lpszFunction, DWORD LastError)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = LastError;
FormatMessage (
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL ,
dw,
MAKELANGID ( LANG_NEUTRAL, SUBLANG_DEFAULT) ,
( LPTSTR) & lpMsgBuf,
0 ,
NULL ) ;
lpDisplayBuf = ( LPVOID) LocalAlloc ( LMEM_ZEROINIT,
( lstrlen ( ( LPCTSTR) lpMsgBuf) + lstrlen ( ( LPCTSTR) lpszFunction) + 40 ) * sizeof ( TCHAR) ) ;
StringCchPrintf ( ( LPTSTR) lpDisplayBuf, LocalSize ( lpDisplayBuf) ,
TEXT ( "%s %d 的含义是: %s" ) ,
lpszFunction,
dw,
lpMsgBuf) ;
MessageBox ( NULL , ( LPCTSTR) lpDisplayBuf, TEXT ( "Error" ) , MB_OK) ;
LocalFree ( lpMsgBuf) ;
LocalFree ( lpDisplayBuf) ;
ExitProcess ( dw) ;
}
#include <Windows.h>
#include <iostream>
#include <shlobj.h>
#include <strsafe.h>
using namespace std;
VOID WINAPI ErrorCodeTransformation ( DWORD ErrorCode) ;
DWORD WINAPI GetProcessElevation ( TOKEN_ELEVATION_TYPE * pElevationType, BOOL* lsAdmin) ;
int main ( INT argc, CHAR * argv[ ] , TCHAR * env[ ] )
{
TOKEN_ELEVATION_TYPE pElevationType; BOOL IsAdmin;
if ( GetProcessElevation ( & pElevationType, & IsAdmin) )
{
switch ( pElevationType)
{
case TokenElevationTypeDefault:
cout << "[-] 进程以默认用户运行,或者UAC被禁用" << endl; break ;
case TokenElevationTypeFull:
cout << "[*] 进程的权限被成功提升,而且令牌没有被筛选过" << endl; break ;
case TokenElevationTypeLimited:
cout << "[*] 进程使用和一个筛选过的令牌对应的受限的权限运行" << endl; break ;
}
if ( IsAdmin) cout << "[*] 进程为管理员权限" << endl;
else cout << "[-] 进程为普通权限" << endl;
}
else
{
DWORD ErrorCode = GetProcessElevation ( & pElevationType, & IsAdmin) ;
ErrorCodeTransformation ( ErrorCode) ;
}
return 0 ;
}
DWORD WINAPI GetProcessElevation ( TOKEN_ELEVATION_TYPE * pElevationType, BOOL* pIsAdmin)
{
HANDLE hToken = NULL ; DWORD dwSize; DWORD ErrorCode = NULL ;
if ( ! OpenProcessToken ( GetCurrentProcess ( ) , TOKEN_QUERY, & hToken) )
{
ErrorCode = GetLastError ( ) ;
return ( ErrorCode) ;
}
BOOL bResult = FALSE;
if ( GetTokenInformation ( hToken, TokenElevationType, pElevationType, sizeof ( TOKEN_ELEVATION_TYPE) , & dwSize) )
{
BYTE adminSID[ SECURITY_MAX_SID_SIZE] ;
dwSize = sizeof ( adminSID) ;
CreateWellKnownSid ( WinBuiltinAccessControlAssistanceOperatorsSid, NULL , & adminSID, & dwSize) ;
if ( * pElevationType == TokenElevationTypeLimited)
{
HANDLE hUnfilteredToken = NULL ;
GetTokenInformation ( hToken, TokenLinkedToken, ( VOID * ) & hUnfilteredToken, sizeof ( HANDLE) , & dwSize) ;
if ( CheckTokenMembership ( hUnfilteredToken, & adminSID, pIsAdmin) ) bResult = TRUE;
CloseHandle ( hUnfilteredToken) ;
}
else
{
* pIsAdmin = IsUserAnAdmin ( ) ;
bResult = TRUE;
}
}
CloseHandle ( hToken) ;
return ( bResult) ;
}
VOID WINAPI ErrorCodeTransformation ( DWORD ErrorCode)
{
LPVOID lpMsgBuf; LPVOID lpDisplayBuf; DWORD dw = ErrorCode;
FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL , dw, MAKELANGID ( LANG_NEUTRAL, SUBLANG_DEFAULT) , ( LPTSTR) & lpMsgBuf, 0 , NULL
) ;
lpDisplayBuf = ( LPVOID) LocalAlloc ( LMEM_ZEROINIT, ( lstrlen ( ( LPCTSTR) lpMsgBuf) + 40 ) * sizeof ( TCHAR) ) ;
StringCchPrintf ( ( LPTSTR) lpDisplayBuf, LocalSize ( lpDisplayBuf) , TEXT ( "错误代码 %d : %s" ) , dw, lpMsgBuf) ;
MessageBox ( NULL , ( LPCTSTR) lpDisplayBuf, TEXT ( "Error" ) , MB_OK) ;
LocalFree ( lpMsgBuf) ; LocalFree ( lpDisplayBuf) ; ExitProcess ( dw) ;
}
#include <Windows.h>
#include <iostream>
#include <shlobj.h>
#include <strsafe.h>
#include <stdio.h>
#include <tlhelp32.h>
using namespace std;
VOID WINAPI ErrorCodeTransformation ( DWORD ErrorCode) ;
DWORD WINAPI ProcessInfo ( VOID) ;
DWORD WINAPI ProcessHandle ( DWORD ProcessID) ;
int main ( INT argc, CHAR * argv[ ] , TCHAR * env[ ] )
{
DWORD ErrorCode = ProcessInfo ( ) ;
ErrorCodeTransformation ( ErrorCode) ;
return 0 ;
}
DWORD WINAPI ProcessInfo ( VOID)
{
DWORD ErrorCode = NULL ; INT ProcessCount = 0 ;
HANDLE ProcessSnapshot = CreateToolhelp32Snapshot ( TH32CS_INHERIT | TH32CS_SNAPALL, 0 ) ;
if ( ProcessSnapshot == INVALID_HANDLE_VALUE)
{
ErrorCode = GetLastError ( ) ;
return ( ErrorCode) ;
}
PROCESSENTRY32W ProcessInfo; ProcessInfo. dwSize = sizeof ( ProcessInfo) ;
BOOL DisResult = Process32First ( ProcessSnapshot, & ProcessInfo) ;
if ( ! DisResult)
{
ErrorCode = GetLastError ( ) ;
return ( ErrorCode) ;
}
ProcessCount++ ;
cout << " 显示进程 ID: " << ProcessInfo. th32ProcessID << endl;
while ( DisResult)
{
ProcessCount++ ;
DisResult = Process32Next ( ProcessSnapshot, & ProcessInfo) ;
cout << " ID: " << ProcessInfo. th32ProcessID;
ProcessHandle ( ProcessInfo. th32ProcessID) ;
printf ( " 进程名: %ls
" , ProcessInfo. szExeFile) ;
}
return 0 ;
}
DWORD WINAPI ProcessHandle ( DWORD ProcessID)
{
DWORD ErrorCode = NULL ;
HANDLE ProcessHandle = OpenProcess ( PROCESS_QUERY_LIMITED_INFORMATION, TRUE, ProcessID) ;
if ( ! ProcessHandle)
{
ErrorCode = GetLastError ( ) ;
if ( ErrorCode == 5 ) cout << " 访问状态: 拒绝访问 " ;
return ( ErrorCode) ;
}
else
{
}
CloseHandle ( ProcessHandle) ;
return TRUE;
}
VOID WINAPI ErrorCodeTransformation ( DWORD ErrorCode)
{
LPVOID lpMsgBuf; LPVOID lpDisplayBuf; DWORD dw = ErrorCode;
FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL , dw, MAKELANGID ( LANG_NEUTRAL, SUBLANG_DEFAULT) , ( LPTSTR) & lpMsgBuf, 0 , NULL
) ;
lpDisplayBuf = ( LPVOID) LocalAlloc ( LMEM_ZEROINIT, ( lstrlen ( ( LPCTSTR) lpMsgBuf) + 40 ) * sizeof ( TCHAR) ) ;
StringCchPrintf ( ( LPTSTR) lpDisplayBuf, LocalSize ( lpDisplayBuf) , TEXT ( "错误代码 %d : %s" ) , dw, lpMsgBuf) ;
MessageBox ( NULL , ( LPCTSTR) lpDisplayBuf, TEXT ( "Error" ) , MB_OK) ;
LocalFree ( lpMsgBuf) ; LocalFree ( lpDisplayBuf) ; ExitProcess ( dw) ;
}