Click here to Skip to main content
11,502,559 members (66,914 online)
Click here to Skip to main content

Sending and posting CString to windows via PostMessage, SendMessage

, 1 Sep 2001 162.3K 2.2K 32
Rate this:
Please Sign up or sign in to vote.
A robust mechanism for sending CString objects to windows within the current process.

Introduction

On occasions, I have needed to send a CString object to a window in my application via SendMessage or PostMessage (usually PostMessage). I have seen this particular question posted on the message boards several times, so I decided to post the code that I use to deal with this situation.

The Problem

The problem is that for whatever reason, you need to send a message to another window in your application and that message needs to include a CString object (passed via the WPARAM or LPARAM parameters). This is problematic because you must be careful to allocate the CString object on the heap (via the new operator), and you must take care to insure that the CString object is deleted when your application is finished with it. Not only this, but potential problems exist, such as accidentally sending the same message to the target window with no CString object passed in and various other situations.

The Solution

The solution is to use a CString derived class specifically designed to deal with this situation which deals with all of the issues involved in sending CStrings via Windows messaging. The solution involves 2 classes which work together to:

  1. Ensure that CString objects are only sent to windows within the current process (sending to other processes would cause unpredictable problems)
  2. Ensure that CString objects are properly cleaned up after they have been used
  3. Ensure that if the message does not make it to the intended target, that the CString object is not leaked
  4. Ensure that the receiving window does not process the message if the CString object is invalid.
  5. Avoid over-complicating the problem with global variables, CWinApp variables, etc.

The Implementation

I have created 2 classes CMessageString (derived from CString) and CMessageStringManager (not derived from any class). CMessageStringManager is not called directly by the program, it is managed as a protected static member of the CMessageString class.

CMessageString is a public derived class of CString which implements all of the currently defined constructors for CString (from MSDN). CMessageString has a protected static member of CMessageStringManager type. CMessageStringManager consists of a protected member CPtrList, an Add method, Delete method, ForceCleanup function and IsValid function. Each time a CMessageString is constructed, the Add method of CMessageStringManager is called to add the new CMessageString object into the CPtrList. When a CMessageString is destroyed, the Delete method is called to remove it from the CPtrList. When the app closes and the CMessageStringManager class is destroyed, it ensures that there are no CMessageString objects left in the list. If there are, it deletes them and if in debug mode, notifies the programmer through the TRACE macro.

CMessageString implements 5 public methods.

  1. SendAsWParam (HWND hwndTarget, UINT uiMessage, WPARAM wParam)
  2. SendAsLParam (HWND hwndTarget, UINT uiMessage, LPARAM lParam)
  3. PostAsWParam (HWND hwndTarget, UINT uiMessage, WPARAM wParam)
  4. PostAsLParam (HWND hwndTarget, UINT uiMessage, LPARAM lParam)
  5. static IsStringValid (CMessageString* pString)
  6. ForceCleanup ()

How to use these classes

I have included a simple demo project to show how this is used. In order to use these classes in your application, first add the 4 files to your project. To see how a CMessageString is sent, look in the CStringMessageDemoView class. To see how a CMessageString is received, look in the CMainFrame class.

First, include the messagestring.h file in your stdafx.h file (or some other appropriate file, I include it here because is makes it easy to use the class throughout my program).

TO SEND A CMessageString as a message

void SendAString()
{
    CMessageString* pString = new CMessageString;
    (*pString) = "bla.bla.bla";
    pString->PostAsWParam(hwnd, ID_SOME_MESSAGE, 0);
}

TO RECEIVE A CMessageString as a message

LRESULT CSomeWindow::OnStringMessage(WPARAM wParam, LPARAM lParam)
{
    // we expect the CMessageString in the WParam paramater
    CMessageString* pString = (CMessageString*)wParam;
       
    // make sure passed string is valid
    // (this is important to prevent unexpected
    // results if an invalid string is passed)
    if (CMessageString::IsStringValid(pString))
    {
        delete pString;
    }
    else
    {
        ASSERT(FALSE);
        return MESSAGESTRING_NOT_RECEIVED;
    }
    
    return 0;
}

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

Share

About the Author

Matt Gullett
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pin
Alexandre GRANVAUD11-Dec-08 4:41
memberAlexandre GRANVAUD11-Dec-08 4:41 
GeneralThread Safety Pin
sweetfa11-Jan-07 12:39
membersweetfa11-Jan-07 12:39 
QuestionWhy CString on heap for PostMessage?? Pin
jbem6-Feb-06 5:11
memberjbem6-Feb-06 5:11 
AnswerRe: Why CString on heap for PostMessage?? Pin
Chris Meech6-Feb-06 5:43
memberChris Meech6-Feb-06 5:43 
GeneralRe: Why CString on heap for PostMessage?? Pin
jbem6-Feb-06 18:43
memberjbem6-Feb-06 18:43 
AnswerRe: Why CString on heap for PostMessage?? Pin
Cedric Moonen20-Mar-07 21:35
mvpCedric Moonen20-Mar-07 21:35 
GeneralThis worked great.... Pin
www.codeproject.com12-May-05 14:02
susswww.codeproject.com12-May-05 14:02 
Questionhow can i send message to Notepad? Pin
jags_vc28-Mar-05 18:27
memberjags_vc28-Mar-05 18:27 
Generalwww.31show.com/ Pin
Anonymous18-Feb-05 1:23
sussAnonymous18-Feb-05 1:23 
Generalhttp://www.31show.com/ Pin
Anonymous11-Dec-04 1:02
sussAnonymous11-Dec-04 1:02 
You of the website is very good, and I like


[URL=http://www.31show.com/]加长车[/URL]
[URL=http://www.31show.com/]房车[/URL]
[URL=http://www.31show.com/gd/]网站地图[/URL]
[URL=http://www.sea123.com/]水产[/URL]
[URL=http://www.sea123.com/]水产品[/URL]
[URL=http://www.sea123.com/gd/www/1/]海鲜 海鲜网 中国海鲜网[/URL]
[URL=http://www.sea123.com/gd/www/4/]白虾 白虾网 中国白虾网[/URL]
[URL=http://www.sea123.com/gd/www/5/]对虾 对虾网 中国对虾网[/URL]
[URL=http://www.sea123.com/gd/www/6/]南美白虾 南美白对虾 南美白对虾网 中国南美白对虾网[/URL]
[URL=http://www.sea123.com/gd/www/7/]斑节虾 斑节虾网 中国斑节虾网[/URL]
[URL=http://www.sea123.com/gd/www/8/]大闸蟹 大闸蟹网 中国大闸蟹网[/URL]
[URL=http://www.31show.com/shop/]网上开店 网上商店[/URL]
[URL=http://www.31show.com/shop/]网上购物[/URL]
[URL=http://www.31show.com/yf/gd/sms.htm]美女 自拍 自拍网 美女图片 美女网 网友自拍 美女相片 美女自拍[/URL]
[URL=http://www.31show.com/mms/]汽车信息[/URL]
[URL=http://www.31show.com/mms/info/7243-1.htm]人体艺术[/URL]
[URL=http://www.31show.com/mms/info/7244-1.htm]明星美女 美女明星[/URL]
[URL=http://www.31show.com/mms/info/5408-1.htm]旅游景点[/URL]
[URL=http://www.31show.com/mms/info/5373-1.htm]旅游[/URL]
[URL=http://www.31show.com/flower/17/]鲜花速递 鲜花速递网 网上鲜花速递 国际鲜花速递[/URL]
[URL=http://www.31show.com/flower/18/]鲜花快递 鲜花快递网 网上鲜花快递 国际鲜花快递[/URL]
[URL=http://www.31show.com/flower/19/]礼品速递网 礼品快递网 生日鲜花[/URL]
[URL=http://www.31show.com/flower/20/]鲜花网 中国鲜花 鲜花礼品网 全国鲜花网[/URL]
[URL=http://www.31show.com/flower/21/]鲜花网站 鲜花速递网站 鲜花快递网站[/URL]
[URL=http://www.31show.com/flower/22/]鲜花订购 网上鲜花订购 怎样订购鲜花[/URL]
[URL=http://www.31show.com/flower/23/]鲜花礼仪 鲜花礼仪网 鲜花礼仪公司[/URL]
[URL=http://www.31show.com/flower/24/]鲜花配送 送鲜花 鲜花订送 订鲜花[/URL]
[URL=http://www.31show.com/flower/1/]送花 网上送花 异地送花 邮政送花[/URL]
[URL=http://www.31show.com/flower/2/]订花 订花网[/URL]
[URL=http://www.31show.com/flower/2/]网络订花[/URL]
[URL=http://www.31show.com/flower/3/]网上订花[/URL]
[URL=http://www.31show.com/flower/4/]送花服务[/URL]
[URL=http://www.31show.com/flower/5/]花 花店送花 花店订花[/URL]
[URL=http://www.31show.com/flower/5/]花店[/URL]
[URL=http://www.31show.com/flower/6/]鲜花 礼品 商务礼品 时尚礼品 鲜花店 鲜花店送花 鲜花店订花[/URL]
[URL=http://www.31show.com/flower/6/]鲜花礼品[/URL]
[URL=http://www.31show.com/flower/7/]蛋糕 蛋糕店 送蛋糕[/URL]
[URL=http://www.31show.com/flower/8/]国际送花[/URL]
[URL=http://www.31show.com/flower/16/]圣诞礼物 圣诞节礼物 圣诞礼品 圣诞节送花 圣诞节订花[/URL]
[URL=http://www.31show.com/flower/16/]圣诞节[/URL]
[URL=http://www.31show.com/flower/9/]网上花店 网上鲜花店[/URL]
[URL=http://www.31show.com/yf/1.htm]服装批发 批发服装[/URL]
[URL=http://www.31show.com/yf/1.htm]服装[/URL]
[URL=http://www.31show.com/yf/2.htm]西服[/URL]
[URL=http://www.31show.com/yf/6.htm]男士 男士服装[/URL]
[URL=http://www.31show.com/yf/7.htm]男士服饰[/URL]
[URL=http://www.31show.com/yf/8.htm]西装 男士西装[/URL]
[URL=http://www.31show.com/yf/9.htm]流行服饰[/URL]
[URL=http://www.31show.com/yf/10.htm]流行服装[/URL]
[URL=http://www.31show.com/yf/11.htm]潮流服饰 潮流服装[/URL]
[URL=http://www.31show.com/flower/10/]制衣厂[/URL]
[URL=http://www.31show.com/flower/11/]服装厂[/URL]
[URL=http://www.31show.com/flower/12/]时尚服装[/URL]
[URL=http://www.31show.com/flower/13/]时尚服饰[/URL]
[URL=http://www.31show.com/flower/13/]服饰[/URL]
[URL=http://www.31show.com/flower/14/]韩国服饰 韩国服装[/URL]
[URL=http://www.31show.com/flower/15/]日本服饰 日本服装[/URL]
[URL=http://www.31show.com/mms/infosort/17_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/18_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/19_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/20_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/21_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/22_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/23_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/24_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/25_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/26_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/27_1.htm]地图[/URL]
[URL=http://www.31show.com/mms/infosort/28_1.htm]地图[/URL]
[URL=http://www.31show.com/web/gd/game.htm]游戏 小游戏 在线小游戏 游戏下载 在线游戏 单机游戏下载[/URL]
[URL=http://www.31show.com/web/gd/mm-plmm.htm]香车美女 日本美女 韩国美女 美女壁纸 卡通美女[/URL]
[URL=http://www.31show.com/web/gd/mobile-sms-mms.htm]手机 手机铃声 手机铃声下载 手机图片 手机短信[/URL]
[URL=http://www.31show.com/web/gd/movie.htm]电影 电影网 电影网站 bt电影 最新电影 宽带电影 下载电影[/URL]
[URL=http://www.31show.com/web/gd/mp3-mp4.htm]mp3 mp4 mp3下载 mp3播放器 mp3歌曲下载 mp3音乐下载[/URL]
[URL=http://www.31show.com/web/gd/music.htm]音乐网 音乐网站 音乐下载 在线音乐 流行音乐[/URL]
[URL=http://www.31show.com/web/gd/mxd.htm]冒险岛 冒险岛外挂 冒险岛下载 冒险岛补丁[/URL]
[URL=http://www.31show.com/web/gd/qq2005.htm]qq qq2005 qq2005下载 qq表情 qq游戏下载[/URL]
[URL=http://www.31show.com/web/gd/xiaoshuo.htm]小说 言情小说 武侠小说 网络小说 文学小说 小说下载[/URL]
[URL=http://www.31show.com/web/gd/xxx/1.htm]成人小说 成人论坛 成人电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/2.htm]成人 成人自拍 成人交友 成人网址[/URL]
[URL=http://www.31show.com/web/gd/xxx/3.htm]色情图片 黄色小说 色情电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/4.htm]情色论坛 情色文学 情色贴图[/URL]
[URL=http://www.31show.com/web/gd/xxx/5.htm]三级 三级片 三级电影 三级女郎[/URL]
[URL=http://www.31show.com/web/gd/xxx/6.htm]色情论坛 黄色网址 色情网[/URL]
[URL=http://www.31show.com/web/gd/xxx/7.htm]情色电影 偷窥图库 偷拍 偷拍网[/URL]
[URL=http://www.31show.com/web/gd/xxx/8.htm]捆绑图片 免费同志电影 色情小说 同志电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/9.htm]色情图 激情自拍 大奶日本妹妹 激情[/URL]
[URL=http://www.31show.com/web/gd/xxx/10.htm]封面女郎 三版女郎 床上戏 叫床声[/URL]
[URL=http://www.31show.com/web/gd/xxx/11.htm]卖春 女优 裸体图片 美女裸体图片[/URL]
[URL=http://www.31show.com/web/gd/xxx/12.htm]援助交际 做爱图片 做爱电影 春宫图片[/URL]
[URL=http://www.31show.com/web/gd/xxx/13.htm]性感美眉 少女玉体 性爱知识 性感写真[/URL]
[URL=http://www.31show.com/web/gd/xxx/14.htm]AV女郎 AV女优 性爱图片 美女性爱图片[/URL]
[URL=http://www.31show.com/web/gd/xxx/15.htm]处女图 亚洲色情 自拍论坛 性爱网址[/URL]
[URL=http://www.31show.com/web/gd/xxx/16.htm]春宫 春宫图 大奶写真 春宫画 a级电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/17.htm]色情图库 色情相片 免费色情电影 色情电影网站[/URL]
[URL=http://www.31show.com/web/gd/xxx/18.htm]一夜情 一夜情网 一夜情网站 一夜情交友[/URL]
[URL=http://www.31show.com/web/gd/xxx/19.htm]六合彩 6合彩 私彩 海南私彩[/URL]
[URL=http://www.31show.com/web/gd/xxx/20.htm]六合彩网 六合彩论坛 私彩网 海南私彩网 海南私彩论坛[/URL]
[URL=http://www.31show.com/web/gd/xxx/21.htm]美女论坛 美女图片论坛 美女诱惑论坛 日本美女论坛 欧美美女论坛[/URL]
[URL=http://www.31show.com/web/gd/xxx/22.htm]言情小说下载 席娟言情小说 言情小说集 古代言情小说[/URL]
[URL=http://www.31show.com/web/gd/xxx/23.htm]港台言情小说 言情小说网 最新言情小说 现代言情小说[/URL]
[URL=http://www.31show.com/web/gd/xxx/24.htm]校园言情小说 韩国言情小说 言情小说网站 在线言情小说 日本言情小说[/URL]
[URL=http://www.31show.com/web/gd/xxx/25.htm]武侠小说下载 成人武侠小说 武侠小说网 香艳武侠小说[/URL]
[URL=http://www.31show.com/web/gd/xxx/26.htm]美女图片 美女照片 美女图库 人造美女 美女游戏[/URL]
[URL=http://www.31show.com/web/gd/xxx/27.htm]美女乱交照片 美女兽乱交图片 美女狗乱交图 美女电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/28.htm]强奸 强奸网 强奸相片 强奸图片 强奸电影 强奸美女 强奸学生妹妹 强奸小说[/URL]
[URL=http://www.31show.com/web/gd/xxx/29.htm]美女网 动画美女 美女手机图片下载 动漫美女 美女乱交照片 美女兽乱交电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/30.htm]伦乱小说 乱伦小说 乱伦电影 刺激小说 特级电影 激情小电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/31.htm]限制级电影 限制级电影论坛 顶级电影在线播放 免费五级电影片 bt限制级电影 韩国限制级电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/32.htm]1级电影 顶级生活片电影 免费2级电影 顶级美女电影 r级电影 香港艳丽级情电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/33.htm]香港限制级电影 x级电影 2 3 4 5 6 7 8 9级电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/34.htm]I II III IIII IIIII IIIIII IIIIIII IIIIIIII级电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/35.htm]未满十八岁禁入电影 未满21岁禁入电影 禁播电影 十八岁禁入电影 中国禁播电影 世界禁播电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/36.htm]禁毁小说 未满21岁禁入小说 中国古代禁毁小说 未满十八岁禁入小说[/URL]
[URL=http://www.31show.com/web/gd/xxx/37.htm]bt色艳情电影 欧美色艳情电影 欧美色艳情图片 现代色艳情电影 现代色艳情小说[/URL]
[URL=http://www.31show.com/web/gd/xxx/38.htm]性交电影 性交小说 性交相片 性交图片 两性知识 性交注意事项[/URL]
[URL=http://www.31show.com/web/gd/xxx/39.htm]好色 教子洞房 红娘洞房花烛夜 洞房花烛夜 红娘洞房 新婚洞房 洞房游戏[/URL]
[URL=http://www.31show.com/web/gd/xxx/40.htm]高潮电影 高潮片段 高潮小说 进入高潮 达到高潮 色狼小说 色狼电影[/URL]
[URL=http://www.31show.com/web/gd/xxx/41.htm]美女按摩 色情按摩女郎 按摩小姐 按摩女信息 靓女郎[/URL]
[URL=http://www.31show.com/web/gd/xxx/42.htm]按摩女做口活 白领丽人图片 白领丽人相片 白领交友 白领丽人做爱电影[/URL]

GeneralHello Sir Little Doubt Using Send Message Pin
ThatsAlok26-Nov-04 20:37
memberThatsAlok26-Nov-04 20:37 
QuestionHow to send user defined structure using PostMessage? Pin
ledallam13-Sep-04 18:21
memberledallam13-Sep-04 18:21 
AnswerRe: How to send user defined structure using PostMessage? Pin
ThatsAlok26-Nov-04 20:17
memberThatsAlok26-Nov-04 20:17 
GeneralRe: How to send user defined structure using PostMessage? Pin
ledallam30-Nov-04 16:51
memberledallam30-Nov-04 16:51 
GeneralProblem... Pin
RobJones26-Aug-04 5:15
memberRobJones26-Aug-04 5:15 
GeneralRe: Problem... Pin
RobJones26-Aug-04 7:53
memberRobJones26-Aug-04 7:53 
Generalpostmessage problem Pin
percyvimal25-Dec-03 18:11
memberpercyvimal25-Dec-03 18:11 
GeneralGreat job.. Pin
RobJones11-Sep-03 8:55
memberRobJones11-Sep-03 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 2 Sep 2001
Article Copyright 2001 by Matt Gullett
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid