call Module32FirstW Failed in C#

Viewed 28
     [DllImport("kernel32.dll")]
           static extern bool Module32FirstW(IntPtr hsnap, out MODULEENTRY32 m);
    [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
        public struct MODULEENTRY32
        {
            public uint dwSize=0;
            public uint th32ModuleID =0;
            public uint th32ProcessID =0;
            public uint GlblcntUsage =0;
            public uint ProccntUsage =0;
          
            public IntPtr modBaseAddr;
            public uint modBaseSize =0;
            public IntPtr hModule=(IntPtr)0;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string szModule=null;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExePath=null;
            
        } 
public static IntPtr getDLLBaseAddress(IntPtr snapHandle,string name)
            {
               
                MODULEENTRY32 me = new MODULEENTRY32();
                me.dwSize = (uint)Marshal.SizeOf(me);
                bool f= Module32FirstW(snapHandle,out me);
                int a = GetLastError();//for debug
                
                while (f) {
                    if (me.szModule.Equals(name)) return (IntPtr)ByteArrToIntPtr(me.modBaseAddr);
                    f= Module32NextW(snapHandle, out me);
                }
                return (IntPtr)(-1);
            }

When i call Module32FirstW(),It always returns false and GetError() return 24.

I guess maybe the MODULEENTRY32 going wrong,but I almost tryed all possible,still don't work.

So,I want to know what did i do wrong?Thanks.

Platform is .Net6.0 x64.

0 Answers
Related