这是我目前的代码:
#include <Windows.h>
DWORD run_portable_executable(unsigned char* binary)
{
BOOL success;
const DWORD binary_address = (DWORD)binary;
IMAGE_DOS_HEADER* const dos_header = (LPVOID)binary;
IMAGE_NT_HEADERS* const nt_header = (LPVOID)(binary_address + dos_header->e_lfanew);
STARTUPINFOW startup_info;
PROCESS_INFORMATION process_info;
// Zero the structs to ensure valid values.
SecureZeroMemory(&startup_info, sizeof(startup_info));
SecureZeroMemory(&process_info, sizeof(process_info));
WCHAR current_file_path[MAX_PATH];
GetModuleFileNameW(NULL, current_file_path, MAX_PATH);
// Use the current executable as a dummy process to be taken over by the binary.
success = CreateProcessW(current_file_path, NULL, NULL, NULL, FALSE,
CREATE_SUSPENDED, NULL, NULL, &startup_info, &process_info);
if (!success)
goto err; …
Run Code Online (Sandbox Code Playgroud) 我原来有以下代码,其中it
是void**
:
while (it != end)
{
*it = *(it + 1);
it++;
}
Run Code Online (Sandbox Code Playgroud)
我决定改为it + 1; it++;
,++it;
因为我两次计算相同的值.
while (it != end)
{
*it = *(++it);
}
Run Code Online (Sandbox Code Playgroud)
但是,在这样做之后,我的程序不再以相同的方式运行.
编辑:我想要注意的是,这不是i = i++;
,而是将指向的值分配给it
它前面的值.
我有一个字符串数组作为我的列之一,我想通过数组的第一个元素对结果进行排序.这是我尝试过的:
SELECT * FROM items ORDER BY aliases[0];
Run Code Online (Sandbox Code Playgroud)
这没用.怎么可能完成?
我目前正在使用Facebook的并发哈希映射,我想知道这样的事情是否可行:
folly::ConcurrentHashMap<std::string, some_class> m;
// add some elements
const auto it = m.find("a");
// during this time, another thread removes the "a" element
if (it != m.end())
it->second.something(); // it is now an invalid iterator
Run Code Online (Sandbox Code Playgroud)
在阅读了哈希映射的源代码后,我发现了这个:
迭代器持有返回元素的危险指针.只有在迭代器仍然有效时才能访问元素!
这是非常令人不安的,感觉使用任何返回的迭代器是不安全的,这是这样吗?
我已经四处查看了,没有发现C ++属性是否应该位于标头或实现中,或在两者中都存在。例如:
file.h
[[nodiscard]] std::future<int> get_data();
Run Code Online (Sandbox Code Playgroud)
file.cpp
[[nodiscard]] std::future<int> get_data() { return ...; }
Run Code Online (Sandbox Code Playgroud)
应该都具有属性还是只有一个?cppreference提供的示例都是没有前向声明的函数。
在寻找一种在内存中运行可移植可执行文件的方法后,我在大约 10 个不同的项目中偶然发现了同一段代码,它们都具有相同的黑魔法硬编码数字。我尽我所能重构它并进行一些错误处理,这是最终结果:
#include <stdint.h>
#include <Windows.h>
unsigned long run_portable_executable(unsigned char* binary)
{
int success = 1, rc = 0;
const uintptr_t binary_address = (uintptr_t)binary;
IMAGE_DOS_HEADER* const dos_header = (IMAGE_DOS_HEADER*)binary;
IMAGE_NT_HEADERS* const nt_header = (IMAGE_NT_HEADERS*)(binary_address + dos_header->e_lfanew);
if (nt_header->Signature != IMAGE_NT_SIGNATURE) {
rc = 1;
goto out;
}
STARTUPINFOW startup_info;
PROCESS_INFORMATION process_info;
SecureZeroMemory(&startup_info, sizeof(startup_info));
SecureZeroMemory(&process_info, sizeof(process_info));
wchar_t current_file_path[MAX_PATH];
GetModuleFileNameW(NULL, current_file_path, MAX_PATH);
success = CreateProcessW(current_file_path, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &startup_info, &process_info);
if (!success)
goto out;
CONTEXT* const ctx …
Run Code Online (Sandbox Code Playgroud) 我知道通常你不需要wchar_t
支持unicode用于通用代码,但是,Windows API似乎有一个ASCII或宽字符等效于所有采用字符串的方法.例如:
FindWindowA(nullptr, "File Explorer"); // ASCII version
FindWindowW(nullptr, L"File Explorer"); // Wide Character version
Run Code Online (Sandbox Code Playgroud)
我Visual Studio 2017
使用以下选项编译Properties > General
:
Character Set: Use Unicode Character Set
Run Code Online (Sandbox Code Playgroud)
另一个可用选项是:Use Multi-Byte Character Set
.
我希望能够支持带有unicode字符的文件名,我不确定是否应该使用multi-byte
字符集进行编译并使用所有宽字符方法,或者我是否应该使用unicode
字符集进行编译并使用所有ASCII方法.
我正在尝试MSVC
使用它们出现的相应版本查找功能列表,以便我可以执行以下操作:
#if defined(_MSC_VER) && _MSC_VER > X
#define MY_INLINE __forceinline
#else
#define MY_INLINE inline
#endif
Run Code Online (Sandbox Code Playgroud)
随着gcc
例如,存在的一个列表最低版本的所有属性。有类似的东西MSVC
吗?