Страница 16 из 24 35)-------------------------------------------------------------------------- Q> Как разорвать соединение? A> bool HangUp() { RASCONN ras[20]; DWORD dSize, dNumber; char szBuf[256]; ras[0].dwSize = sizeof( RASCONN ); dSize = sizeof( ras ); // Get active RAS - Connection DWORD dwRet = RasEnumConnections( ras, &dSize, &dNumber ); if ( dwRet != 0 ) { if ( RasGetErrorString( (UINT)dwRet, (LPSTR)szBuf, 256 ) != 0 ) wsprintf( (LPSTR)szBuf, "Undefined RAS Enum Connections error (%ld).", dwRet ); MessageBox( (LPSTR)szBuf, "RasHangUp", MB_OK | MB_ICONSTOP ); return false; } bool bOK = true; for( DWORD dCount = 0; dCount < dNumber; dCount++ ) { // Hang up that connection HRASCONN hRasConn = ras[dCount].hrasconn; DWORD dwRet = RasHangUp( hRasConn ); if ( dwRet != 0 ) { char szBuf[256]; if ( RasGetErrorString( (UINT)dwRet, (LPSTR)szBuf, 256 ) != 0 ) wsprintf( (LPSTR)szBuf, "Undefined RAS HangUp Error (%ld).", dwRet ); MessageBox( (LPSTR)szBuf, "RasHangUp", MB_OK | MB_ICONSTOP ); bOK = false; } } return bOK; } 36)--------------------------------------------------------------------------- Q>Как узнать максимум информации о типе и возможностях ОС на удаленной машине A> NetServerGetInfo(LPTSTR servername,DWORD level,LPBYTE *bufptr); Откликаются все операционки производства MS и UNIXы с установленной самбой. Выдают о себе все что сами знают ;-) 37)---------------------------------------------------------------------------- Q> Как создать hard link на NTFS? A> #include #include #pragma hdrstop #define err doerr( __FILE__, __LINE__ ) void doerr( const char *file, int line ) { DWORD e; e = GetLastError(); if ( e == 0 ) return; printf( "%s(%d): gle = %lu\n", file, line, e ); exit( 2 ); } void enableprivs() { HANDLE hToken; byte buf[sizeof TOKEN_PRIVILEGES * 2]; TOKEN_PRIVILEGES & tkp = *( (TOKEN_PRIVILEGES *) buf ); if ( ! OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) err; // берем SeBackupPrivilege и SeRestorePrivilege if ( !LookupPrivilegeValue( NULL, SE_BACKUP_NAME, &tkp.Privileges[0].Luid ) ) err; if ( !LookupPrivilegeValue( NULL, SE_RESTORE_NAME, &tkp.Privileges[1].Luid ) ) err; tkp.PrivilegeCount = 2; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; tkp.Privileges[1].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ); } int main( int argc, char *argv[] ) { HANDLE fh; if ( argc != 3 ) { printf( "usage: lnw {file} {new_link_name}\n" ); return 1; } enableprivs(); fh = CreateFile( argv[1], GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_POSIX_SEMANTICS, NULL ); if ( fh == INVALID_HANDLE_VALUE || fh == NULL ) err; static char buf1[MAX_PATH]; static wchar_t buf2[MAX_PATH * 2]; char *p; void *ctx = NULL; WIN32_STREAM_ID wsi; DWORD numwritten; GetFullPathName( argv[2], MAX_PATH, &buf1[0], &p ); wsi.dwStreamId = BACKUP_LINK; wsi.dwStreamAttributes = 0; wsi.dwStreamNameSize = 0; wsi.Size.QuadPart = strlen( buf1 ) * 2 + 2; MultiByteToWideChar( CP_ACP, 0, buf1, strlen( buf1 ) + 1, buf2, MAX_PATH ); if ( ! BackupWrite( fh, (byte *) &wsi, 20, &numwritten, FALSE, FALSE, &ctx ) )err; if ( numwritten != 20 ) err; if ( ! BackupWrite( fh, (byte *) buf2, wsi.Size.LowPart, &numwritten, FALSE, FALSE, &ctx ) )err; if ( numwritten != wsi.Size.LowPart )err; BackupWrite( fh, (byte *) &buf1[0], 0, &numwritten, TRUE, FALSE, &ctx ); CloseHandle( fh ); return 0; } |