PDA

View Full Version : Need Help Converting from C++ to C#



MQSEQ2
11-07-2003, 04:04 PM
I need some help converting the Security Impersonation to C#.

I'm looking around in:
using System.Security;
using System.Security.Principal;
using System.Security.Permissions;

This is stumping me and is needed for the new Windows SysTray Server version. Any help would be appreiated.

==========================================

#include <aclapi.h>

bool AdjustDacl(HANDLE h, DWORD DesiredAccess)
{
// the WORLD Sid is trivial to form programmatically (S-1-1-0)
SID world = {SID_REVISION, 1, SECURITY_WORLD_SID_AUTHORITY, 0};

EXPLICIT_ACCESS ea = {
DesiredAccess,
SET_ACCESS,
NO_INHERITANCE,
{
0, NO_MULTIPLE_TRUSTEE,
TRUSTEE_IS_SID,
TRUSTEE_IS_USER,
reinterpret_cast<LPTSTR>(&world)
}
};
ACL* pdacl = 0;
DWORD err = SetEntriesInAcl(1, &ea, 0, &pdacl);
if (err == ERROR_SUCCESS)
{
err = SetSecurityInfo(h, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, 0, 0, pdacl, 0);
LocalFree(pdacl);
return(err == ERROR_SUCCESS);
}
else
return(FALSE);
}

void scanproclist ()
{
HANDLE hProcessSnap = NULL;
PROCESSENTRY32 pe32 = {0};
eqprocess = 0;

// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == INVALID_HANDLE_VALUE)
return;

// Fill in the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);

if (Process32First(hProcessSnap, &pe32))
{
HANDLE hProcess;

do
{
LPSTR pCurChar;
char pName[512];

// strip path and leave exe filename
for (pCurChar = (pe32.szExeFile + strlen (pe32.szExeFile));
*pCurChar != '\\' && pCurChar != pe32.szExeFile - 1;
--pCurChar)

strcpy(pName, pCurChar);
strlwr(pName);

if ( (strncmp (pName, "testeqgame", 10) == 0) || (strncmp (pName, "eqgame", 6) == 0) )
{
printf ("Found ProcessID: %u - Polling Cycle: %dms\n", pe32.th32ProcessID, Refresh);
hProcess = OpenProcess (PROCESS_VM_READ, FALSE, pe32.th32ProcessID);
if (hProcess == NULL)
{
HANDLE hpWriteDAC = OpenProcess(WRITE_DAC, FALSE, pe32.th32ProcessID);
if (hpWriteDAC == NULL)
{
DWORD dw;
dw = GetLastError();
printf ("OpenProcess failed DACL, error: %u\n", dw);
return;
} else {
AdjustDacl(hpWriteDAC, PROCESS_VM_READ);
DuplicateHandle(
GetCurrentProcess(),
hpWriteDAC,
GetCurrentProcess(),
&hProcess,
PROCESS_VM_READ,
FALSE,
0
);
}
}

eqprocess = hProcess;
return;
}
}
while (Process32Next(hProcessSnap, &pe32));
}

CloseHandle (hProcessSnap);
return;
}

jag111
11-07-2003, 04:13 PM
I'll admit I haven't actually looked at your new alpha server yet. But does this question imply that you're trying to write the new server in C# instead of C++?

MQSEQ2
11-07-2003, 06:25 PM
C# all the way. If I can't use VB then I might as well learn C# and do it all in it.

I might have the information after spending $120 for some books. I wight try the C# methods if I can if not I can use API calls or compile that into a DLL then call it to do it for me.

cavemanbob
11-07-2003, 11:26 PM
Use P/Invoke or use the DllImport attribute to call the API functions directly, AFAIK there isn't anything like those in .NET. P/Invoke is likely the best option, I implemented the server in C# initially using DllInport with some ugly marshalling , but had problems with it being a processor hog, mostly I attribute this to my inexperience with C# and .NET at the time, but it should be fine if done right.

MQSEQ2
11-08-2003, 12:19 AM
Do you have any of that old code? I have alot of the DLL stuff but I want to make sure I get everything. I have a class that can do the OpenProcess and ReadMemory but I cant convert the SetACL routine piece.

The ScanProcess went From:
===========================================

[DllImport("KERNEL32.DLL")]
public static extern int CreateToolhelp32Snapshot(uint flags, uint processid);

[DllImport("KERNEL32.DLL")]
public static extern int Process32First(int handle, ref ProcessEntry32 pe);

[DllImport("KERNEL32.DLL")]
public static extern int Process32Next(int handle, ref ProcessEntry32 pe);

[DllImport("KERNEL32.DLL")]
public static extern int CloseHandle(int handle);

[StructLayout(LayoutKind.Sequential)]
public struct ProcessEntry32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
public string szExeFile;
};





// Take a snapshot of all processes in the system.
int hProcessSnap = CreateToolhelp32Snapshot(2, 0);

if (hProcessSnap == 0)
return;

StringBuilder sb = new StringBuilder(" ", 256);
ProcessEntry32 pe32 = new ProcessEntry32();

pe32.szExeFile = sb.ToString();

// Fill in the size of the structure before using it.
pe32.dwSize = 548;

int retval = Process32First(hProcessSnap, ref pe32);

while(retval == 1) {
if (pe32.szExeFile == settings.EQProgram + ".exe") {
PID.Add(pe32.th32ProcessID);
}
retval = Process32Next(hProcessSnap, ref pe32);
}
CloseHandle(hProcessSnap);


===========================================


To:
===========================================
Process [] Processes = Process.GetProcessesByName(settings.EQProgram);

foreach (Process ProcessID in Processes)
PID.Add(ProcessID.Id);

===========================================
Not too bad in reduction of code.

cavemanbob
11-08-2003, 03:10 AM
I don't have access to the old one at the moment, but I can probably hack something together tomorrow to do it. The SetEntriesInAcl chunk is certainly one of the ugliest things I've seen, but this is hardly a normal thing to be doing... I think P/Invoke is the way to go in that case, I've heard it handles this kind of complex structure more easily, but I'll have to try it before I can say for sure.

EDIT: Actually look at this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp

It might be easier to just toss the security adjustment junk in a DLL if you want to use C# to do this, it may require unsafe sections otherwise.

MQSEQ2
11-08-2003, 03:22 AM
I will look int the P/Invoke routines to see what I can come up with.