digitalmars.D.learn - How do I convert a LPVOID (void*) to string?
- Nieto (1/15) Oct 16 2017
- Nieto (2/21) Oct 16 2017 I forget to call LocalFree(lpMsgBuf); here...
- Adam D. Ruppe (28/29) Oct 16 2017 There is no one answer to this, but for the specific function are
- Igor (7/36) Oct 19 2017 If you will not use this buffer in any other way but as an
- Kagamin (0/16) Oct 19 2017
- Mike Parker (7/26) Oct 19 2017 If you're using this solely for Windows error messages, the
- Nieto (5/11) Oct 21 2017 Yep, I was using solely for Windows error messages. In fact, I
How do I convert/create a D string from LPVOID (void*)?string GetLastErrorMessage() { LPVOID lpMsgBuf; DWORD errorMessageID = GetLastError(); FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), cast(LPSTR) &lpMsgBuf, 0, NULL); return ?? }
Oct 16 2017
On Monday, 16 October 2017 at 21:48:35 UTC, Nieto wrote:How do I convert/create a D string from LPVOID (void*)?I forget to call LocalFree(lpMsgBuf); here...string GetLastErrorMessage() { LPVOID lpMsgBuf; DWORD errorMessageID = GetLastError(); FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), cast(LPSTR) &lpMsgBuf, 0, NULL); return ?? }
Oct 16 2017
On Monday, 16 October 2017 at 21:48:35 UTC, Nieto wrote:How do I convert/create a D string from LPVOID (void*)?There is no one answer to this, but for the specific function are are looking at, the ALLOCATE_BUFFER argument means it puts the pointer in the pointer. So the way I'd do it is: char* lpMsgBuf; instead of LPVOID. You might as well keep some type info there; no need to call it VOID yet (it will implicitly cast to that when it is necessary). You still need to cast at the function call point, so the rest remains the same, but you should keep the return value of FormatMessageA. Then, you can do something like this: string s = lpMsgBuf[0 .. returned_value].idup; and it will copy it into the D string. You could also skip that ALLOCATE_BUFFER argument and pass it a buffer yourself, soemthing like: char[400] buffer; auto ret = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer.ptr, buffer.length, NULL); return buffer[0 .. ret].idup; would also work.
Oct 16 2017
On Monday, 16 October 2017 at 22:54:32 UTC, Adam D. Ruppe wrote:On Monday, 16 October 2017 at 21:48:35 UTC, Nieto wrote:If you will not use this buffer in any other way but as an immutable string slightly better way is to: import std.exception : assumeUnique; return assumeUnique(buffer[0..ret]); This will not allocate another buffer only to copy data to it as immutable.How do I convert/create a D string from LPVOID (void*)?There is no one answer to this, but for the specific function are are looking at, the ALLOCATE_BUFFER argument means it puts the pointer in the pointer. So the way I'd do it is: char* lpMsgBuf; instead of LPVOID. You might as well keep some type info there; no need to call it VOID yet (it will implicitly cast to that when it is necessary). You still need to cast at the function call point, so the rest remains the same, but you should keep the return value of FormatMessageA. Then, you can do something like this: string s = lpMsgBuf[0 .. returned_value].idup; and it will copy it into the D string. You could also skip that ALLOCATE_BUFFER argument and pass it a buffer yourself, soemthing like: char[400] buffer; auto ret = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer.ptr, buffer.length, NULL); return buffer[0 .. ret].idup; would also work.
Oct 19 2017
string GetLastErrorMessage() { wchar* lpMsgBuf; DWORD errorMessageID = GetLastError(); uint len=FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), cast(LPWSTR) &lpMsgBuf, 0, NULL); string msg=lpMsgBuf[0..len].to!string; LocalFree(lpMsgBuf); return msg; }
Oct 19 2017
On Monday, 16 October 2017 at 21:48:35 UTC, Nieto wrote:How do I convert/create a D string from LPVOID (void*)?If you're using this solely for Windows error messages, the std.windows.syserror module has a function, sysErrorString, which wraps it up for you. It also provides a WindowsException you can throw which, given an Windows error code, will contain a formatted system error message. https://dlang.org/phobos/std_windows_syserror.htmlstring GetLastErrorMessage() { LPVOID lpMsgBuf; DWORD errorMessageID = GetLastError(); FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), cast(LPSTR) &lpMsgBuf, 0, NULL); return ?? }
Oct 19 2017
Thanks for all answers guys.If you're using this solely for Windows error messages, the std.windows.syserror module has a function, sysErrorString, which wraps it up for you. It also provides a WindowsException you can throw which, given an Windows error code, will contain a formatted system error message. https://dlang.org/phobos/std_windows_syserror.htmlYep, I was using solely for Windows error messages. In fact, I was trying to reinvent the wheel of wenforce(). Thanks for point out this. I should have asked if there was a native function for this rather go on and write one myself...
Oct 21 2017