主题:[讨论]C#如何用writeprocessmemory写float到内存?
我定义了一个writeprocessmemory的声明,如下
//BOOL WriteProcessMemory(
// HANDLE hProcess, // handle to process whose memory is written to
// LPVOID lpBaseAddress, // address to start writing to
// LPVOID lpBuffer, // pointer to buffer to write data to
// DWORD nSize, // number of bytes to write
// LPDWORD lpNumberOfBytesWritten // actual number of bytes written
// );
[DllImport("kernel32.dll")]
static extern int WriteProcessMemory(int hProcess, IntPtr lpBaseAddress, [In, Out] byte[] lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
现在想将float i=8f;写入内存
调用Writefloat(address,4,i);
public void Writefloat(int address, int length,float bf)
{
if (length > 4)
length = 4;
if (length < 1)
length = 4;
buffer = new byte[length];
string stringbf = bf.ToString();
buffer = System.Text.Encoding.ASCII.GetBytes(stringbf.ToCharArray);
WriteProcessMemory(hProcess, (IntPtr)address, buffer, length, ref lpNumberOfBytesWritten); //写内存
}
现在问题好像出在float类型到byte[]类型的转换上。
float 8转换后,正常到内存应该是00 00 00 41
而上面的buffer得到的值却不是00 00 00 41
请问该如何将一个float类型数值,正确写入内存
//BOOL WriteProcessMemory(
// HANDLE hProcess, // handle to process whose memory is written to
// LPVOID lpBaseAddress, // address to start writing to
// LPVOID lpBuffer, // pointer to buffer to write data to
// DWORD nSize, // number of bytes to write
// LPDWORD lpNumberOfBytesWritten // actual number of bytes written
// );
[DllImport("kernel32.dll")]
static extern int WriteProcessMemory(int hProcess, IntPtr lpBaseAddress, [In, Out] byte[] lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
现在想将float i=8f;写入内存
调用Writefloat(address,4,i);
public void Writefloat(int address, int length,float bf)
{
if (length > 4)
length = 4;
if (length < 1)
length = 4;
buffer = new byte[length];
string stringbf = bf.ToString();
buffer = System.Text.Encoding.ASCII.GetBytes(stringbf.ToCharArray);
WriteProcessMemory(hProcess, (IntPtr)address, buffer, length, ref lpNumberOfBytesWritten); //写内存
}
现在问题好像出在float类型到byte[]类型的转换上。
float 8转换后,正常到内存应该是00 00 00 41
而上面的buffer得到的值却不是00 00 00 41
请问该如何将一个float类型数值,正确写入内存