找回密码
 加入
搜索
查看: 997|回复: 6

[网络通信] 【已解决】关于共享变量的问题

[复制链接]
发表于 2021-8-24 12:26:08 | 显示全部楼层 |阅读模式
本帖最后由 anythinging 于 2021-8-24 16:55 编辑

我想在一个程序中设置一个变量,另一个程序运行时能够读取到,但是未能实现,在此请教2个程序如何共用一个变量?
3个au3文件如下:
read.au3无法读取write.au3设置的$share的值

global.au3
Global $share

write.au3
#include <global.au3>
$share = 6

read.au3
#include <global.au3>
msgbox(0,'',$share)
发表于 2021-8-24 12:51:07 | 显示全部楼层
进程间的数据要么通过通讯获取,要么直接读。像你这种可以直接读。
GUICreate('读取共享变量', 300, 200, -1, -1)
$bt = GUICtrlCreateButton('读取', 30, 60, 50, 20)
GUISetState()

While 1
        Switch GUIGetMsg()
                Case -3
                        ExitLoop
                Case $bt
                        $Share = ControlGetText('写共享变量', '', 'Edit1')
                        MsgBox('', '', $Share, 1)
        EndSwitch
WEnd
发表于 2021-8-24 12:32:01 | 显示全部楼层
read.au3 应该 #include <write.au3>,否则根本执行不到 write.au3
#include <write.au3>
msgbox(0,'',$share)

另外,这不是共享变量,这是全局变量
 楼主| 发表于 2021-8-24 12:42:36 | 显示全部楼层
本帖最后由 anythinging 于 2021-8-24 12:45 编辑
afan 发表于 2021-8-24 12:32
read.au3 应该 #include ,否则根本执行不到 write.au3
#include
msgbox(0,'',$share)

请教write.au3写的变量能否在运行时,可以让read.au3读取到?我想实现的是write.exe始终运行,read.exe运行时可以读取到write的一个变量。(目前可以通过写入文件再读取来实现,想请教高级点的方法,比如是否可以写入内存再读取)
------------write.au3----------------------
GUICreate('写共享变量',300,200,-1,-1)
$input = GUICtrlCreateInput('',30,30,50,20)
$bt = GUICtrlCreateButton('设置',30,60,50,20)
GUISetState()
While 1
        Switch GUIGetMsg()
                Case -3
                        ExitLoop
                Case $bt
                        $t = GUICtrlRead($input)
                        Global $Share = $t
        EndSwitch
WEnd



------------read.au3----------------------
#include <global.au3>
#include <write.au3>
GUICreate('读取共享变量',300,200,-1,-1)
$input = GUICtrlCreateInput('',30,30,50,20)
$bt = GUICtrlCreateButton('读取',30,60,50,20)
GUISetState()

While 1
        Switch GUIGetMsg()
                Case -3
                        ExitLoop
                Case $bt
                        MsgBox('','',$Share,1)
        EndSwitch
WEnd
 楼主| 发表于 2021-8-24 12:53:39 | 显示全部楼层
感谢解答!看来我需要的是通过通讯来获取,求指点或示例。
发表于 2021-8-24 13:43:29 | 显示全部楼层
方法太多。比如简单的可以通过文件或注册表作为中介读写;父子进程的可以通过环境变量;通过TCP;通过内存;通过系统消息……
比如系统消息方式,可以用 GUIRegisterMsg 注册 WM_COPYDATA 消息。
具体可自行搜索关键字如“进程”,按需选择适合自己需求的方式

评分

参与人数 1金钱 +40 收起 理由
anythinging + 40 赞一个!

查看全部评分

 楼主| 发表于 2021-8-24 16:54:09 | 显示全部楼层
afan 发表于 2021-8-24 13:43
方法太多。比如简单的可以通过文件或注册表作为中介读写;父子进程的可以通过环境变量;通过TCP;通过内存 ...

感谢指点,我学习下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-2 11:56 , Processed in 0.077495 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表