AutoIt 和 C++。统一码

AutoIt and C++. Unicode

我用C++写了一个DLL:

extern "C" __declspec(dllexport) void Msg(std::wstring filename)
{   
    MessageBox(NULL, filename.c_str(), L"", MB_OK);
}

当我尝试从 AutoIt 调用 DLL 时:

DllCall("mydll.dll", "none:cdecl", "Msg", "wstr", @AutoItExe)

我收到一条带有少量象形文字的消息。有什么问题?

编写导出的 DLL 函数时,不应在其接口中使用任何 C++ 库类型,因为它们取决于编译器、版本甚至解决方案(DEBUG 或 NDEBUG)。

如果这样做,您必须确保调用者使用这些类型的相同实现。这里不是这种情况。

您应该限制 DLL 导出函数在其接口中仅使用与 C 类型兼容的类型,或其他双方同意的类型。

在您的 DLL 实现中,您可以为所欲为!

在这种情况下,您需要将 std::wstring 参数替换为 LPCWSTR(又名 const wchar_t*)。这在 AutoIt 的 DllCall 文档中有解释:

WSTR
a UNICODE wide character string (a minimum of 65536 chars is allocated).

...

WINDOWS API Type: LPCWSTR/LPWSTR
AutoIt Type: WSTR

来自 MSDN 的 Windows Data Types 文档:

LPWSTR
A pointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.

This type is declared in WinNT.h as follows:

typedef WCHAR *LPWSTR;

...

LPCWSTR
A pointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.

This type is declared in WinNT.h as follows:

typedef CONST WCHAR *LPCWSTR;