Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Change style of window owned by another app in 98? Works in XP.

Change style of window owned by another app in 98? Works in XP.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Gibbs
    wrote on last edited by
    #1

    I need to modify the style of a window owned by another application under Windows 98/ME and XP/2000. I can get it to work under XP, but not 98. Windows XP In XP (and hopefully 2000) the following code works:

    DWORD dwStyle, dwError;

    // Get and modify a window's style.
    if (dwStyle = GetWindowLongPtr (hWnd, GWL_STYLE))
    {
    dwStyle &= ~(WS_TILEDWINDOW | WS_CHILD | WS_POPUP);
    dwStyle |= WS_BORDER | WS_OVERLAPPED;
    SetLastError (0);
    if (0 == SetWindowLongPtr (hWnd, GWL_STYLE, dwStyle) && 0 != (dwError = GetLastError ()))
    {
    CString csMess;

    csMess.Format ("SetWindowLongPtr Failed: %lX", dwError);
    AfxMessageBox (csMess, MB\_ICONSTOP);
    

    }
    }

    To make this work in XP I first have to set some privileges as below:

    bool CTools::SetPrivileges ()
    {
    bool bRet = false;
    HANDLE hToken;

    if (OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken))
    {
    TOKEN_PRIVILEGES tPriv;

    tPriv.PrivilegeCount = 1;
    if (LookupPrivilegeValue ("", SE\_DEBUG\_NAME, &tPriv.Privileges\[0\].Luid))
    {
      tPriv.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED;
      if (AdjustTokenPrivileges (hToken, FALSE, &tPriv, NULL, NULL, NULL) &&
          GetLastError() == ERROR\_SUCCESS)
        bRet = true;
    }
    CloseHandle (hToken);
    

    }
    return bRet;
    }

    Windows 98 Under Windows 98 these don't work. SetWindowLongPtr returns 0 (usually non-zero when it works) but GetLastError returns 0 too. More importantly, the style doesn't change. GetWindowLongPtr does work though. There may be a permissions problem in 98 like there was in XP but SetPrivileges is not relevant to 98. Documentation does say: "Windows 95/98/Me: The SetWindowLong function may fail if the window specified by the hWnd parameter does not belong to the same process as the calling thread." I also tried this, without success in 98 or XP (even with priveleges set):

    CWnd::ModifyStyle (hWnd, WS_TILEDWINDOW | WS_CHILD | WS_POPUP, WS_BORDER | WS_OVERLAPPED, 0);

    In XP it indicates failure with it's return value. In 98 it pretends to work but the style doesn't change. Does anyone know another way to change a windows style? I don't know what to do to set the style of a window my application doesn't own under Win 98. Any ideas? Thanks in advance! Alex Gibbs

    A A 2 Replies Last reply
    0
    • A Alex Gibbs

      I need to modify the style of a window owned by another application under Windows 98/ME and XP/2000. I can get it to work under XP, but not 98. Windows XP In XP (and hopefully 2000) the following code works:

      DWORD dwStyle, dwError;

      // Get and modify a window's style.
      if (dwStyle = GetWindowLongPtr (hWnd, GWL_STYLE))
      {
      dwStyle &= ~(WS_TILEDWINDOW | WS_CHILD | WS_POPUP);
      dwStyle |= WS_BORDER | WS_OVERLAPPED;
      SetLastError (0);
      if (0 == SetWindowLongPtr (hWnd, GWL_STYLE, dwStyle) && 0 != (dwError = GetLastError ()))
      {
      CString csMess;

      csMess.Format ("SetWindowLongPtr Failed: %lX", dwError);
      AfxMessageBox (csMess, MB\_ICONSTOP);
      

      }
      }

      To make this work in XP I first have to set some privileges as below:

      bool CTools::SetPrivileges ()
      {
      bool bRet = false;
      HANDLE hToken;

      if (OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken))
      {
      TOKEN_PRIVILEGES tPriv;

      tPriv.PrivilegeCount = 1;
      if (LookupPrivilegeValue ("", SE\_DEBUG\_NAME, &tPriv.Privileges\[0\].Luid))
      {
        tPriv.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED;
        if (AdjustTokenPrivileges (hToken, FALSE, &tPriv, NULL, NULL, NULL) &&
            GetLastError() == ERROR\_SUCCESS)
          bRet = true;
      }
      CloseHandle (hToken);
      

      }
      return bRet;
      }

      Windows 98 Under Windows 98 these don't work. SetWindowLongPtr returns 0 (usually non-zero when it works) but GetLastError returns 0 too. More importantly, the style doesn't change. GetWindowLongPtr does work though. There may be a permissions problem in 98 like there was in XP but SetPrivileges is not relevant to 98. Documentation does say: "Windows 95/98/Me: The SetWindowLong function may fail if the window specified by the hWnd parameter does not belong to the same process as the calling thread." I also tried this, without success in 98 or XP (even with priveleges set):

      CWnd::ModifyStyle (hWnd, WS_TILEDWINDOW | WS_CHILD | WS_POPUP, WS_BORDER | WS_OVERLAPPED, 0);

      In XP it indicates failure with it's return value. In 98 it pretends to work but the style doesn't change. Does anyone know another way to change a windows style? I don't know what to do to set the style of a window my application doesn't own under Win 98. Any ideas? Thanks in advance! Alex Gibbs

      A Offline
      A Offline
      agentnem
      wrote on last edited by
      #2

      I don't know which version of MFC you're using, but CWnd::ModifyStyle doesn't have a parameter for window handles. According to Microsoft documentation (msdn.microsoft.com/library/default.asp), the function is defined as: BOOL ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0); So you don't need a window handle, just a CWnd pointer to the application in question (I don't know how to do that myself, but I'm sure other forum members can help):suss:.

      A 1 Reply Last reply
      0
      • A agentnem

        I don't know which version of MFC you're using, but CWnd::ModifyStyle doesn't have a parameter for window handles. According to Microsoft documentation (msdn.microsoft.com/library/default.asp), the function is defined as: BOOL ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0); So you don't need a window handle, just a CWnd pointer to the application in question (I don't know how to do that myself, but I'm sure other forum members can help):suss:.

        A Offline
        A Offline
        Alex Gibbs
        wrote on last edited by
        #3

        I too saw that only the one you mentioned is documented. I found the other when Dev Studio showed two forms to choose from when I was typing it in. Going to the declaration takes you to afxwin.h in the CWnd declaration: static BOOL PASCAL ModifyStyle(HWND hWnd, DWORD dwRemove, DWORD dwAdd, UINT nFlags); which of course also has: BOOL ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0); I tried both forms under 98 and neither worked. Both now work in XP though. At least it was another thing to try. :) Alex Gibbs

        1 Reply Last reply
        0
        • A Alex Gibbs

          I need to modify the style of a window owned by another application under Windows 98/ME and XP/2000. I can get it to work under XP, but not 98. Windows XP In XP (and hopefully 2000) the following code works:

          DWORD dwStyle, dwError;

          // Get and modify a window's style.
          if (dwStyle = GetWindowLongPtr (hWnd, GWL_STYLE))
          {
          dwStyle &= ~(WS_TILEDWINDOW | WS_CHILD | WS_POPUP);
          dwStyle |= WS_BORDER | WS_OVERLAPPED;
          SetLastError (0);
          if (0 == SetWindowLongPtr (hWnd, GWL_STYLE, dwStyle) && 0 != (dwError = GetLastError ()))
          {
          CString csMess;

          csMess.Format ("SetWindowLongPtr Failed: %lX", dwError);
          AfxMessageBox (csMess, MB\_ICONSTOP);
          

          }
          }

          To make this work in XP I first have to set some privileges as below:

          bool CTools::SetPrivileges ()
          {
          bool bRet = false;
          HANDLE hToken;

          if (OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken))
          {
          TOKEN_PRIVILEGES tPriv;

          tPriv.PrivilegeCount = 1;
          if (LookupPrivilegeValue ("", SE\_DEBUG\_NAME, &tPriv.Privileges\[0\].Luid))
          {
            tPriv.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED;
            if (AdjustTokenPrivileges (hToken, FALSE, &tPriv, NULL, NULL, NULL) &&
                GetLastError() == ERROR\_SUCCESS)
              bRet = true;
          }
          CloseHandle (hToken);
          

          }
          return bRet;
          }

          Windows 98 Under Windows 98 these don't work. SetWindowLongPtr returns 0 (usually non-zero when it works) but GetLastError returns 0 too. More importantly, the style doesn't change. GetWindowLongPtr does work though. There may be a permissions problem in 98 like there was in XP but SetPrivileges is not relevant to 98. Documentation does say: "Windows 95/98/Me: The SetWindowLong function may fail if the window specified by the hWnd parameter does not belong to the same process as the calling thread." I also tried this, without success in 98 or XP (even with priveleges set):

          CWnd::ModifyStyle (hWnd, WS_TILEDWINDOW | WS_CHILD | WS_POPUP, WS_BORDER | WS_OVERLAPPED, 0);

          In XP it indicates failure with it's return value. In 98 it pretends to work but the style doesn't change. Does anyone know another way to change a windows style? I don't know what to do to set the style of a window my application doesn't own under Win 98. Any ideas? Thanks in advance! Alex Gibbs

          A Offline
          A Offline
          Alex Gibbs
          wrote on last edited by
          #4

          Here is an update. I can modify another window's style under XP using any of the calls below, if the priveleges are set as mentioned in the first post: SetWindowLongPtr (hWnd, GWL_STYLE, dwStyle); CWnd::ModifyStyle (hWnd, dwRemove, dwAdd, 0); pCWnd->ModifyStyle (dwRemove, dwAdd); However, none of them change the visible appearance of a window under 98. Remember I am trying to change the window of another process. I think the problem is one of priveleges/permissions in 98. I found something else that might be relevant at MSDN, but I don't think it is the problem. MSDN Article, 5th paragraph: "Windows 95/98/Me automatically adds and removes the WS_EX_WINDOWEDGE style for windows in all applications. ..." Anyone able to help with priveleges/permissions in 98? Alex Gibbs

          A 1 Reply Last reply
          0
          • A Alex Gibbs

            Here is an update. I can modify another window's style under XP using any of the calls below, if the priveleges are set as mentioned in the first post: SetWindowLongPtr (hWnd, GWL_STYLE, dwStyle); CWnd::ModifyStyle (hWnd, dwRemove, dwAdd, 0); pCWnd->ModifyStyle (dwRemove, dwAdd); However, none of them change the visible appearance of a window under 98. Remember I am trying to change the window of another process. I think the problem is one of priveleges/permissions in 98. I found something else that might be relevant at MSDN, but I don't think it is the problem. MSDN Article, 5th paragraph: "Windows 95/98/Me automatically adds and removes the WS_EX_WINDOWEDGE style for windows in all applications. ..." Anyone able to help with priveleges/permissions in 98? Alex Gibbs

            A Offline
            A Offline
            agentnem
            wrote on last edited by
            #5

            It's possible that the window of another process "undos" any changes made to it. I know of some programmers who put code in their MFC applications to prevent certain window style changes. I believe the Windows Media Player exhibits this behaviour. If this other process is of your own creation, then you may want to use user-defined messages to invoke certain behaviour.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups