简体   繁体   English

AutoIt和C ++。 统一

[英]AutoIt and C++. Unicode

I wrote a DLL in C++: 我用C ++编写了一个DLL:

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

When I try to call the DLL from AutoIt: 当我尝试从AutoIt调用DLL时:

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

I get a message with a small number of hieroglyphs. 我收到一条带有少量象形文字的消息。 What is the problem? 问题是什么?

When you write an exported DLL function, you SHOULD NOT use any C++ library types in its interface, as these are compiler, version, and even solution (DEBUG or NDEBUG) dependent. 编写导出的DLL函数时,不应在其接口中使用任何C ++库类型,因为它们依赖于编译器,版本甚至解决方案(DEBUG或NDEBUG)。

If you do, you must make sure that the caller uses the same implemetation of those types. 如果这样做,则必须确保调用者使用这些类型的相同实现。 Which is not the case here. 这里不是这种情况。

You should restrict DLL exported functions to use only types in their interface that are compatible with C types, or other mutually agreed upon types. 您应该限制DLL导出的函数,使其仅在其接口中使用与C类型兼容的类型,或其他相互同意的类型。

Inside your DLL implementation, you can do whatever you want! 在DLL实现中,您可以做任何您想做的事情!

In this case, you need to replace the std::wstring parameter with LPCWSTR (aka const wchar_t* ) instead. 在这种情况下,您需要用LPCWSTR (aka const wchar_t* )替换std::wstring参数。 This is explained in AutoIt's DllCall documentation: AutoIt的DllCall文档中DllCall说明:

WSTR WSTR
a UNICODE wide character string (a minimum of 65536 chars is allocated). 一个UNICODE宽字符串(至少分配了65536个字符)。

... ...

WINDOWS API Type: LPCWSTR/LPWSTR WINDOWS API类型:LPCWSTR / LPWSTR
AutoIt Type: WSTR AutoIt类型:WSTR

And from MSDN's Windows Data Types documentation: 从MSDN的Windows数据类型文档中:

LPWSTR LPWSTR
A pointer to a null-terminated string of 16-bit Unicode characters. 指向以空字符结尾的16位Unicode字符的指针。 For more information, see Character Sets Used By Fonts. 有关更多信息,请参见字体使用的字符集。

This type is declared in WinNT.h as follows: 在WinNT.h中声明此类型,如下所示:

 typedef WCHAR *LPWSTR; 

... ...

LPCWSTR LPCWSTR
A pointer to a constant null-terminated string of 16-bit Unicode characters. 指向以16位Unicode字符为常数的,以空字符终止的常量的指针。 For more information, see Character Sets Used By Fonts. 有关更多信息,请参见字体使用的字符集。

This type is declared in WinNT.h as follows: 在WinNT.h中声明此类型,如下所示:

 typedef CONST WCHAR *LPCWSTR; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM