c# implementation of TlHelp32.h

-2

I am trying to implement c++'s functionality in c#. Particularly - Tlhelp library from kernel32.dll But it gives me a strange error - https://prnt.sc/tvn79h I can't find why it would do a thing like it, because in c++ it works fine.

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace ConsoleApp2
{
    public class LPPROCESSENTRY32
    {
        public int dwSize;
        public int cntUsage;
        public int th32ProcessID;
        public IntPtr th32DefaultHeapID;
        public int th32ModuleID;
        public int cntThreads;
        public int th32ParentProcessID;
        public long PriClassBase;
        public int dwFlags;
        public string szExeFile;    // Should be char* but I couldn't get it :(
                                    // All DWORDS are int's here
    }

    class Program
    {
        [DllImport("Kernel32.dll")]
        public static extern IntPtr CreateToolhelp32Snapshot(int dwFlags, int th32ProcessID);
        [DllImport("Kernel32.dll")]
        public static extern bool Process32First(IntPtr hSnapshot, LPPROCESSENTRY32 lppe);
        [DllImport("Kernel32.dll")]
        public static extern bool Process32Next(IntPtr hSnapshot, LPPROCESSENTRY32 lppe);
        static void Main(string[] args)
        {
            IntPtr snap = CreateToolhelp32Snapshot(0x00000002, 0); // 0x00000002 = th32cs_snapprocess
            LPPROCESSENTRY32 pe = new LPPROCESSENTRY32();

            if(Process32First(snap,pe))
            {
                while(Process32Next(snap, pe))
                {
                    Console.WriteLine(pe.szExeFile);
                }
            }
        }
    }
}

Edit#1: Actually, there's error in the console - https://prnt.sc/tvnkbv

c#
dll
asked on Stack Overflow Aug 7, 2020 by Raitis • edited Aug 7, 2020 by Raitis

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0