PROC replaceProcAddress(LPCSTR callerModule, PROC original, PROC swap){
HMODULE callerHandle = GetModuleHandleA(callerModule);
if(callerHandle == nullptr)
throw Exception(L"callerHandle is NULL in Process::replaceProcAddress");
ULONG size;
bool found = false;
PIMAGE_IMPORT_DESCRIPTOR pImageDesc = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(ImageDirectoryEntryToData(callerHandle, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size));
if(pImageDesc == nullptr)
throw Exception(L"PIMAGE_IMPORT_DESCRIPTOR is NULL in Process::replaceProcAddress()");
for(; pImageDesc->Name; pImageDesc++){
PSTR pModName = reinterpret_cast<PSTR>(reinterpret_cast<PBYTE>(callerHandle) + pImageDesc->Name);
PIMAGE_THUNK_DATA pThunkData = reinterpret_cast<PIMAGE_THUNK_DATA>(reinterpret_cast<PBYTE>(callerHandle) + pImageDesc->FirstThunk);
for(; pThunkData->u1.Function; pThunkData++){
PROC* ppOriginalFunc = reinterpret_cast<PROC*>(&pThunkData->u1.Function);
if(*ppOriginalFunc == original){
found = true;
DWORD dwOldProtect;
if(VirtualProtect(ppOriginalFunc, sizeof(swap), PAGE_WRITECOPY, &dwOldProtect)){
if(!WriteProcessMemory(getHandle(), ppOriginalFunc, &swap, sizeof(swap), NULL))
throw Exception(L"Write memory is failed for replaceProcAddress");
VirtualProtect(ppOriginalFunc, sizeof(swap), dwOldProtect, &dwOldProtect);
}
}
}
}
if(found) return swap;
throw Exception(L"Address of procedure is not found in Process::replaceProcAddress()");
}
mov dword [block], 1
, а именно по адресу block + 3. В little endian записывалось именно так, как я бы указал. Видимо для этого и существует требование к уточнению размера операнда типа память, потому что порядок байтов бывает разный bool inArray(int* arr, int need, int count){
for(int i = 0; i < count; ++i)
if(arr[i] == need)
return true;
return false;
}
int* uniq(int* arr, int size, int& newSize){
int* localArr = new int[size]{0};
for(int i = 0; i < size; ++i){
if(inArray(localArr - newSize, arr[i], size))
continue;
++newSize;
*localArr++ = arr[i];
}
return localArr - newSize;
}
int main(){
int arr[10] = {1,2,3,4,2,4,5,6,7,4};
int newSize = 0;
int* newArr = uniq(arr, sizeof(arr)/sizeof(int), newSize);
for(int i = 0; i < newSize; ++i)
cout << newArr[i] << "\n";
}
union conv{
char (*pcarr)[4];
int* pint;
};
static conv bconv;
int main(){
bconv.pint = new int(100);
cout << *((int*)bconv.pcarr) << "\n"; //int to char (*)[4]
bconv.pcarr = (char(*)[4])new char[4]{1,2,3,4};
cout << *bconv.pint << "\n"; //char (*)[4] to int
}
union conv{
unsigned char (*pcarr)[4];
int* vint;
};
static conv bconv;
int main(){
bconv.vint = new int(100);
cout << *((int*)bconv.pcarr) << "\n"; //int to char (*)[4]
bconv.pcarr = (unsigned char(*)[4])new unsigned char[4]{255,255,255,127}; //big-endian
if(INT_MAX == *bconv.vint) cout << "bingo\n";
}
class MimicBase{};
class Vampire : public MimicBase{
public:
Vampire();
Vampire(MimicBase& m) : MimicBase(m){};
};
class Elf : public MimicBase{
public:
Elf();
Elf(MimicBase& m) : MimicBase(m){};
};
MimicBase m1;
Vampire v1(m1);
MimicBase m2(v1); //Обратно
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
int* feel(const short&);
int count = 20;
int* pArrMain = feel(count);
for(int i = 0; i < count; ++i){
if(i % 10 == 0) cout << '\n';
cout << pArrMain[i] << " ";
}
}
int* feel(const short& count = 100){
if(count <= 0) throw runtime_error("Bad size");
int* pArr = new int[count];
for(int i = 0; i < count; ++i)
pArr[i] = rand() % 21;
return pArr;
}
int rows = 0, cols = 0;
cout << "Enter size (Example: 200x200)\n";
cin >> rows >> cols;
if(rows <= 0 || cols <= 0) throw runtime_error("Bad size");
int pixmap[rows][cols];
for(int i = 0; i < rows; ++i){
double position = double(i) / rows; //Вычисляем "высоту"
int color = position * 255; //и находим подходящий цвет
cout << position << " " << color << '\n';
for(int j = 0; j < cols; ++j)
pixmap[i][j] = color;
}
#include <iostream>
#include <cstdio>
using namespace std;
void main()
{
int rows = 0, cols = 0;
char* filename = "test.pgm";
cout << "Enter size and filename (Example: 200x200 mygradient.pgm)\n";
cin >> rows >> cols >> filename;
if(rows <= 0 || cols <= 0) throw runtime_error("Bad size");
FILE* pfile = fopen(filename, "w");
fprintf(pfile, "P2\n%d %d\n255\n", cols, rows);
for(int i = 0; i < rows; ++i){
double position = double(i) / rows;
int color = position * 255;
cout << position << " " << color << "\n";
for(int j = 0; j < cols; ++j)
fprintf(pfile, "%d ", color);
fprintf(pfile, "\n");
}
}
#include <iostream>
int main(){
std::cout << "Enter the number: ";
int number;
std::cin >> number;
int buffer = number;
if(number <= 0) return 1;
double multi_factor = 1;
std::cout << std::endl;
while((buffer = number * multi_factor) >= 1){
std::cout << buffer << " ";
multi_factor /= 2;
if(buffer % 2 != 0) break;
}
std::cin.get();
std::cin.get();
return 0;
}