Compiling error

I have VB/Powershell/Vbscript/Java experience, but have never written in C++. I have the following C++ code that I have procured from Microsoft, and I am getting nothing but compiling errors when trying to compile it.

I have tried DEVC++ and Visual Studio 2010. Can someone try to compile this C++ Console app and tell me what I may be doing wrong?

I am trying to use this code to run my Task Manager with seDebugPrivilege, so that I can kill processes that are running as System. I have an app that we use that keeps crashing, and the only way to get it to restart is to reboot, but we cannot reboot everyday. I would like the ability to kill it..

// EnableDebugPrivAndRun.cpp : Defines the entry point for the console application.
//

/*************************************************************
Module name: EnableDebugPrivAndRun.cpp
Notices: Written 1998 by Jeffrey Richter Modefied 2003 by Selketfoo
Description: Enables the Debug privilege before running an app
*************************************************************/


#define STRICT
//#include

#include "stdafx.h"

/*************************************************/

BOOL EnablePrivilege(HANDLE hToken, LPCTSTR szPrivName, BOOL fEnable) {
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
LookupPrivilegeValue(NULL, szPrivName, &tp.Privileges[0].Luid);
tp.Privileges[0].Attributes = ( fEnable ? SE_PRIVILEGE_ENABLED : 0 );
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
return ( (GetLastError() == ERROR_SUCCESS) );
}


int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hToken;
MessageBox(NULL, argv[1], __TEXT("EnableDebugPrivAndRun: ok"), MB_OK | MB_ICONINFORMATION);
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES,&hToken))
{
if (EnablePrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
if (ShellExecute(NULL, NULL, argv[1], NULL, NULL, SW_SHOWNORMAL) < (HINSTANCE)32) {
MessageBox(NULL, argv[1], __TEXT("EnableDebugPrivAndRun:Couldn't run"), MB_OK | MB_ICONINFORMATION);
}
}
CloseHandle(hToken);
}
return(0);
}
Why do you want to kill the processes that are running as System? Hacking?
Everyone always goes to hacking as the first question.

As stated in my post, "I have an app that we use that keeps crashing, and the only way to get it to restart is to reboot, but we cannot reboot everyday. I would like the ability to kill it.."

We use Kaseya to monitor our network infrastructure. On the 2 Server 2000 machines that we still have in our environment; when the application freezes because there are too many frozen threads in the queue, it freezes up and cannot be closed. The only way to resolve this issue and get monitoring back up is to reboot the server. I would like to kill the monitoring process "Agentmon.exe" without rebooting the machine.
So. Not a hacker. Anyone have any ideas? being a programmer, not in C++, I know that this should be an easy compling question.
Allright, sorry for lateing. I am very busy nowdays.
So, I've compiled your code on my machine with a GNU GCC compiler in Code::Blocks IDE. It doesn't find me the stdafx header. Probably it isn't provided by that standard. I replaced this line by #include<windows.h> because you are trying to do that in WINAPI, and in that case, the header below MUST be included. Then it gives me another error which is about the main function. I don't really know what are you trying to do there by writing int _tmain(...). You have to do it simple int main(...). So, with those modifications it compiles ok in my IDE. I didn't build it because I don't want to have trouble with my system because of a tiny program. This is your code with my modifications:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define STRICT
#include<windows.h>



/*************************************************/

BOOL EnablePrivilege(HANDLE hToken, LPCTSTR szPrivName, BOOL fEnable) {
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
LookupPrivilegeValue(NULL, szPrivName, &tp.Privileges[0].Luid);
tp.Privileges[0].Attributes = ( fEnable ? SE_PRIVILEGE_ENABLED : 0 );
   AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
        return ( (GetLastError() == ERROR_SUCCESS) );
}


int main(int argc, _TCHAR* argv[])
{
HANDLE hToken;
MessageBox(NULL, argv[1], __TEXT("EnableDebugPrivAndRun: ok"), MB_OK | MB_ICONINFORMATION);
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES,&hToken))
{
if (EnablePrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
if (ShellExecute(NULL, NULL, argv[1], NULL, NULL, SW_SHOWNORMAL) < (HINSTANCE)32) {
MessageBox(NULL, argv[1], __TEXT("EnableDebugPrivAndRun:Couldn't run"), MB_OK | MB_ICONINFORMATION);
}
}
CloseHandle(hToken);
}
return(0);
}
I hope that I helped you. Hint: If you didn't program in C++, don't try to make that kind of programs (those programs involve WINPI functions which means that they are working just on Windows platform, as far as I know) because WINPI has a very complex kind of functions very hard to understand and to do that (to understand them), you have to learn MANY other things about Object Orientated Programming in C++. Good luck!
Topic archived. No new replies allowed.