윈도우 종류 알아내기
윈도우 95, 98, ME
윈도우 NT 계열
CString CWindowCloserDlg::GetWindows()
{
//////////////////////////////
// OS detection routine.
OSVERSIONINFO version;
version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
CString bNT;
if(GetVersionEx(&version))
{
DWORD dwplatformid = version.dwPlatformId;
switch (version.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
// strplatformid.Format("Windows 3.1");
bNT = "Windows 3.1";
break;
case VER_PLATFORM_WIN32_WINDOWS:
if(version.dwMinorVersion == 0)
{
// strplatformid.Format("Windows 95");
bNT = "Windows 95";
}
else if(version.dwMinorVersion == 10)
{
// strplatformid.Format("Windows 98");
bNT = "Windows 98";
}
else
{
bNT = "Windows Me";
}
break;
case VER_PLATFORM_WIN32_NT:
// strplatformid.Format("Windows NT");
if (version.dwMinorVersion == 0 && version.dwMajorVersion == 4 )
{
bNT = "Windows NT 4.0";
}
else if (version.dwMinorVersion == 0 && version.dwMajorVersion == 5)
{
bNT = "Windows 2000";
}
else if (version.dwMinorVersion == 1)
{
bNT = "Windows XP";
}
else if (version.dwMinorVersion == 2)
{
bNT = "Windows Server 2003";
}
else
{
bNT = "Windows NT 3.51";
}
break;
default:
bNT = "UnKnown";
break;
}
}
return bNT;
}
{
//////////////////////////////
// OS detection routine.
OSVERSIONINFO version;
version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
CString bNT;
if(GetVersionEx(&version))
{
DWORD dwplatformid = version.dwPlatformId;
switch (version.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
// strplatformid.Format("Windows 3.1");
bNT = "Windows 3.1";
break;
case VER_PLATFORM_WIN32_WINDOWS:
if(version.dwMinorVersion == 0)
{
// strplatformid.Format("Windows 95");
bNT = "Windows 95";
}
else if(version.dwMinorVersion == 10)
{
// strplatformid.Format("Windows 98");
bNT = "Windows 98";
}
else
{
bNT = "Windows Me";
}
break;
case VER_PLATFORM_WIN32_NT:
// strplatformid.Format("Windows NT");
if (version.dwMinorVersion == 0 && version.dwMajorVersion == 4 )
{
bNT = "Windows NT 4.0";
}
else if (version.dwMinorVersion == 0 && version.dwMajorVersion == 5)
{
bNT = "Windows 2000";
}
else if (version.dwMinorVersion == 1)
{
bNT = "Windows XP";
}
else if (version.dwMinorVersion == 2)
{
bNT = "Windows Server 2003";
}
else
{
bNT = "Windows NT 3.51";
}
break;
default:
bNT = "UnKnown";
break;
}
}
return bNT;
}
윈도우 95, 98, ME
void CShutdown::SetShutDownMode(UINT l_bMode)
{
if(l_bMode == LOGOFF)
OnWindowsLogOff();
else if(l_bMode == REBOOT)
SystemShutdown(TRUE);
else if(l_bMode == SHUTDOWN)
SystemShutdown(FALSE);
}
BOOL CShutdown::SystemShutdown(BOOL l_bReboot)
{
if(GetWindowsSystemType()) // If This OS is like WinNT, The Return value is TRUE
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
CString l_sErrorCode;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox("OpenProcessToken failed.");
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
AfxMessageBox("AdjustTokenPrivileges enable failed.");
if(!InitiateSystemShutdown( NULL,
NULL, //"System Shutdown , so You must Save datas of Each Application ",
0,
TRUE,
l_bReboot)) //<--이 함수가 2번째 Argument에 NULL을 입력해도 NT에서만 메세지가 나타나는지 검사할것.
{ //또다른 함수로써 ExitWindowsEx가 있음 이 함수의 Flag로써는 종료시 EWX_SHUTDOWN을 이용하면 됨
l_sErrorCode.Format("%d",GetLastError());
if(l_sErrorCode != "1115")
{
AfxMessageBox("Abort System Shutdown! Error Code is "+l_sErrorCode);
return FALSE;
}
}
return TRUE;
}
else
{
if(l_bReboot)
return ExitWindowsEx(EWX_REBOOT,0);
else
return ExitWindowsEx(EWX_SHUTDOWN,0);
}
}
BOOL CShutdown::OnWindowsLogOff()
{
return ExitWindowsEx(EWX_LOGOFF,0);
}
BOOL CShutdown::GetWindowsSystemType()
{
UINT l_uiVersion = GetVersion();
if (l_uiVersion < 0x80000000) // Windows NT
return TRUE;
else
return FALSE;
}
{
if(l_bMode == LOGOFF)
OnWindowsLogOff();
else if(l_bMode == REBOOT)
SystemShutdown(TRUE);
else if(l_bMode == SHUTDOWN)
SystemShutdown(FALSE);
}
BOOL CShutdown::SystemShutdown(BOOL l_bReboot)
{
if(GetWindowsSystemType()) // If This OS is like WinNT, The Return value is TRUE
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
CString l_sErrorCode;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox("OpenProcessToken failed.");
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
AfxMessageBox("AdjustTokenPrivileges enable failed.");
if(!InitiateSystemShutdown( NULL,
NULL, //"System Shutdown , so You must Save datas of Each Application ",
0,
TRUE,
l_bReboot)) //<--이 함수가 2번째 Argument에 NULL을 입력해도 NT에서만 메세지가 나타나는지 검사할것.
{ //또다른 함수로써 ExitWindowsEx가 있음 이 함수의 Flag로써는 종료시 EWX_SHUTDOWN을 이용하면 됨
l_sErrorCode.Format("%d",GetLastError());
if(l_sErrorCode != "1115")
{
AfxMessageBox("Abort System Shutdown! Error Code is "+l_sErrorCode);
return FALSE;
}
}
return TRUE;
}
else
{
if(l_bReboot)
return ExitWindowsEx(EWX_REBOOT,0);
else
return ExitWindowsEx(EWX_SHUTDOWN,0);
}
}
BOOL CShutdown::OnWindowsLogOff()
{
return ExitWindowsEx(EWX_LOGOFF,0);
}
BOOL CShutdown::GetWindowsSystemType()
{
UINT l_uiVersion = GetVersion();
if (l_uiVersion < 0x80000000) // Windows NT
return TRUE;
else
return FALSE;
}
윈도우 NT 계열
BOOL InitiateSystemShutdown
(
LPTSTR lpMachineName, // pointer to name of computer to shut down
LPTSTR lpMessage, // pointer to message to display in dialog box
DWORD dwTimeout, // time to display dialog box
BOOL bForceAppsClosed, // force applications closed flag
BOOL bRebootAfterShutdown // reboot flag
);
BOOL IsOsNT()
{
//////////////////////////////
// OS detection routine.
OSVERSIONINFO version;
/* First we have to find out which OS we are running on */
version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
/* If we get and error, we can"t continue. */
// CString strplatformid;
BOOL bNT = FALSE;
if(GetVersionEx(&version))
{
DWORD dwplatformid = version.dwPlatformId;
switch (version.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
// strplatformid.Format("Windows 3.1");
bNT = FALSE;
break;
case VER_PLATFORM_WIN32_WINDOWS:
if(version.dwMinorVersion == 0)
{
// strplatformid.Format("Windows 95");
bNT = FALSE;
}
else if(version.dwMinorVersion > 0)
{
// strplatformid.Format("Windows 98");
bNT = FALSE;
}
break;
case VER_PLATFORM_WIN32_NT:
// strplatformid.Format("Windows NT");
bNT = TRUE;
break;
default:
bNT = FALSE;
break;
}
}
return bNT;
}
int nResult = ::MessageBox(NULL,
"새로운 설정 내용을 적용하기위해 시스템을 다시 시작합니다.",
"시스템 설정 바꾸기",
MB_OK);
if(nResult == IDOK)
{
if(IsOsNT())
{
ShutDownNT(); //NT,2000
}
else
{
ExitWindowsEx( EWX_REBOOT, 0 ); //win95,98,ME
}
}
(
LPTSTR lpMachineName, // pointer to name of computer to shut down
LPTSTR lpMessage, // pointer to message to display in dialog box
DWORD dwTimeout, // time to display dialog box
BOOL bForceAppsClosed, // force applications closed flag
BOOL bRebootAfterShutdown // reboot flag
);
void ShutDownNT()
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox("OpenProcessToken failed.");
// Get the LUID for shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
AfxMessageBox("AdjustTokenPrivileges enable failed.");
// Display the shutdown dialog box and start the time-out countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
"System rebooting.", // message to user
0, // time-out period (<- 여기를 20 이라고 쓰면 20초 후 에 리부팅한다.)
FALSE, // ask user to close apps
TRUE); // reboot after shutdown
if (!fResult)
{
AfxMessageBox("InitiateSystemShutdown failed.");
}
// error string..
//PrintCSBackupAPIErrorMessage(GetLastError());
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
{
AfxMessageBox("AdjustTokenPrivileges disable failed.");
}
}
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox("OpenProcessToken failed.");
// Get the LUID for shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
AfxMessageBox("AdjustTokenPrivileges enable failed.");
// Display the shutdown dialog box and start the time-out countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
"System rebooting.", // message to user
0, // time-out period (<- 여기를 20 이라고 쓰면 20초 후 에 리부팅한다.)
FALSE, // ask user to close apps
TRUE); // reboot after shutdown
if (!fResult)
{
AfxMessageBox("InitiateSystemShutdown failed.");
}
// error string..
//PrintCSBackupAPIErrorMessage(GetLastError());
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
{
AfxMessageBox("AdjustTokenPrivileges disable failed.");
}
}
이제 OS 가 win NT 계열인지 알아내는 function 이 필요하다. win 2000 도 NT 기반이다.
BOOL IsOsNT()
{
//////////////////////////////
// OS detection routine.
OSVERSIONINFO version;
/* First we have to find out which OS we are running on */
version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
/* If we get and error, we can"t continue. */
// CString strplatformid;
BOOL bNT = FALSE;
if(GetVersionEx(&version))
{
DWORD dwplatformid = version.dwPlatformId;
switch (version.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
// strplatformid.Format("Windows 3.1");
bNT = FALSE;
break;
case VER_PLATFORM_WIN32_WINDOWS:
if(version.dwMinorVersion == 0)
{
// strplatformid.Format("Windows 95");
bNT = FALSE;
}
else if(version.dwMinorVersion > 0)
{
// strplatformid.Format("Windows 98");
bNT = FALSE;
}
break;
case VER_PLATFORM_WIN32_NT:
// strplatformid.Format("Windows NT");
bNT = TRUE;
break;
default:
bNT = FALSE;
break;
}
}
return bNT;
}
리부팅하려면 메시지 창을 하나 보여주고 다음과 같이 OS 에 따라 적절한 function 을 call 해준다.
int nResult = ::MessageBox(NULL,
"새로운 설정 내용을 적용하기위해 시스템을 다시 시작합니다.",
"시스템 설정 바꾸기",
MB_OK);
if(nResult == IDOK)
{
if(IsOsNT())
{
ShutDownNT(); //NT,2000
}
else
{
ExitWindowsEx( EWX_REBOOT, 0 ); //win95,98,ME
}
}
"MFC" 카테고리의 다른 글
- 프로그램내에서 한영전환 하기 ImmGetConversionSt... (0)2007/03/26
- 프로그램 중복 실행 방지 (0)2007/03/26
- 윈도우 종류 및 종료하기 (0)2007/03/25
- 툴바의 툴팁을 내가 원하는데로 (0)2007/03/23
- Child윈도를 Popup시키는 방법 (0)2007/03/23

수안이의 컴퓨터 연구실



Leave your greetings.