internal HSZ CreateStringHandle(string text) { if (text.Length > 255) { Trace.WriteLine(String.Format("A string cannot be wrapped to a DDE string because it's too long for that. It's {0} chars while the limit is 255. The original string is \"{1}\", was truncated down to the required length.", text.Length, text), "[DDE]"); text = text.Substring(0, 255); } byte[] ps = Encoding.ASCII.GetBytes(text); byte[] psz = new byte[ps.Length + 1]; ps.CopyTo(psz, 0); psz[psz.Length - 1] = 0; HSZ handle = DdeCreateStringHandle(_instance, psz, Win32Declarations.CP_WINANSI); // TODO: winneutral // Error checks if (handle == IntPtr.Zero) { throw new DdeException("Cannot create a DDE handle of the string.", GetLastError()); } DdeError dwErr = GetLastError(); if (dwErr != DdeError.DMLERR_NO_ERROR) { throw new DdeException("Cannot create a DDE handle of the string.", dwErr); } return(handle); }
/// <summary> /// Provides a human-readable string representation of a DDE raw error code. /// </summary> /// <param name="error">DDE error.</param> /// <returns>Error string.</returns> public static string ErrorToString(DdeError error) { // Return a string message, if available, or a numerical code otherwise object message; lock (_hashErrorMessages) message = _hashErrorMessages[error]; return(message != null?message.ToString() : String.Format("An unknown DDE error {0} has occured.", (uint)error)); }
/// <summary> /// Initializes the use of DDE. /// </summary> public Dde() { if (_instance != 0) { throw new InvalidOperationException("Must be uninitialized."); } CheckOwnerThread(); // Initialize as a client _ddecallback = new PFNCALLBACK(DdeCallback); DdeError dwError = DdeInitialize(ref _instance, _ddecallback, (uint)AfCmd.APPCMD_CLIENTONLY, 0u); // TODO: callback collected if (dwError != DdeError.DMLERR_NO_ERROR) { throw new DdeException("Failed to initialize DDE.", dwError); } }
/// <summary> /// Creates an exception that consists of a raw DDE error code. /// </summary> /// <param name="error">Raw error code.</param> public DdeException(DdeError error) : base(ErrorToString(error)) { _error = error; }
/// <summary> /// Creates an exception that consists of a text message and a raw DDE error code. /// </summary> /// <param name="message">Text message.</param> /// <param name="error">Raw error code.</param> public DdeException(string message, DdeError error) : base(message + " " + ErrorToString(error)) { _error = error; }