我是一个C++初学者(3-4个月),我正试图做一个关于窗口挂钩的精益求精.我有一个DLL的错误,我正在尝试注入,一段时间后,我意识到我的DllMain没有被调用!我查看了StackOverflow上的几乎所有线程,无法弄清楚我的问题.我发现通过将变量初始化为5,在DllMain中将其更改为1并在函数中输出变量.变量永远不会改变.这是代码:
int i = 5;
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
i=1;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hDll = (HINSTANCE) hModule;
break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
case DLL_PROCESS_DETACH: break;
}
return TRUE;
}
bool InstallHook(){
cout << "INSTALLING HOOK... " << endl;
cout << i << endl;
hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC) CBTProc, hDll, 0);
return hHook != NULL;
}
Run Code Online (Sandbox Code Playgroud)
这是我加载DLL ...
typedef bool (*InstallHook)();
typedef void (*UninstallHook)();
InstallHook ih;
UninstallHook uh;
LRESULT CALLBACK …
Run Code Online (Sandbox Code Playgroud) 我正在制作一个机器人,可以在游戏中测试一些卡片策略,机器人可以工作,但我有一个问题.我的卡摇法不是很好.我这样摇动卡片:
public void ShuffelDeck()
{
for (int i = 0; i < 5; i++)
Cards = ShuffelCards(Cards);
}
private ArrayList ShuffelCards(ArrayList Cards)
{
ArrayList ShuffedCards = new ArrayList();
Random SeedCreator = new Random();
Random RandomNumberCreator = new Random(SeedCreator.Next());
while (Cards.Count != 0)
{
int RandomNumber = RandomNumberCreator.Next(0, Cards.Count);
ShuffedCards.Insert(ShuffedCards.Count, Cards[RandomNumber]);
Cards.RemoveAt(RandomNumber);
}
return ShuffedCards;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我计算卡片震动过程之间没有暂停时,一些玩家将赢得很多游戏.例如
Player 1: 8 games
Player 2: 2 games
Player 3: 0 games
Player 4: 0 games
Run Code Online (Sandbox Code Playgroud)
但是当我在卡片摇动过程之前添加一个对话框时,胜利将分散在玩家身上:
Player 1: 3 games
Player 2: 2 …
Run Code Online (Sandbox Code Playgroud) 我想测试一些纸牌游戏战术,我的目标是知道巫术战术在现实生活中会更好.为了做到这一点,我做了一个模拟,但我不知道我是否做了一个好的.所以我想知道如何创建一个好的模拟.
你不能模拟在现实生活中被压迫的每个因素,例如虚张声势.还有一些因素可以消除,我尽可能地消除了.留下的因素,如虚张声势,超出了范围.模拟那些是不可能的.
我使用我的模拟项目来比较一些战术.通过这种方式,您可以看到您可以使用哪种策略来对抗其他策略.这样做的缺点是,如果你知道其他玩家的战术,你就可以使用它.如果您不知道可以使用哪种策略,那么您需要有一台计算机来测试它.因此,您只能使用此项目来查看您的演奏风格.
这个问题不是关于创建"神战术",而是关于编写可以用来模拟例如纸牌游戏的好的模拟软件.所以我想知道如何进行良好的模拟:
我想列出指向另一个进程的进程内存中包含某个值的位置的指针.值可以是short,int,long,string,bool或其他值.
我的想法是使用泛型.我有一个问题,我怎么能告诉编译器他需要转换字节数组的类型?
这就是我所做的:
public List<IntPtr> ScanProccessFor<T>(T ItemToScanFor)
{
List<IntPtr> Output = new List<IntPtr>();
IntPtr StartOffset = SelectedProcess.MainModule.BaseAddress;
int ScanSize = SelectedProcess.MainModule.ModuleMemorySize;
for (int i = 0; i < ScanSize; i++)
if (ReadMemory(SelectedProcess, StartOffset + i, (UInt16)Marshal.SizeOf(ItemToScanFor)) == ItemToScanFor)
Output.Insert(Output.Count,StartOffset + i);
return Output;
}
Run Code Online (Sandbox Code Playgroud)
如何告诉编译器他需要将byte []转换为T类?
我正在测试DLL中的导出函数的速度和正常函数.如何在DLL中导出的函数更快?
100000000 function calls in a DLL cost: 0.572682 seconds
100000000 normal function class cost: 2.75258 seconds
Run Code Online (Sandbox Code Playgroud)
这是DLL中的函数:
extern "C" __declspec (dllexport) int example()
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这是正常的函数调用:
int example()
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这是我测试它的方式:
int main()
{
LARGE_INTEGER frequention;
LARGE_INTEGER dllCallStart,dllCallStop;
LARGE_INTEGER normalStart,normalStop;
int resultCalculation;
//Initialize the Timer
::QueryPerformanceFrequency(&frequention);
double frequency = frequention.QuadPart;
double secondsElapsedDll = 0;
double secondsElapsedNormal = 0;
//Load the Dll
HINSTANCE hDll = LoadLibraryA("example.dll");
if(!hDll)
{
cout << "Dll error!" << endl;
return 0; …
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,该字符串包含例如"Hello \n这是一个测试.\n".
我想在字符串中的每个\n上分割整个字符串.我已经制作了这个代码:
vector<string> inData = "Hello\nThis is a test.\n";
for ( int i = 0; i < (int)inData.length(); i++ )
{
if(inData.at(i) == "\n")
{
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我完成这个时,我得到一个错误:(\n as a string)
binary '==' : no operator found which takes a left-hand operand of type 'char' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)
(上面的代码)
'==' : no conversion from 'const char *' to 'int'
'==' : 'int' differs in levels of indirection from 'const char [2]'
Run Code Online (Sandbox Code Playgroud)
问题是我无法查看char是否等于"new line".我怎样才能做到这一点?
我工作的公司要求我们遵循no-loop-func ES-lint规则.我处于回调中需要循环变量的情况.
下面是一个例子:
var itemsProcessed = 0;
for (var index = 0; index < uniqueIdentifiers.length; index++) {
let uniqueIdentifier = uniqueIdentifiers[index];
// ESLint: Don't make functions within a loop (no-loop-func)
var removeFileReferenceCallback = function (removeReferenceError) {
if (removeReferenceError !== null) {
NotificationSystem.logReferenceNotRemoved(uniqueIdentifier, undefined);
}
// When all items are removed, use the callback
if (++itemsProcessed === uniqueIdentifiers.length) {
callback(null);
}
};
// Remove the reference
this.database.removeFileReference(uniqueIdentifier, removeFileReferenceCallback);
}
Run Code Online (Sandbox Code Playgroud)
如何重构代码以便满足规则?
如何匹配最后一次出现的正则表达式?
例如,我如何匹配[XXX]:
data[X][XX][XXX]
Run Code Online (Sandbox Code Playgroud)
其中X,XX,XXX可以随机分配.
我已经尝试使用否定前瞻
\[.*?\](?!.*?\[.*?\])
Run Code Online (Sandbox Code Playgroud)
但是第一个[与最后一个]将匹配不能给出正确结果的东西.
当我用"擦除"删除向量中的元素时,则没有清除内存.例如,我创建了一个大小为2000的向量.创建后,程序使用1.5 MB内存.当我进行擦除呼叫时,不会清除任何内容.所有的元素都消失了.但他们仍然记忆犹新.
例如:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//Makes a vector of 2000 items
vector<int> test(200000);
//Pause for test purpose
system("pause");
//erase all elements
test.erase(test.begin(),test.end());
//Pause for test purpose
system("pause");
return false;
}
Run Code Online (Sandbox Code Playgroud)
大小返回0.但该过程仍然使用1,5MB的内存.
可能重复:
Windows上的C++ std :: string转换问题
如何将std :: string转换为LPCSTR?
我想将窗口(WM_SETTEXT)重命名为其他内容.在一个包含新窗口名称的std :: string中.我需要将std :: string转换为"LPCTSTR",这是因为SendMessage需要"LPCTSTR"中的名称.
我无法让这个工作,有人可以帮助我将字符串转换为LPCTSTR吗?
我正在开发一个需要以下结构的项目:
2个CPP文件包含类,这些类需要H文件1中的裸函数.类和nakeds需要H文件2中的变量.
我可以拆分CPP文件,以便他们使用2个单独的文件,其中包含nakes和他们需要的变量.但我更喜欢使用这种结构.
看起来编译器跳过#ifndef命令,我对问题进行了测试:
主要:
#include <iostream>
//1>CPPFile2.obj : error LNK2005: "bool Test1" (?Test1@@3_NA) already defined in CPPFile1.obj
//1>CPPFile2.obj : error LNK2005: "bool Test2" (?Test2@@3_NA) already defined in CPPFile1.obj
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
CPPFile 1:
#include <iostream>
using namespace std;
#include "HFile1.h"
Run Code Online (Sandbox Code Playgroud)
CPPFile 2:
#include <iostream>
using namespace std;
#include "HFile2.h"
Run Code Online (Sandbox Code Playgroud)
HFile 1:
#include "HFile2.h"
#pragma once
#ifndef Name1
#define Name1
//Use test1, this workes fine
//Use test2, this workes fine
#endif
Run Code Online (Sandbox Code Playgroud)
HFile 2:
#pragma once
#ifndef Name2
#define Name2 …
Run Code Online (Sandbox Code Playgroud) 可能重复:
MySQL:确定选择了哪个数据库?
我正在尝试从其他人调试项目的一些问题.看起来我从其他数据库获取数据,但我已连接到正确的数据库.所以我想,让我们来看看我连接的数据库.
如何查看我在运行时连接的数据库?