Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

mpossible strange behaviour with sendmessage (make no sense at all)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
michelqa - 19 Jul 2008 13:29 GMT
Hi,

I use sendMessage to retrieve information from another application.

For some obscur reasons, my code work only in a button click event and
nowhere else in my application.  I mean I get unexpected result
depending where the code is placed in my application ?!?!?!?!?

Here is my code:
================================
//retrieve all line in a win32 multiline textbox
IntPtr
NbOfLines=SendMessage(1050958,EM_GETLINECOUNT,IntPtr.Zero,IntPtr.Zero);
string CtrlContent="";
// MessageBox.Show("Uncomment this line and everything work....");
for (int cnt=0;cnt<(int)NbOfLines;cnt++)
{
    StringBuilder textBuffer = new StringBuilder();
    int NbOfChars=SendMessage(1050958,EM_GETLINE,cnt,textBuffer);
    CtrlContent+=textBuffer.ToString();
}
MessageBox.Show("Content="+CtrlContent);

The preceding code placed in a button click event work
perfectly....but the same code in a TreeView afterSelect event is not
working at all, the content is always empty without any reasons...

if I put a messagebox before the loop, everything work
perfectly.....This is totally weird is anybody can explain what is
going on???

This non logic behaviour is a ***MAJOR PROBLEM*** in my application I
really need to find a way to fix this..

Thanks!
Brian Gideon - 19 Jul 2008 15:01 GMT
> Hi,
>
[quoted text clipped - 31 lines]
>
> Thanks!

This could be a timing relating problem or it could be because
SendMessage does partial messaging pumping while waiting or maybe
something entirely different.  It's going to be nearly impossible for
us to give you the answer without being able to replicate it
ourselves, but we may be able to offer clues.  Have you tried placing
the code in other locations?  How did you declare the SendMessage
function?
michelqa - 19 Jul 2008 16:14 GMT
> This could be a timing relating problem or it could be because
> SendMessage does partial messaging pumping while waiting or maybe
[quoted text clipped - 3 lines]
> the code in other locations?  How did you declare the SendMessage
> function

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(int hwnd, int msg, IntPtr
wparam, IntPtr lparam);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int msg, int wParam,
StringBuilder lParam);
const int EM_GETLINECOUNT = 0x00BA;
const int EM_GETLINE = 0x00C4;

Ok directly in Form1() function (first line of code at the very
begining of the application) it work in a c# new application.

the same thing in my application is not working....both are C#
project.

timing problem?? ...but why it work for a Button click event and not
for an afterSelect treeview event ??

..
this.InterfaceElementTreeView.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.InterfaceElementTreeView_AfterSelect);
..
this.button1.Click += new System.EventHandler(this.button1_Click);
...
private void InterfaceElementTreeView_AfterSelect(Object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
  //The code is NOT WORKING properly if placed here
}
private void button1_Click(object sender, System.EventArgs e)
{
  //The code is working properly if placed here
}
michelqa - 19 Jul 2008 20:07 GMT
Just to add more informations...

in my application a lot of SendMessage works correctly, the problem
occurs only for the EM_GETLINE send message.

so look like something strange happen when lParam is a string and not
an IntPtr
Stanimir Stoyanov - 20 Jul 2008 16:44 GMT
I believe that the issue is related to timing (as Brian suggested). The
MessageBox 'makes things work' because it gives the target application a
little time (even milliseconds) in order to respond to you. Instead of
SendMessage, try using SendMessageTimeout with a timeout of a few, say 5
seconds (5000 milliseconds, in case the target application is hung/working
on other requests at the same time):
http://www.pinvoke.net/default.aspx/user32/SendMessageTimeout.html. The
other option is to put the current thread to sleep for a little ammount of
time, e.g. System.Threading.Thread.Sleep(50);

Another issue I spot is the use of a string (immutable) which gets filled
with the StringBuilder (mutable) info inside the loop. I would suggest you
use StringBuilder in both cases.

Best Regards,
Stanimir Stoyanov | www.stoyanoff.info

> Hi,
>
[quoted text clipped - 31 lines]
>
> Thanks!
michelqa - 22 Jul 2008 21:29 GMT
> I believe that the issue is related to timing (as Brian suggested). The
> MessageBox 'makes things work' because it gives the target application a
> little time (even milliseconds) in order to respond to you. Instead of
> SendMessage, try using SendMessageTimeout with a timeout of a few, say 5
> seconds (5000 milliseconds, in case the target application is hung/working
> on other requests at the same time)

I already try and retry with SendMessageTimeout and with a sleep...I
get the same unexpected result.  The target application is not in use
by any process...

Im still tinking the way .net manage the conversion of the lparam to
string in my sendmessage is the cause of this.  Why displaying a
messagebox **before** calling SendMessage can totally change the way
my SendMessage respond ??  Now i'm looking for an alternative way to
get my string with SendMessage and looking for any global config or
parameter that can cause this impossible thing.  Is there a way to
display a messagebox without displaying it? ;o)

any other suggestion...still looking for a work around ??
michelqa - 22 Jul 2008 23:56 GMT
> Im still tinking the way .net manage the conversion of the lparam to
> string in my sendmessage is the cause of this.

But WM_GETTEXT is working correctly.... the difference is that
WM_GETTEXT use a parameter for the size of the string.

I will use WM_GETTEXT as a workaround... so thanks to everybody who
try to help.

BUT still want to understand that behaviour...dont hesitate if you
have an explanation.  Send an email to michelqa@yahoo.ca if you want
to take a look at the problem in my C# project

Thanks again.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.