コード例 #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);
        }
コード例 #3
0
ファイル: ComReference.cs プロジェクト: maoxingda/msbuild-1
        /// <summary>
        /// Strips type library number from a type library path (for example, "ref.dll\2" becomes "ref.dll")
        /// </summary>
        /// <param name="typeLibPath">type library path with possible typelib number appended to it</param>
        /// <returns>proper file path to the type library</returns>
        internal static string StripTypeLibNumberFromPath(string typeLibPath, FileExists fileExists)
        {
            bool lastChance = false;

            if (!string.IsNullOrEmpty(typeLibPath))
            {
                if (!fileExists(typeLibPath))
                {
                    // Strip the type library number
                    int lastSlash = typeLibPath.LastIndexOf('\\');

                    if (lastSlash != -1)
                    {
                        bool allNumbers = true;

                        for (int i = lastSlash + 1; i < typeLibPath.Length; i++)
                        {
                            if (!Char.IsDigit(typeLibPath[i]))
                            {
                                allNumbers = false;
                                break;
                            }
                        }

                        // If we had all numbers past the last slash then we're OK to strip
                        // the type library number
                        if (allNumbers)
                        {
                            typeLibPath = typeLibPath.Substring(0, lastSlash);
                            if (!fileExists(typeLibPath))
                            {
                                lastChance = true;
                            }
                        }
                        else
                        {
                            lastChance = true;
                        }
                    }
                    else
                    {
                        lastChance = true;
                    }
                }
            }

            // If we couldn't find the path directly, we'll use the same mechanism Windows uses to find
            // libraries.  LoadLibrary() will search all of the correct paths to find this module.  We can then
            // use GetModuleFileName() to determine the actual path from which the module was loaded.  This problem
            // was exposed in Vista where certain libraries are registered but are lacking paths in the registry,
            // so the old code would fail to find them on disk using the simplistic checks above.
            if (lastChance)
            {
                IntPtr libraryHandle = NativeMethodsShared.LoadLibrary(typeLibPath);
                if (IntPtr.Zero != libraryHandle)
                {
                    try
                    {
                        typeLibPath = GetModuleFileName(libraryHandle);
                    }
                    finally
                    {
                        NativeMethodsShared.FreeLibrary(libraryHandle);
                    }
                }
                else
                {
                    typeLibPath = "";
                }
            }

            return(typeLibPath);
        }
コード例 #4
0
ファイル: ComReference.cs プロジェクト: hongli051/IronyTest
        /// <summary>
        /// Strips type library number from a type library path (for example, "ref.dll\2" becomes "ref.dll")
        /// </summary>
        /// <param name="typeLibPath">type library path with possible typelib number appended to it</param>
        /// <returns>proper file path to the type library</returns>
        internal static string StripTypeLibNumberFromPath(string typeLibPath, FileExists fileExists)
        {
            bool lastChance = false;

            if (typeLibPath != null && typeLibPath.Length > 0)
            {
                if (!fileExists(typeLibPath))
                {
                    // Strip the type library number
                    int lastSlash = typeLibPath.LastIndexOf('\\');

                    if (lastSlash != -1)
                    {
                        bool allNumbers = true;

                        for (int i = lastSlash + 1; i < typeLibPath.Length; i++)
                        {
                            if (!Char.IsDigit(typeLibPath[i]))
                            {
                                allNumbers = false;
                                break;
                            }
                        }

                        // If we had all numbers past the last slash then we're OK to strip
                        // the type library number
                        if (allNumbers)
                        {
                            typeLibPath = typeLibPath.Substring(0, lastSlash);
                            if (!fileExists(typeLibPath))
                            {
                                lastChance = true;
                            }
                        }
                        else
                        {
                            lastChance = true;
                        }
                    }
                    else
                    {
                        lastChance = true;
                    }
                }
            }

            // If we couldn't find the path directly, we'll use the same mechanism Windows uses to find
            // libraries.  LoadLibrary() will search all of the correct paths to find this module.  We can then
            // use GetModuleFileName() to determine the actual path from which the module was loaded.  This problem
            // was exposed in Vista where certain libraries are registered but are lacking paths in the registry,
            // so the old code would fail to find them on disk using the simplistic checks above.
            if (lastChance)
            {
                IntPtr libraryHandle = NativeMethodsShared.LoadLibrary(typeLibPath);
                if (IntPtr.Zero != libraryHandle)
                {
                    try
                    {
                        StringBuilder sb = new StringBuilder(NativeMethodsShared.MAX_PATH);
                        System.Runtime.InteropServices.HandleRef handleRef = new System.Runtime.InteropServices.HandleRef(sb, libraryHandle);
                        int len = NativeMethodsShared.GetModuleFileName(handleRef, sb, sb.Capacity);
                        if ((len != 0) &&
                            ((uint)Marshal.GetLastWin32Error() != NativeMethodsShared.ERROR_INSUFFICIENT_BUFFER))
                        {
                            typeLibPath = sb.ToString();
                        }
                        else
                        {
                            typeLibPath = "";
                        }
                    }
                    finally
                    {
                        NativeMethodsShared.FreeLibrary(libraryHandle);
                    }
                }
                else
                {
                    typeLibPath = "";
                }
            }

            return(typeLibPath);
        }
コード例 #5
0
        /// <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);
        }