最近用到这个东西,发现网上并没有现成可用的解决方案。
这里给出我的解决办法~
anydesk虽然小巧但是不稳定,teamview功能更强大也更稳定一点。
**方案一**
使用TeamView自带的API来实现远程控制。(AnyDesk好像没有开放的接口)
优点:简单。
缺点:需要通过浏览器并且需要输入密码。
客户端获取本地TeamViewer的ID传给服务器
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-------------------------------------首先引入命名空间 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> System.Net; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> System.Net.Sockets; -----------------------下面是客户端获取TeamViewerID并发送给服务器的函数 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">send</span>() { TcpClient client; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { client = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> TcpClient(); client.Connect(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"localhost"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8500</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 与服务器连接</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (Exception ex) { MessageBox.Show(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"连接服务器失败"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } RegistryKey key = Registry.LocalMachine; RegistryKey keyID = key.OpenSubKey(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SOFTWARE\\Wow6432Node\\TeamViewer"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>); String ID = keyID.GetValue(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ClientID"</span>).ToString(); NetworkStream streamToServer = client.GetStream(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] buffer = Encoding.Unicode.GetBytes(ID); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获得缓存</span> streamToServer.Write(buffer, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, buffer.Length); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 发往服务器</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>
下面这一段代码是寻找注册表中TeamViewer的ID。有了ID就可以远程控制了。
<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> RegistryKey <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">key</span> = Registry.LocalMachine; RegistryKey keyID = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">key</span>.OpenSubKey(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SOFTWARE\\Wow6432Node\\TeamViewer"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span> ID = keyID.GetValue(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ClientID"</span>).ToString();</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
服务器端获取客户端发送的TeamViewerID从而实现远程控制。
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">--------------------------------------同样引入命名空间 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> System.Net; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> System.Net.Sockets; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> Microsoft.Win32; ----------------------------------下面是服务器接收客户端内容并远程控制的代码 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">receive</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> BufferSize = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8192</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 缓存大小,8192字节</span> IPAddress ip = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> IPAddress(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] { <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> }); TcpListener listener = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> TcpListener(ip, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8500</span>); listener.Start(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 开始侦听</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) { TcpClient remoteClient = listener.AcceptTcpClient(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取一个连接,同步方法,在此处中断</span> NetworkStream streamToClient = remoteClient.GetStream(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取Stream</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] buffer = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[BufferSize]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> bytesRead = streamToClient.Read(buffer, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, BufferSize); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获得请求的字符串</span> String ID = Encoding.Unicode.GetString(buffer, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, bytesRead); String teamViewer = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"start.teamviewer.com/"</span> + ID ; System.Diagnostics.Process.Start(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"iexplore.exe"</span>, teamViewer); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//启动IE浏览器远程控制</span> } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>
上面这段程序并没有考虑同一个客户端多次发送相同的ID~。实际需要中可以进一步进行限制
下面这一段就是远程控制的代码了~~
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> String teamViewer = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"start.teamviewer.com/device/"</span> + ID + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/au-thorization/password/mode/control"</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Diagnostics</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Process</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Start</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"iexplore.exe"</span>, teamViewer)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //启动IE浏览器远程制</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
是不是很简单~~~想具体了解的可以参考TeamViewer文档
可以参考我的百度经验
**方案二**
使用Windows API远程控制TeamView或AnyDesk。
优点:可以自动获取密码直接访问
缺点:较为负杂
首先客户端要自动将自己的Id和密码获取到。然后上传。
客户端首先通过
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">IntPtr maindHwnd = FindWindow(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TeamViewer"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得句柄 </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
获取TeamView或AnyDesk的句柄
接下来用一个循环遍历子窗口
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;"> IntPtr winPtr </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> GetWindow(maindHwnd, GetWindowCmd.GW_CHILD);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
通过
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> GetClassName(winPtr, stringBuilder1, stringBuilder1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Capacity</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
判断每一个子窗口的类型。如果是自己需要的就读取它的值。(通过Spy来查看TeamView和AnyDesk各个控件的句柄,以此判断自己需要子窗口的类型)
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> GetWindowText(winPtr, stringBuilder, stringBuilder<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Capacity</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
这样客户端就可以自动得到TeamView和AnyDesk的id和密码了。上传以后控制方就可以通过服务器得到id和密码。同样使用Windows API(SendMessage)可以自动输入id和密码实现远程控制。
因为有人私信,下面给出部分代码参考.
首先引入相应命名空间
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"User32.dll"</span>, EntryPoint = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"FindWindow"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> IntPtr <span class="hljs-title" style="box-sizing: border-box;">FindWindow</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span> lpClassName, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span> lpWindowName); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"User32.dll"</span>, EntryPoint = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"FindWindowEx"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> IntPtr <span class="hljs-title" style="box-sizing: border-box;">FindWindowEx</span>(IntPtr hwndParent, IntPtr hwndChildAfter, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span> lpClassName, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span> lpWindowName); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"User32.dll"</span>, EntryPoint = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SendMessage"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-title" style="box-sizing: border-box;">SendMessage</span>(IntPtr hWnd, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> Msg, IntPtr wParam, StringBuilder lParam); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user32.dll "</span>, EntryPoint = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GetDlgItem"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> IntPtr <span class="hljs-title" style="box-sizing: border-box;">GetDlgItem</span>(IntPtr hParent, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> nIDParentItem); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user32.dll"</span>, EntryPoint = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GetWindowText"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-title" style="box-sizing: border-box;">GetWindowText</span>(IntPtr hwnd, StringBuilder lpString, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> cch); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user32.dll"</span>, SetLastError = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> IntPtr GetWindow(IntPtr hWnd, GetWindowCmd uCmd); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user32.dll"</span>, SetLastError = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, CharSet = CharSet.Auto)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> GetClassName(IntPtr hWnd, StringBuilder lpClassName, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> nMaxCount); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user32.dll"</span>, EntryPoint = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"PostMessage"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-title" style="box-sizing: border-box;">PostMessage</span>(IntPtr hwnd, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> wMsg, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> wParam, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> lParam); [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user32.dll"</span>, EntryPoint = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ShowWindow"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> <span class="hljs-title" style="box-sizing: border-box;">ShowWindow</span>(IntPtr hWnd, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> nCmdShow); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//恢复窗口 1是 SW_SHOWNORMAL</span> [DllImport(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user32.dll"</span>)] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> <span class="hljs-title" style="box-sizing: border-box;">SetForegroundWindow</span>(IntPtr hWnd);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul>
获取TeamView信息:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-title" style="box-sizing: border-box;">getTeamViewInfo</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> index = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> number = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> BM_CLICK = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xF5</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> WM_SETTEXT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xC</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> WM_GETTEXT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xD</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) { Thread.Sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>); number++; IntPtr maindHwnd = FindWindow(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TeamViewer"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得句柄 </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (maindHwnd != IntPtr.Zero) { IntPtr winPtr = GetWindow(maindHwnd, GetWindowCmd.GW_CHILD); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3、循环取得桌面下的所有子窗口</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (winPtr != IntPtr.Zero) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4、继续获取下一个子窗口</span> IntPtr winPtr1 = GetWindow(winPtr, GetWindowCmd.GW_CHILD); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (winPtr1 != IntPtr.Zero) { StringBuilder stringBuilder1 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuilder(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span>); StringBuilder stringBuilder2 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuilder(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span>); StringBuilder stringBuilder3 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuilder(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span>); GetWindowText(winPtr1, stringBuilder1, stringBuilder1.Capacity); GetClassName(winPtr1, stringBuilder2, stringBuilder2.Capacity); SendMessage(winPtr1, WM_GETTEXT, (IntPtr)stringBuilder3.Capacity, stringBuilder3); str = stringBuilder1.ToString(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (str.Equals(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"连接到伙伴"</span>)) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// SendMessage(winPtr1, BM_CLICK, (IntPtr)stringBuilder3.Capacity, stringBuilder3);</span> connect = winPtr1; } str = stringBuilder2.ToString(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (str.Equals(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ComboBox"</span>)) { connect_id = winPtr1; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (str.Equals(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Edit"</span>)) { teamviewer_id = stringBuilder3.ToString(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//if (index == 0)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//{</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// teamviewer_id = stringBuilder3.ToString();</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//}</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//if (index == 1)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//{</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// teamviewer_password = stringBuilder3.ToString();</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// return 1;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//}</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//index++;</span> } winPtr1 = GetWindow(winPtr1, GetWindowCmd.GW_HWNDNEXT); } winPtr = GetWindow(winPtr, GetWindowCmd.GW_HWNDNEXT); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (number ><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; } } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 连接</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">connectTeamviewer</span>() { Thread.Sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> BM_CLICK = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xF5</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> WM_SETTEXT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xC</span>; StringBuilder stringBuilder = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuilder(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span>); stringBuilder.Append(teamviewer_id); SendMessage(connect_id, WM_SETTEXT, (IntPtr)stringBuilder.Capacity, stringBuilder); SendMessage(connect, BM_CLICK, (IntPtr)stringBuilder.Capacity, stringBuilder); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// confirm();</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li></ul>
获取AnyDesk的信息:
<code class="hljs axapta has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> getAnyDeskInfo() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">index</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> number = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; const <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> BM_CLICK = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xF5</span>; const <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> WM_SETTEXT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xC</span>; const <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> WM_GETTEXT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xD</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) { Thread.Sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>); number++; IntPtr maindHwnd = FindWindow(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"AnyDesk"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得句柄 </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (maindHwnd != IntPtr.Zero) { IntPtr winPtr = GetWindow(maindHwnd, GetWindowCmd.GW_CHILD); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3、循环取得桌面下的所有子窗口</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (winPtr != IntPtr.Zero) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4、继续获取下一个子窗口</span> IntPtr winPtr1 = GetWindow(winPtr, GetWindowCmd.GW_CHILD); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (winPtr1 != IntPtr.Zero) { IntPtr winPtr2 = GetWindow(winPtr1, GetWindowCmd.GW_CHILD); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (winPtr2 != IntPtr.Zero) { StringBuilder stringBuilder1 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuilder(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// GetWindowText(winPtr1, stringBuilder1, stringBuilder1.Capacity);</span> SendMessage(winPtr2, WM_GETTEXT, (IntPtr)stringBuilder1.Capacity, stringBuilder1); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = stringBuilder1.ToString(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.Equals(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This Desk"</span>) || <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.Equals(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"此工作台"</span>)) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) { winPtr2 = GetWindow(winPtr2, GetWindowCmd.GW_HWNDNEXT); StringBuilder stringBuilder2 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuilder(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span>); GetClassName(winPtr2, stringBuilder2, stringBuilder2.Capacity); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = stringBuilder2.ToString(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.Equals(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id_label_cls#12\n"</span>)) { StringBuilder stringBuilder3 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuilder(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span>); GetWindowText(winPtr2, stringBuilder3, stringBuilder3.Capacity); anydesk_id = stringBuilder3.ToString(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } } } winPtr2 = GetWindow(winPtr2, GetWindowCmd.GW_HWNDNEXT); } winPtr1 = GetWindow(winPtr1, GetWindowCmd.GW_HWNDNEXT); } winPtr = GetWindow(winPtr, GetWindowCmd.GW_HWNDNEXT); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (number > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; } } }</code>