예제 #1
0
        // Convert a UTF16 string to ANSI byte array using flags
        public static unsafe int ConvertWideCharToMultiByte(char *wideCharStr,
                                                            int wideCharLen,
                                                            byte *multiByteStr,
                                                            int multiByteLen,
                                                            bool bestFit,
                                                            bool throwOnUnmappableChar)
        {
            uint flags = (bestFit ? 0 : Interop.Kernel32.WC_NO_BEST_FIT_CHARS);

            Interop.BOOL defaultCharUsed = Interop.BOOL.FALSE;
            int          ret             = Interop.Kernel32.WideCharToMultiByte(Interop.Kernel32.CP_ACP,
                                                                                flags,
                                                                                wideCharStr,
                                                                                wideCharLen,
                                                                                multiByteStr,
                                                                                multiByteLen,
                                                                                null,
                                                                                throwOnUnmappableChar ? &defaultCharUsed : null);

            if (defaultCharUsed != Interop.BOOL.FALSE)
            {
                throw new ArgumentException(SR.Interop_Marshal_Unmappable_Char);
            }

            return(ret);
        }
예제 #2
0
        internal static unsafe int StringToAnsiString(string s, byte *buffer, int bufferLength, bool bestFit = false, bool throwOnUnmappableChar = false)
        {
            Debug.Assert(bufferLength >= (s.Length + 1) * SystemMaxDBCSCharSize, "Insufficient buffer length passed to StringToAnsiString");

            int nb;

            uint flags = bestFit ? 0 : Interop.Kernel32.WC_NO_BEST_FIT_CHARS;

            Interop.BOOL defaultCharUsed = Interop.BOOL.FALSE;

            fixed(char *pwzChar = s)
            {
                nb = Interop.Kernel32.WideCharToMultiByte(
                    Interop.Kernel32.CP_ACP,
                    flags,
                    pwzChar,
                    s.Length,
                    buffer,
                    bufferLength,
                    null,
                    throwOnUnmappableChar ? &defaultCharUsed : null);
            }

            if (defaultCharUsed != Interop.BOOL.FALSE)
            {
                throw new ArgumentException(SR.Interop_Marshal_Unmappable_Char);
            }

            buffer[nb] = 0;
            return(nb);
        }
예제 #3
0
        internal static unsafe void ConvertFixedToNative(int flags, string strManaged, IntPtr pNativeBuffer, int length)
        {
            if (strManaged == null)
            {
                if (length > 0)
                {
                    *(byte *)pNativeBuffer = 0;
                }
                return;
            }

            int numChars = strManaged.Length;

            if (numChars >= length)
            {
                numChars = length - 1;
            }

            byte *buffer = (byte *)pNativeBuffer;

            // Flags defined in ILFixedCSTRMarshaler::EmitConvertContentsCLRToNative(ILCodeStream* pslILEmit).
            bool throwOnUnmappableChar = 0 != (flags >> 8);
            bool bestFit = 0 != (flags & 0xFF);

            Interop.BOOL defaultCharUsed = Interop.BOOL.FALSE;

            int cbWritten;

            fixed(char *pwzChar = strManaged)
            {
#if TARGET_WINDOWS
                cbWritten = Interop.Kernel32.WideCharToMultiByte(
                    Interop.Kernel32.CP_ACP,
                    bestFit ? 0 : Interop.Kernel32.WC_NO_BEST_FIT_CHARS,
                    pwzChar,
                    numChars,
                    buffer,
                    length,
                    null,
                    throwOnUnmappableChar ? &defaultCharUsed : null);
#else
                cbWritten = Encoding.UTF8.GetBytes(pwzChar, numChars, buffer, length);
#endif
            }

            if (defaultCharUsed != Interop.BOOL.FALSE)
            {
                throw new ArgumentException(SR.Interop_Marshal_Unmappable_Char);
            }

            if (cbWritten == (int)length)
            {
                cbWritten--;
            }

            buffer[cbWritten] = 0;
        }
예제 #4
0
 internal static Interop.Kernel32.SECURITY_ATTRIBUTES GetSecurityAttributes(
     GCHandle securityDescriptorHandle,
     Interop.BOOL inheritHandle = Interop.BOOL.FALSE)
 {
     Interop.Kernel32.SECURITY_ATTRIBUTES securityAttributes = new Interop.Kernel32.SECURITY_ATTRIBUTES();
     securityAttributes.bInheritHandle       = inheritHandle;
     securityAttributes.nLength              = (uint)Marshal.SizeOf(securityAttributes);
     securityAttributes.lpSecurityDescriptor = securityDescriptorHandle.AddrOfPinnedObject();
     return(securityAttributes);
 }
예제 #5
0
        private static unsafe long QueryPerformanceFrequency()
        {
            long resolution;

            Interop.BOOL result = Interop.Kernel32.QueryPerformanceFrequency(&resolution);
            // The P/Invoke is documented to never fail on Windows XP or later
            Debug.Assert(result != Interop.BOOL.FALSE);

            return(resolution);
        }
예제 #6
0
        private static unsafe long QueryPerformanceCounter()
        {
            long timestamp;

            Interop.BOOL result = Interop.Kernel32.QueryPerformanceCounter(&timestamp);
            // The P/Invoke is documented to never fail on Windows XP or later
            Debug.Assert(result != Interop.BOOL.FALSE);

            return(timestamp);
        }
예제 #7
0
            public Interop.HRESULT QueryAcceptData(IComDataObject lpdataobj, IntPtr lpcfFormat, uint reco, Interop.BOOL fReally, IntPtr hMetaPict)
            {
                Debug.WriteLineIf(RichTextDbg.TraceVerbose, "IRichTextBoxOleCallback::QueryAcceptData(reco=" + reco + ")");
                if (reco == NativeMethods.RECO_PASTE)
                {
                    DataObject dataObj = new DataObject(lpdataobj);
                    if (dataObj != null &&
                        (dataObj.GetDataPresent(DataFormats.Text) || dataObj.GetDataPresent(DataFormats.UnicodeText)))
                    {
                        return(Interop.HRESULT.S_OK);
                    }

                    return(Interop.HRESULT.E_FAIL);
                }

                return(Interop.HRESULT.E_NOTIMPL);
            }
예제 #8
0
 public Interop.HRESULT ShowContainerUI(Interop.BOOL fShow)
 {
     Debug.WriteLineIf(RichTextDbg.TraceVerbose, "IRichTextBoxOleCallback::ShowContainerUI");
     return(Interop.HRESULT.S_OK);
 }
        public static unsafe IDictionary GetEnvironmentVariables()
        {
            // Format for GetEnvironmentStrings is:
            //     [=HiddenVar=value\0]* [Variable=value\0]* \0
            // See the description of Environment Blocks in MSDN's CreateProcess
            // page (null-terminated array of null-terminated strings). Note
            // the =HiddenVar's aren't always at the beginning.

            // Copy strings out, parsing into pairs and inserting into the table.
            // The first few environment variable entries start with an '='.
            // The current working directory of every drive (except for those drives
            // you haven't cd'ed into in your DOS window) are stored in the
            // environment block (as =C:=pwd) and the program's exit code is
            // as well (=ExitCode=00000000).

            char *stringPtr = Interop.Kernel32.GetEnvironmentStringsW();

            if (stringPtr == null)
            {
                throw new OutOfMemoryException();
            }

            try
            {
                var results = new Hashtable();

                char *currentPtr = stringPtr;
                while (true)
                {
                    ReadOnlySpan <char> variable = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(currentPtr);
                    if (variable.IsEmpty)
                    {
                        break;
                    }

                    // Find the = separating the key and value. We skip entries that begin with =.  We also skip entries that don't
                    // have =, which can happen on some older OSes when the environment block gets corrupted.
                    int i = variable.IndexOf('=');
                    if (i > 0)
                    {
                        // Add the key and value.
                        string key   = new string(variable.Slice(0, i));
                        string value = new string(variable.Slice(i + 1));
                        try
                        {
                            // Add may throw if the environment block was corrupted leading to duplicate entries.
                            // We allow such throws and eat them (rather than proactively checking for duplication)
                            // to provide a non-fatal notification about the corruption.
                            results.Add(key, value);
                        }
                        catch (ArgumentException) { }
                    }

                    // Move to the end of this variable, after its terminator.
                    currentPtr += variable.Length + 1;
                }

                return(results);
            }
            finally
            {
                Interop.BOOL success = Interop.Kernel32.FreeEnvironmentStringsW(stringPtr);
                Debug.Assert(success != Interop.BOOL.FALSE);
            }
        }
예제 #10
0
        private uint Detour_SleepEx(uint milliSeconds, Interop.BOOL alertable)
        {
            SpeedHack This = (SpeedHack)HookRuntimeInfo.Callback;

            return(Interop.Kernel32.SleepEx((uint)(milliSeconds / This._acceleration), alertable));
        }
 private static extern void GetEnumValuesAndNames(QCallTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, Interop.BOOL getNames);
예제 #12
0
 public void Save(Interop.Ole32.IStream pstm, Interop.BOOL fClearDirty)
 {
     throw new NotImplementedException();
 }
예제 #13
0
 public static bool IsFalse(this Interop.BOOL b) => b == Interop.BOOL.FALSE;
예제 #14
0
 public static bool IsTrue(this Interop.BOOL b) => b != Interop.BOOL.FALSE;
예제 #15
0
 public Interop.HRESULT ContextSensitiveHelp(Interop.BOOL fEnterMode)
 {
     Debug.WriteLineIf(RichTextDbg.TraceVerbose, "IRichTextBoxOleCallback::ContextSensitiveHelp");
     return(Interop.HRESULT.E_NOTIMPL);
 }
예제 #16
0
 public Interop.HRESULT GetDragDropEffect(Interop.BOOL fDrag, int grfKeyState, ref int pdwEffect)
 {
     pdwEffect = (int)DragDropEffects.None;
     return(Interop.HRESULT.S_OK);
 }
예제 #17
0
 internal static extern int RhpStartNoGCRegion(long totalSize, Interop.BOOL hasLohSize, long lohSize, Interop.BOOL disallowFullBlockingGC);