patterncppMinor
Resolving a link - follow-up
Viewed 0 times
linkfollowresolving
Problem
Similar piece of code to that I recently posted as:
Resolving a link
I have another piece of code which cannot be as easily extracted out into a method:
```
#define THROW_LAST_WINDOWS_ERROR()\
WindowsApi::Exception::Throw(::GetLastError(), __FILE__, __LINE__)
#define THROW_MANUAL_WINDOWS_ERROR(x)\
WindowsApi::Exception::Throw(x, __FILE__, __LINE__)
Process CreateNormalProcess(
ProcessSnapshot *parent,
const UNICODE_STRING& name,
const unsigned __int32 pid,
const std::vector& threads
)
{
std::wstring nameStr(name.Buffer, name.Length/sizeof(wchar_t));
std::wstring commandLine;
std::wstring mainModulePath;
std::wstring error;
std::vector modules;
try
{
using WindowsApi::Dll::NtDll;
using WindowsApi::AutoArray;
HANDLE hProc = OpenProcess(
PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE,
pid);
if (hProc == 0)
{
THROW_LAST_WINDOWS_ERROR();
}
//Populate the process environment block
NtDll ntDll;
PEB peb;
AutoArray procInfoBuf = ntDll.NtQueryInformationProcess(
ProcessBasicInformation,
hProc,
sizeof(PROCESS_BASIC_INFORMATION)
);
BOOL rpmError = ReadProcessMemory(
hProc,
procInfoBuf.GetAs()->PebBaseAddress,
&peb,
sizeof(peb),
0);
if (rpmError == 0)
{
THROW_LAST_WINDOWS_ERROR();
}
RTL_USER_PROCESS_PARAMETERS procParameters;
rpmError = ReadProcessMemory(hProc,
peb.ProcessParameters,
&procParameters,
sizeof(procParameters),
0);
if (rpmError == 0)
{
THROW_LAST_WINDOWS_ERROR();
}
commandLine.assign(ReadRemoteUnicodeString(hProc,
procParameters.CommandLine));
mainModulePath.assign(ReadRemoteUnicodeString(hProc,
Resolving a link
I have another piece of code which cannot be as easily extracted out into a method:
```
#define THROW_LAST_WINDOWS_ERROR()\
WindowsApi::Exception::Throw(::GetLastError(), __FILE__, __LINE__)
#define THROW_MANUAL_WINDOWS_ERROR(x)\
WindowsApi::Exception::Throw(x, __FILE__, __LINE__)
Process CreateNormalProcess(
ProcessSnapshot *parent,
const UNICODE_STRING& name,
const unsigned __int32 pid,
const std::vector& threads
)
{
std::wstring nameStr(name.Buffer, name.Length/sizeof(wchar_t));
std::wstring commandLine;
std::wstring mainModulePath;
std::wstring error;
std::vector modules;
try
{
using WindowsApi::Dll::NtDll;
using WindowsApi::AutoArray;
HANDLE hProc = OpenProcess(
PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE,
pid);
if (hProc == 0)
{
THROW_LAST_WINDOWS_ERROR();
}
//Populate the process environment block
NtDll ntDll;
PEB peb;
AutoArray procInfoBuf = ntDll.NtQueryInformationProcess(
ProcessBasicInformation,
hProc,
sizeof(PROCESS_BASIC_INFORMATION)
);
BOOL rpmError = ReadProcessMemory(
hProc,
procInfoBuf.GetAs()->PebBaseAddress,
&peb,
sizeof(peb),
0);
if (rpmError == 0)
{
THROW_LAST_WINDOWS_ERROR();
}
RTL_USER_PROCESS_PARAMETERS procParameters;
rpmError = ReadProcessMemory(hProc,
peb.ProcessParameters,
&procParameters,
sizeof(procParameters),
0);
if (rpmError == 0)
{
THROW_LAST_WINDOWS_ERROR();
}
commandLine.assign(ReadRemoteUnicodeString(hProc,
procParameters.CommandLine));
mainModulePath.assign(ReadRemoteUnicodeString(hProc,
Solution
If the question is how to take something like this:
And reduce it to a one-liner, I usually use a
HANDLE hProc = OpenProcess(
PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE,
pid);
if (hProc == 0)
{
THROW_LAST_WINDOWS_ERROR();
}And reduce it to a one-liner, I usually use a
verify macro:template RetVal Verify(Eval eval, static const string& file, unsigned line)
{
if( !eval )
throw MyException(error_string, file, line);
else
return eval;
}
#define verify(EVAL) (Verify(EVAL, __FILE__, __LINE__))
// ...
HANDLE hProc = verify( OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid ));Code Snippets
HANDLE hProc = OpenProcess(
PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE,
pid);
if (hProc == 0)
{
THROW_LAST_WINDOWS_ERROR();
}template<class Eval> RetVal Verify(Eval eval, static const string& file, unsigned line)
{
if( !eval )
throw MyException(error_string, file, line);
else
return eval;
}
#define verify(EVAL) (Verify(EVAL, __FILE__, __LINE__))
// ...
HANDLE hProc = verify( OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid ));Context
StackExchange Code Review Q#281, answer score: 8
Revisions (0)
No revisions yet.