I have a number of working mock classes that replace actual calls to the Windows API. However, I am struggling to put together something for a function that utilises FormatMessageW. In fact the problem is more to do with how a wchar_t buffer is passed into the function.
When using the FormatMessageW function I am first declaring my buffer as follows:
wchar_t * buffer = nullptr;
I then pass the buffer by address as the lpBuffer argument (expected type is LPWSTR):
reinterpret_cast<::LPWSTR>(&buffer)
The Windows API function will automatically create a buffer of the correct size.
I go on further with the buffer by stripping out line breaks, converting from wide characters to multibyte characters etc.
In order to fully unit test the clean up of the output buffer I am attempting to mock the FormatMessageW call by having the function simply return a predefined string (which will be a member of the mock object).
To simplify the problem, the following code attempts to replicate my problem:
// represents my mock class
class mocker
{
public:
// takes a wchar_t pointer and attempts to reassign it
int mockFunction(wchar_t * buffer)
{
// assigns local copy of wchar_t pointer...
buffer = &message[0];
return message.length();
}
protected:
std::wstring message = L"test";
};
// test code
mocker mocking;
wchar_t * buffer = nullptr;
auto size = mocking.mockFunction(reinterpret_cast<wchar_t *>(&buffer));
// at this point buffer is still null
// but I want the buffer to point to L"test"
Is there a way to achieve my aim of redirecting the pointer to an existing std::wstring without changing anything but the implementation of int mockFunction(wchar_t * buffer)?
Aucun commentaire:
Enregistrer un commentaire