How to get the name of an external window in a C # application?

I developed a simple application ( .dll) in LABVIEW, and I applied this dll to a Windows C # window ( Winforms). how

    [DllImport(@".\sample.dll")]
    public static extern void MyFunc(char[] a, StringBuilder b ,Int32 c); 

so when I call the function MyFunc, a window will be displayed (window Lab View(of Front panelmy labview application

Window

I need to get the window name ( ExpectedFuncName) in my C # application. ie I need to get the name of the external window that my C # application opens. Can we use FileVersionInfoor assembly loaderto get a name?

Is there any idea to do this? Thanks in advance.

+1
source share
2 answers

If you have a window handle, this is relatively simple:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);


[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);

...

int len;

// Window caption
if ((len = GetWindowTextLength(WindowHandle)) > 0) {
    sb = new StringBuilder(len + 1);
    if (GetWindowText(WindowHandle, sb, sb.Capacity) == 0)
        throw new Exception(String.Format("unable to obtain window caption, error code {0}", Marshal.GetLastWin32Error()));
    Caption = sb.ToString();
}

"WindowHandle" - .

, ( , ), , ( , MyFunc, [*]), .

# , , :

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

EnumWindows EnumWindowsProc , . , .

List<string> WindowLabels = new List<string>();

string GetWindowCaption(IntPtr hWnd) { ... }

bool MyEnumWindowsProc(IntPtr hWnd, IntPtr lParam) {
    int pid;

    GetWindowThreadProcessId(hWnd, out pid);

    if (pid == Process.GetCurrentProcess().Id) {
        // Window created by this process -- Starts heuristic
        string caption = GetWindowCaption(hWnd);

        if (caption != "MyKnownMainWindowCaption") {
           WindowLabels.Add(caption);
        }
    }

    return (true);
}

void DetectWindowCaptions() {
    EnumWindows(MyEnumWindowsProc, IntPtr.Zero);

    foreach (string s in WindowLabels) {
        Console.WriteLine(s);
    }
}

[*] , ( , ), , GetWindowThreadProcessId, , ...

+6

LabVIEW- (LabVIEW 2010) (LV 8.6, 2009), , 'FP.nativewindow. .
, :
FP.NativeWindow

+4

All Articles