Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 41669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:13:03+00:00 2026-05-10T15:13:03+00:00

In a C++ app, I have an hWnd pointing to a window running in

  • 0

In a C++ app, I have an hWnd pointing to a window running in a third party process. This window contains controls which extend the COM TreeView control. I am interested in obtaining the CheckState of this control.
I use the hWnd to get an HTREEITEM using TreeView_GetRoot(hwnd) from commctrl.h

hwnd points to the window and hItem is return value from TreeView_GetRoot(hwnd). They are used as follows:

int iCheckState = TreeView_GetCheckState(hwnd,  hItem); switch (iCheckState) {    case 0:       // (unchecked)    case 1:       // checked    ... } 

I’m looking to port this code into a C# app which does the same thing (switches off the CheckState of the TreeView control). I have never used COM and am quite unfamiliar.

I have tried using the .NET mscomctl but can’t find equivalent methods to TreeView_GetRoot or TreeView_GetCheckState. I’m totally stuck and don’t know how to recreate this code in C# 🙁

Suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:13:04+00:00Added an answer on May 10, 2026 at 3:13 pm

    We have these definitions from CommCtrl.h:

    #define TreeView_SetItemState(hwndTV, hti, data, _mask) \ { TVITEM _ms_TVi;\   _ms_TVi.mask = TVIF_STATE; \   _ms_TVi.hItem = (hti); \   _ms_TVi.stateMask = (_mask);\   _ms_TVi.state = (data);\   SNDMSG((hwndTV), TVM_SETITEM, 0, (LPARAM)(TV_ITEM *)&_ms_TVi);\ }  #define TreeView_SetCheckState(hwndTV, hti, fCheck) \   TreeView_SetItemState(hwndTV, hti, INDEXTOSTATEIMAGEMASK((fCheck)?2:1), TVIS_STATEIMAGEMASK) 

    We can translate this to C# using PInvoke. First, we implement these macros as functions, and then add whatever other support is needed to make those functions work. Here is my first shot at it. You should double check my code especially when it comes to the marshalling of the struct. Further, you may want to Post the message cross-thread instead of calling SendMessage.

    Lastly, I am not sure if this will work at all since I believe that the common controls use WM_USER+ messages. When these messages are sent cross-process, the data parameter’s addresses are unmodified and the resulting process may cause an Access Violation. This would be a problem in whatever language you use (C++ or C#), so perhaps I am wrong here (you say you have a working C++ program).

    static class Interop {  public static IntPtr TreeView_SetCheckState(HandleRef hwndTV, IntPtr hti, bool fCheck) {     return TreeView_SetItemState(hwndTV, hti, INDEXTOSTATEIMAGEMASK((fCheck) ? 2 : 1), (uint)TVIS.TVIS_STATEIMAGEMASK); }  public static IntPtr TreeView_SetItemState(HandleRef hwndTV, IntPtr hti, uint data, uint _mask) {     TVITEM _ms_TVi = new TVITEM();     _ms_TVi.mask = (uint)TVIF.TVIF_STATE;     _ms_TVi.hItem = (hti);     _ms_TVi.stateMask = (_mask);     _ms_TVi.state = (data);     IntPtr p = Marshal.AllocCoTaskMem(Marshal.SizeOf(_ms_TVi));     Marshal.StructureToPtr(_ms_TVi, p, false);     IntPtr r = SendMessage(hwndTV, (int)TVM.TVM_SETITEMW, IntPtr.Zero, p);     Marshal.FreeCoTaskMem(p);     return r; }  private static uint INDEXTOSTATEIMAGEMASK(int i) { return ((uint)(i) << 12); }  [DllImport('user32.dll', CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, IntPtr lParam);  private enum TVIF : uint {     TVIF_STATE = 0x0008 }  private enum TVIS : uint {     TVIS_STATEIMAGEMASK = 0xF000 }  private enum TVM : int {     TV_FIRST = 0x1100,     TVM_SETITEMA = (TV_FIRST + 13),     TVM_SETITEMW = (TV_FIRST + 63) }  private struct TVITEM {     public uint mask;     public IntPtr hItem;     public uint state;     public uint stateMask;     public IntPtr pszText;     public int cchTextMax;     public int iImage;     public int iSelectedImage;     public int cChildren;     public IntPtr lParam; } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 59k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer const int * a; Read the declaration from right to… May 11, 2026 at 8:57 am
  • added an answer Have you tried Magick++ and/or MagickCore? Your next best bet… May 11, 2026 at 8:57 am
  • added an answer Use the class property and change your css styles to… May 11, 2026 at 8:57 am

Related Questions

Background In a C# command-line app I'm writing, several of the parameters have yes
In my C# winforms app, I have a datagrid. When the datagrid reloads, I
I'm writing an app in C# (.net 3.5) and I have a question about
I have a WinForms app written in C# with .NET 3.5. It runs a
I have a Windows Form app written in C#. Its job is to send
I have an app written in C# that lies on a network share. When
I need to create reports in a C# .NET Windows app. I've got an
In my c# app, I've got a list that I navigate with an Enumerator.
In a .NET WinForms app, how do I get a specific drive's icon (C:\
I am writing a very specialized app in C# that floats as a mostly

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.