简体   繁体   中英

AutoIt and C++. Unicode

I wrote a DLL in C++:

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:

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.

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.

Inside your DLL implementation, you can do whatever you want!

In this case, you need to replace the std::wstring parameter with LPCWSTR (aka const wchar_t* ) instead. This is explained in AutoIt's DllCall documentation:

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

...

WINDOWS API Type: LPCWSTR/LPWSTR
AutoIt Type: WSTR

And from MSDN's Windows Data Types documentation:

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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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