1

我在其中开发了一个简单的应用程序 ( .dll),LABVIEW并将该 dll 导入到 C# windows 应用程序( Winforms) 中。喜欢

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

所以当我调用该函数时,MyFunc会弹出一个Lab View窗口(Front panel我的labview应用程序的窗口(

窗户

我需要ExpectedFuncName在我的 C# 应用程序中获取窗口名称 ()。即我需要获取由我的 C# 应用程序打开的外部窗口的名称。我们可以使用FileVersionInfoassembly loader获取名称吗?

有什么想法吗?提前致谢。

4

2 回答 2

6

如果你有窗口句柄,这相对容易:

[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创建的,所以你知道进程 ID [*]),然后使用一些启发式方法来确定所需的信息。

这是您在没有句柄的情况下应使用的函数的 C# 导入:

[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);
    }
}

[*] 如果窗口不是由您的应用程序创建的(即来自另一个后台进程),您应该使用另一个进程 ID 过滤 GetWindowThreadProcessId 返回的值,但这需要另一个问题...

于 2010-10-03T08:48:42.363 回答
4

如果您激活 LabVIEW 脚本 (LabVIEW 2010),或安装它 ( LV 8.6, 2009 ),则会有一个名为“FP.nativewindow”的前面板属性。这将返回前面板窗口的句柄。
使用以下代码段获取属性:
FP.NativeWindow

于 2010-10-05T19:29:33.780 回答