LPTSTR lpBuffer
is a pointer to a non-constant TCHAR. TCHAR being either a simple char or a wide char depending on whether UNICODE is defined in your project.
Means from MQL you either have to use FormatMessageA() or FormatMessageW(). As you use MQL5, in your case you have to use the wide char version.
Use the context help of Visual Studio, or PE Explorer from HeavenTools to quickly find out how a specific function is exported by a DLL.
- alphatrading: have to use FormatMessageA() or FormatMessageW().Strings are all wide since Build 600 and Higher (17.02.2014)
-
Amy Liu: Ref: https://docs.microsoft.com/en-us/windows/desktop/api/WinBase/nf-winbase-formatmessagePlease use the link button See the difference? https://docs.microsoft.com/en-us/windows/desktop/api/WinBase/nf-winbase-formatmessage
Messages Editor
whroeder1:
- Strings are all wide since Build 600 and Higher (17.02.2014)
-
Please use the link button See the
difference? https://docs.microsoft.com/en-us/windows/desktop/api/WinBase/nf-winbase-formatmessage
Messages Editor
Thank you. I have edited the original post to use link button.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Ref: https://docs.microsoft.com/en-us/windows/desktop/api/WinBase/nf-winbase-formatmessage
Below code is the syntax:
In my MQL4 code, I import it first like this:
DWORD is 32 bit unsigned, so use uint to replace DWORD.
LPCVOID lpSource has the follow description in ms article:
lpSource
The location of the message definition. The type of this parameter depends upon the settings in the dwFlagsparameter.
If neither of these flags is set in dwFlags, then lpSource is ignored.
This means lpsource is a handle or pointer, so we can try uint or ulong.
LPTSTR lpBuffer is a pointer to a 16 bit UNICODE character buffer, so we can pass ushort array to get the string.
va_list *Arguments is a little complicated but the key information is here:
If you do not have a pointer of type va_list*, then specify the FORMAT_MESSAGE_ARGUMENT_ARRAY flag and pass a pointer to an array of DWORD_PTR values; those values are input to the message formatted as the insert values. Each insert must have a corresponding element in the array.
and combine the upper information with below:
Security Remarks
If this function is called without FORMAT_MESSAGE_IGNORE_INSERTS, the Arguments parameter must contain enough parameters to satisfy all insertion sequences in the message string, and they must be of the correct type. Therefore, do not use untrusted or unknown message strings with inserts enabled because they can contain more insertion sequences than Arguments provides, or those that may be of the wrong type. In particular, it is unsafe to take an arbitrary system error code returned from an API and use FORMAT_MESSAGE_FROM_SYSTEM withoutFORMAT_MESSAGE_IGNORE_INSERTS.
So va_list *Arguments can be a 64 bit pointer when ulong can be used.
and ref to an example of use FormatMessage to get system error description: https://docs.microsoft.com/zh-cn/windows/desktop/Debug/retrieving-the-last-error-code
I then use the imported function like this:
But when my code run to FormatMessage line, it generates errors of "Cannot find 'FormatMessage' in 'kernel32.dll'" and "unresolved import function call". Can anybody help? Thank you very much!