Exemplo n.º 1
0
        public void UpdateHandle(IntPtr hWnd)
        {
            var iPID = AutoItApi.WinGetProcess(hWnd);

            //if (iPID == -1) { return _CloseHandle(); }
            if (iPID == g_iD2pid)
            {
                // Already initialized
                return;
            }

            _CloseHandle();
            g_iUpdateFailCounter += 1;
            g_ahD2Handle          = OpenProcess((int)iPID);
            if (g_ahD2Handle == IntPtr.Zero)
            {
                // https://docs.microsoft.com/en-au/windows/win32/debug/system-error-codes
                var lastWin32Error = Marshal.GetLastWin32Error();
                throw new Exception($"UpdateHandle: Couldn't open Diablo II memory handle. No Admin rights? lastWin32Error: {lastWin32Error}");
            }

            if (!UpdateDllHandles())
            {
                _CloseHandle();
                logger.Debug("UpdateHandle: Couldn't update dll handles.");
                //throw new Exception("UpdateHandle: Couldn't update dll handles.");
            }

            if (InjectFunctions() == false)
            {
                _CloseHandle();
                throw new Exception("UpdateHandle: Couldn't inject functions.");
            }

            g_iUpdateFailCounter = 0;
            g_iD2pid             = iPID;
            g_pD2sgpt            = (IntPtr)MemoryRead(g_hD2Common + 0x99E1C, g_ahD2Handle);
        }