问题描述
这是api定义<DllImport("kernel32.dll")>PublicSharedFunctionWriteProcessMemory(ByValhProcessAsInteger,ByVallpBaseAddressAsInteger,ByValbufferAsByte(),ByValsizeAsInteger,ByVallpNumberOfBytesWrittenAsInteger)AsInt32EndFunction<DllImport("kernel32.dll")>PublicSharedFunctionWriteProcessMemory(ByValhProcessAsInteger,ByVallpBaseAddressAsInteger,ByValbufferAsInteger,ByValsizeAsInteger,ByVallpNumberOfBytesWrittenAsInteger)AsInt32EndFunction
下边是代码DimxrbpmcAsIntegerDimstrbpidAsInteger=5000DimstrbpmcAsString=bpmc.TextDimencbpmc()AsByte=Encoding.Unicode.GetBytes(strbpmc)Hwd=FindWindow(vbNullString,"喜喜喜喜喜喜")IfHwd=0ThenMsgBox("喜喜喜喜喜喜")ElseGetWindowThreadProcessId(Hwd,pid)hProcess=OpenProcess(PROCESS_ALL_ACCESS,False,pid)IfhProcess=0ThenMsgBox("不能读取进程",,"提示")ElseReadProcessMemory(hProcess,&HD3432C,p,4,0)'获得内存中的基址ReadProcessMemory(hProcess,p+&H1C,p1,4,0)'获得内存中的一级偏移ReadProcessMemory(hProcess,p1+&H28,p2,4,0)'环境偏移WriteProcessMemory(hProcess,p2+&H684,strbpid,4,0)ReadProcessMemory(hProcess,p2+&H814,xrbpmc,4,0)'玩家帮派名称ReadProcessMemory(hProcess,xrbpmc+&HC,xrbpmc,4,0)'玩家帮派偏移ReadProcessMemory(hProcess,xrbpmc+&H0,xrbpmc,4,0)'玩家帮派偏移ReadProcessMemory(hProcess,xrbpmc+&H0,xrbpmc,4,0)'玩家帮派偏移WriteProcessMemory(hProcess,xrbpmc+&H0,encbpmc,32,0)EndIfCloseHandle(hProcess)'读完关闭进程,防止崩溃EndIf
为什么字节型的帮派名称可以写入,整数型的帮派ID写不进去内存呢?
解决方案
解决方案二:
<DllImport("kernel32.dll")>PublicSharedFunctionWriteProcessMemory(ByValhProcessAsInteger,ByVallpBaseAddressAsInteger,ByValbufferAsByte(),ByValsizeAsInteger,ByVallpNumberOfBytesWrittenAsInteger)AsInt32EndFunction
<DllImport("kernel32.dll")>PublicSharedFunctionWriteProcessMemory(ByValhProcessAsInteger,ByVallpBaseAddressAsInteger,ByRefbufferAsInteger,ByValsizeAsInteger,ByReflpNumberOfBytesWrittenAsInteger)AsInt32EndFunction
将第3个和第5个的形参由byval改为byref后问题解决了ByVal与ByRef(默认值)这两个是子过程的参数传递时,指定参数按什么传递的ByVal(按值传递)ByRef(按地址传递)
解决方案三:
嗯,看原型BOOLWINAPIWriteProcessMemory(_In_HANDLEhProcess,_In_LPVOIDlpBaseAddress,_In_LPCVOIDlpBuffer,_In_SIZE_TnSize,_Out_SIZE_T*lpNumberOfBytesWritten);
本来就是指针。
解决方案四:
你把指针声明为整数没有问题,但是你这个整数中应该保存的是指针地址,而不是最终的整数值。