示例#1
0
        static SysInfo()
        {
            // ' let's get some version information!

            _memInfo.dwLength = Marshal.SizeOf(_memInfo);
            GetNativeSystemInfo(ref _sysInfo);
            GlobalMemoryStatusEx(ref _memInfo);

            // ' now let's figure out how many processors we have on this system
            var    mm = new MemPtr();
            MemPtr org;
            var    lp   = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION();
            int    lRet = 0;

            SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] rets;
            int i;
            int c;

            // ' The maximum number of processors for any version of Windows is 128, we'll allocate more for extra information.
            mm.Alloc(Marshal.SizeOf(lp) * 1024);

            // ' record the original memory pointer
            org  = mm;
            lRet = (int)mm.Length();
            GetLogicalProcessorInformation(mm, ref lRet);
            c    = (int)(lRet / (double)Marshal.SizeOf(lp));
            rets = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[c];
            var loopTo = c - 1;

            for (i = 0; i <= loopTo; i++)
            {
                rets[i] = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION)Marshal.PtrToStructure(mm.Handle, typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
                mm     += Marshal.SizeOf(lp);

                // ' what we're really after are the number of cores.
                if (rets[i].Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore)
                {
                    _sysInfo.dwNumberOfProcessors += 1;
                }
            }

            // ' store that data in case we need it for later.
            _procRaw = rets;

            // ' free our unmanaged resources.
            org.Free();
        }