Results 1 to 7 of 7

Thread: Need Help Converting from C++ to C#

  1. #1
    Registered User MQSEQ2's Avatar
    Join Date
    Oct 2003
    Posts
    910

    Need Help Converting from C++ to C#

    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;
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    92
    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++?

  3. #3
    Registered User MQSEQ2's Avatar
    Join Date
    Oct 2003
    Posts
    910
    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.

  4. #4
    Moderator
    Join Date
    Jan 2003
    Posts
    426
    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.

  5. #5
    Registered User MQSEQ2's Avatar
    Join Date
    Oct 2003
    Posts
    910
    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.

  6. #6
    Moderator
    Join Date
    Jan 2003
    Posts
    426
    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/de...rp09192002.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.
    Last edited by cavemanbob; 11-08-2003 at 03:23 AM.

  7. #7
    Registered User MQSEQ2's Avatar
    Join Date
    Oct 2003
    Posts
    910
    I will look int the P/Invoke routines to see what I can come up with.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

You may post new threads
You may post replies
You may post attachments
You may edit your posts
HTML code is Off
vB code is On
Smilies are On
[IMG] code is On