联动通达信 - 通达信自定义消息

Published

大智慧或者同花顺浏览股票的时候,通达信跟着变动

1.通达信有很多自定义消息,通过传递消息来完成某个小功能将大大简化编程,提高效率.就是win32调用.比如显示某支股票可以向通达信发送消息来实现。

这些消息是逆向工程得出的,并非官方公开API,因此可能随着通达信版本的更新而变化,使用时需注意兼容性。

通达信软件提供了很多窗口消息用于外部程序控制。这些消息通常通过Windows消息机制(SendMessage、PostMessage)来发送。

以下是一些常用的通达信自定义消息及其功能描述。

 

c

UINT UWM_STOCK = RegisterWindowMessage(_T("Stock"));
  
::PostMessage(HWND_BROADCAST,UWM_STOCK,7580019,0);
//就是查看 580019 的页面,
::PostMessage(HWND_BROADCAST,UWM_STOCK,6031007,0);
//是查看031007页面

注意股票代码:沪市代码前加7,其它市场股票代码前面加6.

2.若无消息接口,模拟键盘输入来联动,效率低稳定性差。

AutoHotkey代码

;这里指定0xFFFF就是HWND_BROADCAST,是对多个通达信窗口广播消息;
;如果指定具体窗口的句柄,则是向特定窗口发送消息。
 
active_id:=0xFFFF
UWM_STOCK := DllCall("RegisterWindowMessage", Str,"Stock")
PostMessage,UWM_STOCK,7600050,0,,ahk_id %active_id%

 

 

https://www.cnblogs.com/freeboygirl

完整源代码

https://blog.51cto.com/u_15408625/6221685

 

 

c

UINT UWM_STOCK = RegisterWindowMessage(_T("Stock"));

::PostMessage(HWND_BROADCAST,UWM_STOCK,7580019,0);
//就是查看 580019 的页面,
::PostMessage(HWND_BROADCAST,UWM_STOCK,6031007,0);
//是查看031007页面

c#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WgServer
{
    public class TdxWHelp   //关联打开通达信K线图用的
    {
        private static Hashtable processWnd = new Hashtable();
        [StructLayout(LayoutKind.Sequential)]
        public struct ProcessEntry32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExeFile;
        }
        public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);

        [DllImport("KERNEL32.DLL")]
        public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
        [DllImport("KERNEL32.DLL")]
        public static extern int CloseHandle(IntPtr handle);
        [DllImport("KERNEL32.DLL")]
        public static extern int Process32First(IntPtr handle, ref ProcessEntry32 pe);
        [DllImport("KERNEL32.DLL")]
        public static extern int Process32Next(IntPtr handle, ref ProcessEntry32 pe);

        [DllImport("user32.dll")]
        private static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

        [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

        [DllImport("user32.dll", EntryPoint = "IsWindow")]
        public static extern bool IsWindow(IntPtr hWnd);

        [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
        public static extern void SetLastError(uint dwErrCode);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool GetGUIThreadInfo(IntPtr hTreadID, ref ProcessEntry32 lpgui);

        [DllImport("user32.dll")]
        public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        public extern static int RegisterWindowMessage(string stock);

        public static void SendStockCode(string StockCode)
        {
            if (StockCode == null) { return; }
            if (StockCode.Length != 6) { return; }
            int SendStock;
            if (StockCode.Substring(0, 1) == "6" || StockCode.Substring(0, 2) == "11")
            {
                SendStock = int.Parse("7" + StockCode);
            }
            else if (StockCode == "999999")
            {
                SendStock = int.Parse("7" + StockCode);
            }
            else
            {
                SendStock = int.Parse("6" + StockCode);
            }

            //if (StockCode.Substring(0, 1) == "6")
            //{
            //    SendStock = int.Parse( StockCode);
            //}
            //else if (StockCode == "999999")
            //{
            //    SendStock = int.Parse(StockCode);
            //}
            //else
            //{
            //    SendStock = int.Parse(StockCode);
            //}

            IntPtr hh = GetHandleByProcessName("TdxW.exe");

            // StringBuilder s = new StringBuilder(512);
            //  int i = GetWindowText(hh, s, s.Capacity);  
            int UWM_STOCK = RegisterWindowMessage("Stock");
            // int UWM_STOCK = RegisterWindowMessage("通达信金融终端");
            PostMessage(hh, UWM_STOCK, SendStock, 0);
        }

 public static IntPtr GetHandleByProcessName(string ProcessName)
        {
            List<ProcessEntry32> list = new List<ProcessEntry32>();
            IntPtr handle = CreateToolhelp32Snapshot(0x2, 0);
            IntPtr hh = IntPtr.Zero;
            if ((int)handle > 0)
            {
                ProcessEntry32 pe32 = new ProcessEntry32();
                pe32.dwSize = (uint)Marshal.SizeOf(pe32);
                int bMore = Process32First(handle, ref pe32);
                while (bMore == 1)
                {
                    IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize);
                    Marshal.StructureToPtr(pe32, temp, true);
                    ProcessEntry32 pe = (ProcessEntry32)Marshal.PtrToStructure(temp, typeof(ProcessEntry32));
                    Marshal.FreeHGlobal(temp);
                    list.Add(pe);
                    if (pe.szExeFile.ToUpper() == ProcessName.ToUpper())
                    {
                        bMore = 2;
                        hh = GetCurrentWindowHandle(pe.th32ProcessID);
                        break;
                    }
                    bMore = Process32Next(handle, ref pe32);
                }
            }
            return hh;
        }
        public static IntPtr GetCurrentWindowHandle(uint proid)
        {
            IntPtr ptrWnd = IntPtr.Zero;
            uint uiPid = proid;
            object objWnd = processWnd[uiPid];
            if (objWnd != null)
            {
                ptrWnd = (IntPtr)objWnd;
                if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))  // 从缓存中获取句柄
                {
                    return ptrWnd;
                }
                else
                {
                    ptrWnd = IntPtr.Zero;
                }
            }
            bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
            // 枚举窗口返回 false 并且没有错误号时表明获取成功
            if (!bResult && Marshal.GetLastWin32Error() == 0)
            {
                objWnd = processWnd[uiPid];
                if (objWnd != null)
                {
                    ptrWnd = (IntPtr)objWnd;
                }
            }
            return ptrWnd;
        }
        private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
        {
            uint uiPid = 0;
            if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref uiPid);
                if (uiPid == lParam)                                              // 找到进程对应的主窗口句柄
                {
                    if (!processWnd.Contains(uiPid))
                    {
                        processWnd.Add(uiPid, hwnd);                    // 把句柄缓存起来
                        SetLastError(0);                                             // 设置无错误
                    }
                    return false;                                                     // 返回 false 以终止枚举窗口
                }
            }
            return true;
        }

    }
}