كيف تحصل على اسم نافذة خارجية في تطبيق C#؟

StackOverflow https://stackoverflow.com/questions/3849025

  •  27-09-2019
  •  | 
  •  

سؤال

لقد طورت تطبيقًا بسيطًا (.dll) في LABVIEW وتوترت هذا DLL إلى تطبيق W Windows (Winforms). يحب

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

لذلك عندما أسمي الوظيفة MyFunc سيتم ظهر نافذة ( Lab View نافذة او شباك( Front panel تطبيق LabView الخاص بي

Window

أحتاج إلى الحصول على اسم النافذة (ExpectedFuncName) في تطبيق C# الخاص بي. أي أحتاج إلى الحصول على اسم النافذة الخارجية التي يتم Opend بواسطة تطبيق C# الخاص بي. هل يمكننا استخدام FileVersionInfo أو assembly loader للحصول على الاسم؟

هل هناك أي فكرة للقيام بذلك؟ شكرا مقدما.

هل كانت مفيدة؟

المحلول

إذا كان لديك مقبض النافذة ، فهذا سهل نسبيًا:

[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, ، حتى تعرف معرف العملية [*]) ، ثم استخدم بعض الاستدلال لتحديد المعلومات المطلوبة.

فيما يلي استيراد 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);
    }
}

*] في الحالة ، لا يتم إنشاء النافذة بواسطة تطبيقك (أي ولكن من عملية خلفية أخرى) ، يجب عليك تصفية القيم التي يتم إرجاعها بواسطة GetWindowThreadProcessID باستخدام معرف عملية آخر ، لكن هذا يتطلب سؤالًا آخر ...

نصائح أخرى

إذا قمت بتنشيط LabView Scripting (LabView 2010) ، أو تثبيته (LV 8.6 ، 2009) هناك خاصية لوح أمامي تسمى "fp.nativeWindow". هذا يعيد مقبض إلى نافذة اللوحة الأمامية.
استخدم المقتطف التالي للحصول على العقار:
FP.NativeWindow

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top