首页 > 解决方案 > C++ 脚本无法从 AHK 检索变量

问题描述

我正在尝试制作一个运行 AHK 线程并从中检索变量的 C++ 脚本。它设法毫无问题地运行 AHK 脚本,但似乎没有返回任何内容。

typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2);
typedef BOOL (*pahkReady)(void);

HINSTANCE handle;
pahkdll ahkdll;

pahkReady ahkReady;


void debugAHK(){
   while (!ahkReady()){
      Sleep(10);
   }
}

bool init(){
    handle = LoadLibrary("AutoHotkey.dll");
    if (!handle){ std::cout << "Could not load dynamic library!\n"; return 0;}

    ahkdll = (pahkdll)GetProcAddress(handle, "ahkdll");
    ahkReady = (pahkReady)GetProcAddress(handle, "ahkReady");

    ahkdll("","","");

    debugAHK();

   return 1;
}

void check(){
    ahkdll("ahk.ahk", "", "");
    debugAHK();
    typedef wchar_t* (*AhkGetVar)(wchar_t *, UINT);
    AhkGetVar l_hAutoHotKeyGetVar = (AhkGetVar)GetProcAddress(handle, "ahkgetvar");
    wchar_t* wcha = l_hAutoHotKeyGetVar(L"MyVar", 0); // 0 : content, 1 : pointer

    std::wstring wcha(l_sRes);
    std::cout << std::string(w.begin(), w.end());

}

当我尝试返回内容时,它不会返回任何内容。当我尝试返回一个指针时,它只是返回一个看似随机的数字序列,带有 .txt 后缀或其他一些随机字符串。
AHK 脚本很简单:

#Persistent
#NoTrayIcon
MyVar := "test"

标签: c++autohotkey

解决方案


推荐阅读