作者声明有一两个错误,找不出来,编译不过,求助
1.是哪个版本的VC 能编译?
2.错在哪里
#include <Windows.h>#define ALLOC(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define FREE(m) HeapFree(GetProcessHeap(), NULL, m)const LPWSTR lpSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
LPWSTR CurrentPath = NULL;BOOL _wprintf(const wchar_t *Format, ...)
{
va_list vl;
SIZE_T dwSize = 0;
BOOL STATUS = FALSE;
wchar_t *lpBuffer = '\0'; va_start(vl, Format);
dwSize = (wcslen(Format) + sizeof(vl)) * sizeof(wchar_t *);
lpBuffer = (wchar_t *)ALLOC(dwSize);
vswprintf(lpBuffer, Format, vl);
va_end(vl); STATUS = WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), lpBuffer, wcslen(lpBuffer), NULL, NULL);
FREE(lpBuffer);
return STATUS;
}BOOL PrintLastError(LPWSTR lpErrorMessage, LSTATUS dwMessageID)
{
LPWSTR lpBuffer = NULL;
DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
if (!FormatMessage(dwFlags, NULL, dwMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpBuffer, 0, NULL))
{
_wprintf(L"FormatMessage ERROR: %d\n", GetLastError());
FREE(lpBuffer);
return FALSE;
}
else
{
_wprintf(L"%s ERROR: %s\n", lpErrorMessage, lpBuffer);
FREE(lpBuffer);
return TRUE;
}
}LPWSTR GetCurrentPath()
{
const SIZE_T dwSize = (MAX_PATH * sizeof(LPWSTR)) + 1;
CurrentPath = (LPWSTR)ALLOC(dwSize);
GetModuleFileName(GetModuleHandle(NULL), CurrentPath, dwSize - 1);
return CurrentPath;
}LSTATUS RegStartup(LPWSTR lpValueName, LPWSTR lpData)
{
HKEY hKey;
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT; REG_STATUS = RegCreateKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, 0);
if(REG_STATUS == ERROR_SUCCESS)
{
REG_STATUS = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (LPBYTE)lpData, wcslen(lpData) * sizeof(LPWSTR));
RegCloseKey(hKey);
return REG_STATUS;
}
else
{
RegCloseKey(hKey);
return REG_STATUS;
}
}LSTATUS RegNotifyChange(HANDLE hEvent)
{
HKEY hKey;
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT;
const DWORD dwNotifyFilter = REG_NOTIFY_CHANGE_NAME|REG_NOTIFY_CHANGE_ATTRIBUTES|REG_NOTIFY_CHANGE_LAST_SET|REG_NOTIFY_CHANGE_SECURITY; REG_STATUS = RegOpenKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, KEY_NOTIFY, &hKey);
if(REG_STATUS != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return REG_STATUS;
} REG_STATUS = RegNotifyChangeKeyValue(hKey, TRUE, dwNotifyFilter, hEvent, TRUE);
if(REG_STATUS == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return REG_STATUS;
}
else
{
RegCloseKey(hKey);
return REG_STATUS;
}
}DWORD WINAPI RegistryPersistenceThread(LPVOID Data)
{
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT;
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if(!hEvent)
PrintLastError(L"CreateEvent", GetLastError()); REG_STATUS = RegNotifyChange(hEvent);
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"RegNotifyChange", REG_STATUS); while(WaitForSingleObject(hEvent, INFINITE) != WAIT_FAILED)
{
_wprintf(L"RegNotifyChangeKeyValue: %s\n", lpSubKey); REG_STATUS = RegStartup(L"Startup", GetCurrentPath());
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"Registry Startup", REG_STATUS);
FREE(CurrentPath); REG_STATUS = RegNotifyChange(hEvent);
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"RegNotifyChange", REG_STATUS);
} CloseHandle(hEvent);
return 0;
}BOOL wmain(void)
{
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&RegistryPersistenceThread, NULL, 0, 0);
WaitForSingleObject(hThread, INFINITE);
return FALSE;
}
1.是哪个版本的VC 能编译?
2.错在哪里
#include <Windows.h>#define ALLOC(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define FREE(m) HeapFree(GetProcessHeap(), NULL, m)const LPWSTR lpSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
LPWSTR CurrentPath = NULL;BOOL _wprintf(const wchar_t *Format, ...)
{
va_list vl;
SIZE_T dwSize = 0;
BOOL STATUS = FALSE;
wchar_t *lpBuffer = '\0'; va_start(vl, Format);
dwSize = (wcslen(Format) + sizeof(vl)) * sizeof(wchar_t *);
lpBuffer = (wchar_t *)ALLOC(dwSize);
vswprintf(lpBuffer, Format, vl);
va_end(vl); STATUS = WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), lpBuffer, wcslen(lpBuffer), NULL, NULL);
FREE(lpBuffer);
return STATUS;
}BOOL PrintLastError(LPWSTR lpErrorMessage, LSTATUS dwMessageID)
{
LPWSTR lpBuffer = NULL;
DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
if (!FormatMessage(dwFlags, NULL, dwMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpBuffer, 0, NULL))
{
_wprintf(L"FormatMessage ERROR: %d\n", GetLastError());
FREE(lpBuffer);
return FALSE;
}
else
{
_wprintf(L"%s ERROR: %s\n", lpErrorMessage, lpBuffer);
FREE(lpBuffer);
return TRUE;
}
}LPWSTR GetCurrentPath()
{
const SIZE_T dwSize = (MAX_PATH * sizeof(LPWSTR)) + 1;
CurrentPath = (LPWSTR)ALLOC(dwSize);
GetModuleFileName(GetModuleHandle(NULL), CurrentPath, dwSize - 1);
return CurrentPath;
}LSTATUS RegStartup(LPWSTR lpValueName, LPWSTR lpData)
{
HKEY hKey;
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT; REG_STATUS = RegCreateKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, 0);
if(REG_STATUS == ERROR_SUCCESS)
{
REG_STATUS = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (LPBYTE)lpData, wcslen(lpData) * sizeof(LPWSTR));
RegCloseKey(hKey);
return REG_STATUS;
}
else
{
RegCloseKey(hKey);
return REG_STATUS;
}
}LSTATUS RegNotifyChange(HANDLE hEvent)
{
HKEY hKey;
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT;
const DWORD dwNotifyFilter = REG_NOTIFY_CHANGE_NAME|REG_NOTIFY_CHANGE_ATTRIBUTES|REG_NOTIFY_CHANGE_LAST_SET|REG_NOTIFY_CHANGE_SECURITY; REG_STATUS = RegOpenKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, KEY_NOTIFY, &hKey);
if(REG_STATUS != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return REG_STATUS;
} REG_STATUS = RegNotifyChangeKeyValue(hKey, TRUE, dwNotifyFilter, hEvent, TRUE);
if(REG_STATUS == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return REG_STATUS;
}
else
{
RegCloseKey(hKey);
return REG_STATUS;
}
}DWORD WINAPI RegistryPersistenceThread(LPVOID Data)
{
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT;
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if(!hEvent)
PrintLastError(L"CreateEvent", GetLastError()); REG_STATUS = RegNotifyChange(hEvent);
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"RegNotifyChange", REG_STATUS); while(WaitForSingleObject(hEvent, INFINITE) != WAIT_FAILED)
{
_wprintf(L"RegNotifyChangeKeyValue: %s\n", lpSubKey); REG_STATUS = RegStartup(L"Startup", GetCurrentPath());
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"Registry Startup", REG_STATUS);
FREE(CurrentPath); REG_STATUS = RegNotifyChange(hEvent);
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"RegNotifyChange", REG_STATUS);
} CloseHandle(hEvent);
return 0;
}BOOL wmain(void)
{
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&RegistryPersistenceThread, NULL, 0, 0);
WaitForSingleObject(hThread, INFINITE);
return FALSE;
}
#include <Stdio.h>
#include <Windows.h>
#define LSTATUS LONG
#define ALLOC(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define FREE(m) HeapFree(GetProcessHeap(), NULL, m)const LPWSTR lpSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
LPWSTR CurrentPath = NULL;BOOL _wprintf(const wchar_t *Format, ...)
{
va_list vl;
SIZE_T dwSize = 0;
BOOL STATUS = FALSE;
wchar_t *lpBuffer = '\0';va_start(vl, Format);
dwSize = (wcslen(Format) + sizeof(vl)) * sizeof(wchar_t *);
lpBuffer = (wchar_t *)ALLOC(dwSize);
vswprintf(lpBuffer, Format, vl);
va_end(vl);STATUS = WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), lpBuffer, wcslen(lpBuffer), NULL, NULL);
FREE(lpBuffer);
return STATUS;
}
/////////////////////////////////////////////////////////////
BOOL PrintLastError(LPWSTR lpErrorMessage, LSTATUS dwMessageID)
{
LPWSTR lpBuffer = NULL;
DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;if (!FormatMessageW(dwFlags, NULL, dwMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), lpBuffer, 0, NULL))
{
_wprintf(L"FormatMessage ERROR: %d\n", GetLastError());
FREE(lpBuffer);
return FALSE;
}
else
{
_wprintf(L"%s ERROR: %s\n", lpErrorMessage, lpBuffer);
FREE(lpBuffer);
return TRUE;
}
}LPWSTR GetCurrentPath()
{
const SIZE_T dwSize = (MAX_PATH * sizeof(LPWSTR)) + 1;CurrentPath = (LPWSTR)ALLOC(dwSize);
GetModuleFileNameW(GetModuleHandle(NULL), CurrentPath, dwSize - 1);
return CurrentPath;
}LONG RegStartup(LPWSTR lpValueName, LPWSTR lpData)
{
HKEY hKey;
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT;REG_STATUS = RegCreateKeyEx(HKEY_CURRENT_USER, (char*)lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, 0);
if(REG_STATUS == ERROR_SUCCESS)
{
REG_STATUS = RegSetValueExW(hKey, lpValueName, 0, REG_SZ, (LPBYTE)lpData, wcslen(lpData) * sizeof(LPWSTR));
RegCloseKey(hKey);
return REG_STATUS;
}
else
{
RegCloseKey(hKey);
return REG_STATUS;
}
}LSTATUS RegNotifyChange(HANDLE hEvent)
{
HKEY hKey;
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT;
const DWORD dwNotifyFilter = REG_NOTIFY_CHANGE_NAME|REG_NOTIFY_CHANGE_ATTRIBUTES|REG_NOTIFY_CHANGE_LAST_SET|REG_NOTIFY_CHANGE_SECURITY; REG_STATUS = RegOpenKeyExW(HKEY_CURRENT_USER, lpSubKey, 0, KEY_NOTIFY, &hKey);
if(REG_STATUS != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return REG_STATUS;
} REG_STATUS = RegNotifyChangeKeyValue(hKey, TRUE, dwNotifyFilter, hEvent, TRUE);
if(REG_STATUS == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return REG_STATUS;
}
else
{
RegCloseKey(hKey);
return REG_STATUS;
}
}DWORD WINAPI RegistryPersistenceThread(LPVOID Data)
{
LSTATUS REG_STATUS = ERROR_REGISTRY_CORRUPT;
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);if(!hEvent)
PrintLastError(L"CreateEvent", GetLastError());REG_STATUS = RegNotifyChange(hEvent);
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"RegNotifyChange", REG_STATUS);while(WaitForSingleObject(hEvent, INFINITE) != WAIT_FAILED)
{
_wprintf(L"RegNotifyChangeKeyValue: %s\n", lpSubKey);REG_STATUS = RegStartup(L"Startup", GetCurrentPath());
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"Registry Startup", REG_STATUS);FREE(CurrentPath);REG_STATUS = RegNotifyChange(hEvent);
if(REG_STATUS != ERROR_SUCCESS)
PrintLastError(L"RegNotifyChange", REG_STATUS);
}CloseHandle(hEvent);
return 0;
}
BOOL wmain(void)
{
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&RegistryPersistenceThread, NULL, 0, 0);
WaitForSingleObject(hThread, INFINITE);
return FALSE;
}