.NET 2.x X64 GetThreadContext Error 998: Invalid Access Memory Location - C#
Формулировка задачи:
Доброго времени суток.
При попытке получить контекст потока
выскакивает ошибка 998.
Вообще-то эта ошибка должна появляться в том случае, если пытаться получить контекст х86 процесса.
И так что имеется:
OS - Windows 10 TP (x64) & Visual Studio 2013 Update 3
Приложение на C# x64.
Так вот сама ошибка появляется если скомпилировать данное приложение на этом компьютере с ОС Windows 10 TP (x64).
Если скомпилировать аналогичное приложение на С++ все работает.
Если скомпилировать приложение на С# но на
Аналогичное приложение на С++ работает нормально:
Есть у кого-то какие-то соображения по этому поводу?
Листинг программы
- if (!GetThreadContext(handle, ref context))
- throw new Win32Exception();
другом компьютере (OS Windows 7 x64 & Visual Studio 2013 Update 3) - все работает
. Может какие-то настройки Visual Studio? Вот тестовый исходник на С#:
Листинг программы
- using System;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
- namespace TestGetContext
- {
- class Program
- {
- [DllImport("kernel32", SetLastError = true)]
- public static extern IntPtr OpenThread(int DesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwThreadId);
- [DllImport("kernel32", SetLastError = true)]
- public static extern uint SuspendThread(IntPtr thandle);
- [DllImport("kernel32", SetLastError = true)]
- public static extern uint ResumeThread(IntPtr thandle);
- [DllImport("kernel32", SetLastError = true)]
- public static extern bool GetThreadContext(IntPtr thandle, ref CONTEXT context);
- static void Main(string[] args)
- {
- Console.Write("Plese enter the thread Id: ");
- int id;
- if (!int.TryParse(Console.ReadLine(), out id))
- return;
- // open thread
- var handle = OpenThread(0x1FFFFF, false, id);
- try
- {
- if (handle == IntPtr.Zero)
- throw new Win32Exception();
- if (SuspendThread(handle) == 0xFFFFFFFF)
- throw new Win32Exception();
- var context = new CONTEXT { ContextFlags = 0x100001u /* CONTROL */ };
- if (!GetThreadContext(handle, ref context))
- throw new Win32Exception();
- Console.WriteLine("Rip: 0x{0:X}", context.Rip);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- ResumeThread(handle);
- Console.ReadLine();
- }
- }
- }
- [StructLayout(LayoutKind.Sequential/*, Size = 1232*/)]
- public struct CONTEXT
- {
- public ulong P1Home;
- public ulong P2Home;
- public ulong P3Home;
- public ulong P4Home;
- public ulong P5Home;
- public ulong P6Home;
- public uint ContextFlags;
- public uint MxCsr;
- public ushort SegCs;
- public ushort SegDs;
- public ushort SegEs;
- public ushort SegFs;
- public ushort SegGs;
- public ushort SegSs;
- public uint EFlags;
- public ulong Dr0;
- public ulong Dr1;
- public ulong Dr2;
- public ulong Dr3;
- public ulong Dr6;
- public ulong Dr7;
- public ulong Rax;
- public ulong Rcx;
- public ulong Rdx;
- public ulong Rbx;
- public ulong Rsp;
- public ulong Rbp;
- public ulong Rsi;
- public ulong Rdi;
- public ulong R8;
- public ulong R9;
- public ulong R10;
- public ulong R11;
- public ulong R12;
- public ulong R13;
- public ulong R14;
- public ulong R15;
- public ulong Rip;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst=976)]
- private byte[] buff;
- };
- }
Листинг программы
- #include <stdio.h>
- #include <tchar.h>
- #include <Windows.h>
- int _tmain(int argc, _TCHAR* argv[])
- {
- DWORD threadId = -1;
- printf("Please enter thread ID: ");
- scanf("%i", &threadId);
- if (threadId > 0)
- {
- HANDLE handle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadId);
- SuspendThread(handle);
- CONTEXT context;
- context.ContextFlags = CONTEXT_CONTROL;
- GetThreadContext(handle, &context);
- printf("\nRip: 0x%X\n", context.Rip);
- ResumeThread(handle);
- CloseHandle(handle);
- }
- system("pause");
- return 0;
- }
Решение задачи: «.NET 2.x X64 GetThreadContext Error 998: Invalid Access Memory Location»
textual
Листинг программы
- typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
- //
- // Register parameter home addresses.
- //
- // N.B. These fields are for convience - they could be used to extend the
- // context record in the future.
- //
- DWORD64 P1Home;
- DWORD64 P2Home;
- DWORD64 P3Home;
- DWORD64 P4Home;
- DWORD64 P5Home;
- DWORD64 P6Home;
- // ...
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д