c++ 在main()[closed]中声明函数时出现块作用域错误

eblbsuwk  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(161)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由错字或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
一年前关闭。
这篇文章是编辑并提交审查7天前.
Improve this question
我正在尝试创建一个程序,它将为一堆消息框生成随机数,然后蓝屏。不要担心,它实际上不会伤害任何人的文件或程序。我一直无法运行我的程序,因为WinEventProc有一个错误,说a block-scope function may only have extern storage class。我一直在main内部声明我的函数。换句话说,我一直把我的整个程序放在main()里面,所以我想知道我是否不能这样做。

  1. #include <iostream>
  2. #include <random>
  3. #include <Windows.h>
  4. #include <winternl.h>
  5. #include "Uh_Oh.h"
  6. using namespace std;
  7. typedef NTSTATUS(NTAPI* pdef_NtRaiseHardError)(NTSTATUS ErrorStatus, ULONG NumberOfParameters, ULONG UnicodeStringParameterMask OPTIONAL, PULONG_PTR Parameters, ULONG ResponseOption, PULONG Response);
  8. typedef NTSTATUS(NTAPI* pdef_RtlAdjustPrivilege)(ULONG Privilege, BOOLEAN Enable, BOOLEAN CurrentThread, PBOOLEAN Enabled);
  9. int main()
  10. {
  11. // Random #'s generated and put in variables
  12. random_device dev;
  13. mt19937 rng(dev());
  14. uniform_int_distribution<std::mt19937::result_type> distx(1, 1920); // distribution for x in range [1, 1920]
  15. uniform_int_distribution<std::mt19937::result_type> disty(1, 1080); // distribution for y in range [1, 1080]
  16. int randomx;
  17. int randomy;
  18. randomx = distx(rng);
  19. randomy = disty(rng);
  20. thread_local int MsgBox_X;
  21. thread_local int MsgBox_Y;
  22. static void CALLBACK WinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
  23. {
  24. SetWindowPos(hwnd, NULL, MsgBox_X, MsgBox_Y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  25. }
  26. int MessageBoxPos(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType, int X, int Y)
  27. {
  28. HWINEVENTHOOK hHook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL, &WinEventProc, GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_INCONTEXT);
  29. MsgBox_X = X;
  30. MsgBox_Y = Y;
  31. int result = MessageBox(hWnd, lpText, lpCaption, uType);
  32. if (hHook) UnhookWinEvent(hHook);
  33. return result;
  34. }
  35. void _MessageBox()
  36. {
  37. UhOh = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  38. // ... call same MessageBoxPos function a bunch of more times
  39. // UhOh2
  40. // UhOh3
  41. }
  42. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  43. typedef NTSTATUS(NTAPI* pdef_NtRaiseHardError)(NTSTATUS ErrorStatus, ULONG NumberOfParameters, ULONG UnicodeStringParameterMask OPTIONAL, PULONG_PTR Parameters, ULONG ResponseOption, PULONG Response);
  44. typedef NTSTATUS(NTAPI* pdef_RtlAdjustPrivilege)(ULONG Privilege, BOOLEAN Enable, BOOLEAN CurrentThread, PBOOLEAN Enabled);
  45. BOOLEAN bEnabled;
  46. ULONG uResp;
  47. LPVOID lpFuncAddress = GetProcAddress(LoadLibraryA("ntdll.dll"), "RtlAdjustPrivilege");
  48. LPVOID lpFuncAddress2 = GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtRaiseHardError");
  49. pdef_RtlAdjustPrivilege NtCall = (pdef_RtlAdjustPrivilege)lpFuncAddress;
  50. pdef_NtRaiseHardError NtCall2 = (pdef_NtRaiseHardError)lpFuncAddress2;
  51. NTSTATUS NtRet = NtCall(19, TRUE, FALSE, &bEnabled);
  52. NtCall2(STATUS_FLOAT_MULTIPLE_FAULTS, 0, 0, 0, 6, &uResp);
  53. return 0;
  54. }

字符串

uklbhaso

uklbhaso1#

你必须移动所有这些代码:

  1. static void CALLBACK WinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
  2. {
  3. SetWindowPos(hwnd, NULL, MsgBox_X, MsgBox_Y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  4. }
  5. int MessageBoxPos(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType, int X, int Y)
  6. {
  7. HWINEVENTHOOK hHook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL, &WinEventProc, GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_INCONTEXT);
  8. MsgBox_X = X;
  9. MsgBox_Y = Y;
  10. int result = MessageBox(hWnd, lpText, lpCaption, uType);
  11. if (hHook) UnhookWinEvent(hHook);
  12. return result;
  13. }
  14. void _MessageBox()
  15. {
  16. UhOh = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  17. UhOh2 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  18. UhOh3 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  19. UhOh4 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  20. UhOh5 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  21. UhOh6 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  22. UhOh7 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  23. UhOh8 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  24. UhOh9 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  25. UhOh10 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  26. UhOh11 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  27. UhOh12 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  28. UhOh13 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  29. UhOh14 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  30. UhOh15 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  31. UhOh16 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  32. UhOh17 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  33. UhOh18 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  34. UhOh19 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  35. UhOh20 = MessageBoxPos(NULL, TEXT("Uh Oh!"), TEXT("Your computer ran out of memory! Windows will now crash."), MB_ICONERROR | MB_OK, randomx, randomy);
  36. }

字符串
行前

  1. int main()


不能在其他函数中定义函数。

展开查看全部

相关问题