0

我在我的一个 C# 应用程序中使用了 Sopcast activex 插件 (sopocx.ocx)。我想检索播放器状态(“正在缓冲频道”、“正在播放频道”、“频道离线...”)和缓冲百分比。这两个信息都显示在播放器上(我试图发布图片但我还没有足够的声誉)。

问题是 Sopcast activex 插件没有提供任何方法来检索这些信息。

有人知道如何做到这一点吗?

GetWindowText 产生一个空字符串...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace test
{


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


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

    private void testToolStripMenuItem_Click(object sender, EventArgs e)
    {
        IntPtr hwnd = sopcast.Handle;
        StringBuilder lpString = new StringBuilder(256);

        GetWindowText(hwnd, lpString, 256);

        MessageBox.Show(lpString.ToString());
    }


    private void playToolStripMenuItem_Click(object sender, EventArgs e)
    {
        sopcast.SetSopAddress("sop://broker.sopcast.com:3912/123456789");
        sopcast.SetChannelName("Channel");
        sopcast.Play();
    }
}

}
4

4 回答 4

1

这里有一个代码示例(用您的应用程序名称替换记事本)对您来说最重要的是从您的应用程序中获得一种方法来获得对您的 ocx 窗口的 ID 控制

using System;

使用 System.Runtime.InteropServices;

使用 System.Text;

使用 System.Security;

命名空间应用程序

{

public class Program

{

    public static void Main ( )

    {

        IntPtr hwnd = UnsafeNativeMethods.FindWindow("Notepad", null);

        StringBuilder stringBuilder = new StringBuilder(256);

        UnsafeNativeMethods.GetWindowText(hwnd, stringBuilder, stringBuilder.Capacity);

        Console.WriteLine(stringBuilder.ToString());

    }

}

[SuppressUnmanagedCodeSecurity]

internal static class UnsafeNativeMethods

{

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

    internal static extern int GetWindowText ( IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount );

    [DllImport("user32.dll", SetLastError = true)]

    internal static extern IntPtr FindWindow ( string lpClassName, string lpWindowName );



}

}

于 2012-07-28T19:16:03.133 回答
1

可以通过 api 窗口识别 I​​d 控件并获取文本

于 2012-07-28T11:51:57.907 回答
0

I download sopcast and try to get status using spy++: enter image description here

As you see, caption is status not the channel... so you can not get it easier enter image description here

the handle you have catched is for the whole control

于 2012-07-29T03:41:47.477 回答
0

Sopcast 使用 DrawText 绘制状态文本(我使用 API Monitor http://www.rohitab.com/apimonitor发现)。因此无法使用常规的 GetWindowText 函数或类似函数获取文本。我能够通过挂钩 DrawText 函数来获取文本。对于 .NET,EasyHook 将使您能够做到这一点。我的场景:我有一个承载 activex 控件的 winforms 应用程序,我想获取状态文本。

public class hooklocal : EasyHook.IEntryPoint
{
    [DllImport("user32.dll")]
    static extern int DrawText(IntPtr hDC, string lpString, int nCount, IntPtr lpRect, uint uFormat);

    [UnmanagedFunctionPointer(CallingConvention.StdCall,CharSet = CharSet.Auto,SetLastError = true)]
    delegate int DDrawText(IntPtr hDC, string lpString, int nCount, IntPtr lpRect, uint uFormat);

    int DrawTextH(IntPtr hDC, string lpString, int nCount, IntPtr lpRect, uint uFormat)
    {
        //lpString contains the status text 
        return DrawText(hDC, lpString, nCount, lpRect, uFormat);
    }

    public hooklocal()
    {
        try
        {
            var CreateHook = LocalHook.Create(
                 LocalHook.GetProcAddress("user32.dll", "DrawTextW"),
                 new DDrawText(DrawTextH),
                 this);

            CreateHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
        }
        catch (Exception ExtInfo)
        {
            Debugger.Break();
        }

    }

}

要使用,请在程序启动时在新线程中实例化 hooklocal 类。

EasyHook 下载 https://easyhook.github.io/downloads.html

于 2015-12-24T12:11:37.387 回答