Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / MFC

Sending and posting CString to windows via PostMessage, SendMessage

Rate me:
Please Sign up or sign in to vote.
4.89/5 (13 votes)
1 Sep 20013 min read 1.5M   2.7K   32  
A robust mechanism for sending CString objects to windows within the current process.
// MessageString.h: interface for the CMessageString class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MESSAGESTRING_H__8D4B2F14_C5DB_49C0_B7C1_2E4B3D22440E__INCLUDED_)
#define AFX_MESSAGESTRING_H__8D4B2F14_C5DB_49C0_B7C1_2E4B3D22440E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "MessageStringManager.h"

// this is the result we will return from SendMessage
// if the target window is not in this process
#define MESSAGESTRING_NOT_SENT			-99999

// this is the result returned by receivers if an invalid
// CMessageString is send
#define MESSAGESTRING_NOT_RECEIVED		-99998

class CMessageString : public CString
{
public:
	CMessageString();
	CMessageString(const CString& string);
	CMessageString( TCHAR ch, int nRepeat = 1 );
	CMessageString( LPCTSTR lpch, int nLength );
	CMessageString( const unsigned char* psz );
	CMessageString( LPCWSTR lpsz );
	CMessageString( LPCSTR lpsz );
	virtual ~CMessageString();

public:
	void ForceCleanup();
	static BOOL IsStringValid(CMessageString* pStringToCheck);
	LRESULT SendAsWParam(HWND hwndTarget, UINT uiMessage, LPARAM lParam = NULL);
	BOOL PostAsWParam(HWND hwndTarget, UINT uiMessage, LPARAM lParam = NULL);

	LRESULT SendAsLParam(HWND hwndTarget, UINT uiMessage, WPARAM wParam = NULL);
	BOOL PostAsLParam(HWND hwndTarget, UINT uiMessage, WPARAM wParam = NULL);

protected:
	BOOL IsWindowInThisProcess(HWND hWnd);
	static CMessageStringManager m_Manager;
};

#endif // !defined(AFX_MESSAGESTRING_H__8D4B2F14_C5DB_49C0_B7C1_2E4B3D22440E__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions