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

  • SEARCH
  • Home
  • 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 972679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:09:42+00:00 2026-05-16T03:09:42+00:00

I’m trying to use a class named CFindWnd . Unfortunately it’s written in C

  • 0

I’m trying to use a class named CFindWnd. Unfortunately it’s written in C and I don’t have a clue how to translate it into C# (or any other language that I know). As it is not a very large class, I am hoping to find a helpful soul that is willing to translate it for me.

////////////////////////////////////////////////////////////////
// MSDN Magazine -- August 2003
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET on Windows XP. Tab size=3.
//
// ---
// This class encapsulates the process of finding a window with a given class name
// as a descendant of a given window. To use it, instantiate like so:
//
//        CFindWnd fw(hwndParent,classname);
//
// fw.m_hWnd will be the HWND of the desired window, if found.
//
class CFindWnd {
private:
    //////////////////
    // This private function is used with EnumChildWindows to find the child
    // with a given class name. Returns FALSE if found (to stop enumerating).
    //
    static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam) {
        CFindWnd *pfw = (CFindWnd*)lParam;
        HWND hwnd = FindWindowEx(hwndParent, NULL, pfw->m_classname, NULL);
        if (hwnd) {
            pfw->m_hWnd = hwnd;    // found: save it

            return FALSE;            // stop enumerating

        }
        EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse

        return TRUE;                // keep looking
    }

public:
    LPCSTR m_classname;            // class name to look for

    HWND m_hWnd;                    // HWND if found

    // ctor does the work--just instantiate and go

    CFindWnd(HWND hwndParent, LPCSTR classname)
        : m_hWnd(NULL), m_classname(classname)
    {
        FindChildClassHwnd(hwndParent, (LPARAM)this);
    }
};

EDIT:
I have come up with the following. But I have still trouble converting from object to IntPtr and vice versa.

class CFindWnd
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

    public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
    [DllImport("user32.Dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);


    private static bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
    {
        CFindWnd pfw = lParam as CFindWnd; //??? Error: can not be converted
        IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, pfw.m_classname, IntPtr.Zero);
        if (!hwnd.Equals(IntPtr.Zero)) //??? not sure if that is correct
        {
            pfw.m_hWnd = hwnd;    // found: save it
            return false;            // stop enumerating
        }
        EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
        return true;                // keep looking
    }


    public string m_classname;            // class name to look for
    public IntPtr m_hWnd;                    // HWND if found


    // ctor does the work--just instantiate and go
    public CFindWnd(IntPtr hwndParent, string classname)
    {
        m_hWnd = IntPtr.Zero;
        m_classname = classname;
        FindChildClassHwnd(hwndParent, this); //??? conversion error
    }
}
  • 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. Editorial Team
    Editorial Team
    2026-05-16T03:09:42+00:00Added an answer on May 16, 2026 at 3:09 am

    I have solved it. The solution is not quite a direct translation, because the original seems a bit too roundabout.
    Usage: Call the static method handle = CFindWnd.FindHandle(parentHandle, classname) to get the handle of a window that is a descendant of parentHandle and has the name classname.
    Example: Assuming you have a webbrowser control and want to manipulate the underlying Internet Explorer_Server directly, you can use CFindWnd.FindHandle(webbrowser.Handle, “Internet Explorer_Server”) to get it’s handle.

    class CFindWnd
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
    
        public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
        [DllImport("user32.Dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
    
    
        static private bool FindChildClassHwnd(IntPtr parentHandle, IntPtr lParam)
        {
            IntPtr hwnd = FindWindowEx(parentHandle, IntPtr.Zero, _classname, IntPtr.Zero);
            if (!hwnd.Equals(IntPtr.Zero))
            {
                _handle = hwnd;           // found: save it
                return false;            // stop enumerating
            }
            EnumChildWindows(parentHandle, FindChildClassHwnd, lParam); // recurse
            return true;                 // keep looking
        }
    
        static private string _classname;              // class name to look for
        static private IntPtr _handle;                 // HWND if found
    
        static public IntPtr FindHandle(IntPtr parentHandle, string classname)
        {
            _handle = IntPtr.Zero;
            _classname = classname;
            FindChildClassHwnd(parentHandle, IntPtr.Zero);
            return _handle;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a

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.