示例#1
0
        public void TestGetProcAddress()
        {
            IntPtr kernel32Dll = NativeMethodsShared.LoadLibrary("kernel32.dll");

            try
            {
                IntPtr processHandle = NativeMethodsShared.NullIntPtr;
                if (kernel32Dll != NativeMethodsShared.NullIntPtr)
                {
                    processHandle = NativeMethodsShared.GetProcAddress(kernel32Dll, "GetCurrentProcessId");
                }
                else
                {
                    Assert.True(false);
                }

                // Make sure the pointer passed back for the method is not null
                Assert.NotEqual(processHandle, NativeMethodsShared.NullIntPtr);

                //Actually call the method
                GetProcessIdDelegate processIdDelegate = (GetProcessIdDelegate)Marshal.GetDelegateForFunctionPointer(processHandle, typeof(GetProcessIdDelegate));
                uint processId = processIdDelegate();

                //Make sure the return value is the same as retrieved from the .net methods to make sure everything works
                Assert.Equal((uint)Process.GetCurrentProcess().Id, processId); // "Expected the .net processId to match the one from GetCurrentProcessId"
            }
            finally
            {
                if (kernel32Dll != NativeMethodsShared.NullIntPtr)
                {
                    NativeMethodsShared.FreeLibrary(kernel32Dll);
                }
            }
        }
示例#2
0
        private static string GetCurrentProcessArchitecture()
        {
            string str    = null;
            IntPtr module = NativeMethodsShared.LoadLibrary("kernel32.dll");

            try
            {
                if (!(module != NativeMethodsShared.NullIntPtr))
                {
                    return(str);
                }
                IntPtr procAddress = NativeMethodsShared.GetProcAddress(module, "IsWow64Process");
                if (procAddress == NativeMethodsShared.NullIntPtr)
                {
                    return("x86");
                }
                IsWow64ProcessDelegate delegateForFunctionPointer = (IsWow64ProcessDelegate)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(IsWow64ProcessDelegate));
                bool flag = false;
                if (!delegateForFunctionPointer(Process.GetCurrentProcess().Handle, out flag))
                {
                    return(str);
                }
                if (flag)
                {
                    return("x86");
                }
                NativeMethodsShared.SYSTEM_INFO lpSystemInfo = new NativeMethodsShared.SYSTEM_INFO();
                NativeMethodsShared.GetSystemInfo(ref lpSystemInfo);
                switch (lpSystemInfo.wProcessorArchitecture)
                {
                case 0:
                    return("x86");

                case 6:
                    return("IA64");

                case 9:
                    return("AMD64");
                }
                str = null;
            }
            finally
            {
                if (module != NativeMethodsShared.NullIntPtr)
                {
                    NativeMethodsShared.FreeLibrary(module);
                }
            }
            return(str);
        }
        /// <summary>
        /// Gets the processor architecture of the currently running process
        /// </summary>
        /// <returns>null if unknown architecture or error, one of the known architectures otherwise</returns>
        static private string GetCurrentProcessArchitecture()
        {
            string architecture = null;

            IntPtr kernel32Dll = NativeMethodsShared.LoadLibrary("kernel32.dll");

            try
            {
                if (kernel32Dll != NativeMethodsShared.NullIntPtr)
                {
                    // This method gets the current architecture from the currently running msbuild.
                    // If the entry point is missing, we're running on Kernel older than WinXP
                    // http://msdn.microsoft.com/en-us/library/ms684139.aspx
                    IntPtr isWow64ProcessHandle = NativeMethodsShared.GetProcAddress(kernel32Dll, "IsWow64Process");

                    if (isWow64ProcessHandle == NativeMethodsShared.NullIntPtr)
                    {
                        architecture = ProcessorArchitecture.X86;
                    }
                    else
                    {
                        // entry point present, check if running in WOW
                        IsWow64ProcessDelegate isWow64Process = (IsWow64ProcessDelegate)Marshal.GetDelegateForFunctionPointer(isWow64ProcessHandle, typeof(IsWow64ProcessDelegate));
                        bool isWow64 = false;
                        bool success = isWow64Process(Process.GetCurrentProcess().Handle, out isWow64);

                        if (success)
                        {
                            // if it's running on WOW, must be an x86 process
                            if (isWow64)
                            {
                                architecture = ProcessorArchitecture.X86;
                            }
                            else
                            {
                                // it's a native process. Check the system architecture to determine the process architecture.
                                NativeMethodsShared.SYSTEM_INFO systemInfo = new NativeMethodsShared.SYSTEM_INFO();

                                NativeMethodsShared.GetSystemInfo(ref systemInfo);

                                switch (systemInfo.wProcessorArchitecture)
                                {
                                case NativeMethodsShared.PROCESSOR_ARCHITECTURE_INTEL:
                                    architecture = ProcessorArchitecture.X86;
                                    break;

                                case NativeMethodsShared.PROCESSOR_ARCHITECTURE_AMD64:
                                    architecture = ProcessorArchitecture.AMD64;
                                    break;

                                case NativeMethodsShared.PROCESSOR_ARCHITECTURE_IA64:
                                    architecture = ProcessorArchitecture.IA64;
                                    break;

                                case NativeMethodsShared.PROCESSOR_ARCHITECTURE_ARM:
                                    architecture = ProcessorArchitecture.ARM;
                                    break;

                                // unknown architecture? return null
                                default:
                                    architecture = null;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (kernel32Dll != NativeMethodsShared.NullIntPtr)
                {
                    NativeMethodsShared.FreeLibrary(kernel32Dll);
                }
            }

            return(architecture);
        }