Thread overview
How do I convert a LPVOID (void*) to string?
Oct 16, 2017
Nieto
Oct 16, 2017
Nieto
Oct 16, 2017
Adam D. Ruppe
Oct 19, 2017
Igor
Oct 19, 2017
Kagamin
Oct 20, 2017
Mike Parker
Oct 22, 2017
Nieto
October 16, 2017
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 ??
>}
October 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*)?
>
>> 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 ??
>>}

I forget to call LocalFree(lpMsgBuf); here...
October 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.
October 19, 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:
>> 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.

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.
October 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;
>}
October 20, 2017
On Monday, 16 October 2017 at 21:48:35 UTC, Nieto wrote:
> 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 ??
>>}

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.html
October 22, 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.html

Yep, 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...