dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Detecting if EXE is running in compatibility mode in XP

  1. #1
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Detecting if EXE is running in compatibility mode in XP

    Hi everyone:


    I just discovered that if someone runs my app in compatibilty mode with Windows 95 from within Win XP it not only messes up itself but also some system processes.

    Does anybody know how to detect if an EXE was started in a compatibilty mode from Windows XP?

  2. #2
    Join Date
    Nov 2004
    Posts
    67

    Re: Detecting if EXE is running in compatibility mode in XP

    You simply have to check the version of the OS you are running.

    Code:
             memset( &osvi, 0, sizeof(OSVERSIONINFO) );
             osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
             GetVersionEx( &osvi );
             if ( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
                ... NT and XP
             }
             else {
                ... not NT nor XP   
             }


    Rate this post if it helps you.

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Detecting if EXE is running in compatibility mode in XP

    Yeah, thanks that was the first what I tried, but the thing is a user in Windows XP can overwrite this value. I tried it already. With normal settings on my Windows XP GetVersionEx returns osvi.dwPlatformId == VER_PLATFORM_WIN32_NT, but if someone goes to the Properties window of the EXE file and sets "Run this program in compatibility mode for Windows 95" GetVersionEx will return osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS, which stands for Windows 9x.

    This is the problem. Do you know how to see if we're still running from under WinNT?

  4. #4
    Join Date
    Nov 2004
    Posts
    67

    Re: Detecting if EXE is running in compatibility mode in XP

    GetWindowsMajorVersion and GetWindowsMinorVersion
    give the version numbers for the currently installed Windows software.

    GetVersionEx
    gives the version of the operating system that is currently running.


    Rate this post if it helps you.

  5. #5
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Detecting if EXE is running in compatibility mode in XP

    GetWindowsMajorVersion and GetWindowsMinorVersion what kind of APIs are they - I didn't find them neither in MFC nor in global namespace?

    I made some research and came up with the following bit of code. Tell me what you think?

    Code:
    int GetOSTypeSmart(BOOL* pbOutCompatibilityMode)
    {
    	//Determine type of OS.
    	//RETURN: = 0 if Windows 95/98/Me;
    	//        = 1 if Windows NT/2000/XP and later;
    	//*pbOutCompatibilityMode might be set to TRUE if compatibility mode detected for WinNT
    	int nNT = -1;
    	OSVERSIONINFO os;
    	os.dwOSVersionInfoSize = sizeof(os);
    	if(::GetVersionEx(&os))
    	{
    		//Get data from GetVersionEx
    		if(os.dwPlatformId == VER_PLATFORM_WIN32_NT)
    			nNT = 1;
    		else
    			nNT = 0;
    	}
    
    	//Determine compatibility mode for Windows NT our way
    	int nMyNT = 0;
    	HMODULE hNTDll = GetModuleHandle("ntdll.dll");
    	HMODULE hKrnl = GetModuleHandle("kernel32.dll");
    	if(hNTDll && hKrnl)
    	{
    		if(GetProcAddress(hKrnl, "HeapLock"))
    			nMyNT = 1;
    	}
    
    	//See if GetVersionEx was successful
    	BOOL bComptbtyMode = FALSE;
    	if(nNT != -1)
    	{
    		//Check compatibility mode
    		if(nNT != nMyNT)
    			bComptbtyMode = TRUE;
    	}
    	else
    		nNT = nMyNT;
    
    	if(pbOutCompatibilityMode)
    		*pbOutCompatibilityMode = bComptbtyMode;
    
    	return nNT;
    }

  6. #6
    Join Date
    Nov 2004
    Posts
    67

    Re: Detecting if EXE is running in compatibility mode in XP

    1. Found GetWindowsMajorVersion in old Borland doc. It's implementation doesn't seem to exist anymore.
    2. Your code looks good. Don't have pure Win 9x to check it.
    3. Because of 2 I go with operating system of the host computer
    4. You had all knowledge to answer the initial question. You didn't really need help on this topic.


    Regards.

  7. #7
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Detecting if EXE is running in compatibility mode in XP

    Hey man don't get worked up about it.... I came up with this solution after I asked that question and made some research. Thank you for your reply though Now all I need to find out is if it's OK to check version like that since it's my complete improv and I'm not sure if it's gonna work on each platform?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)