Board index » delphi » Help Needed: Delphi Strings Passed to SendMessage()

Help Needed: Delphi Strings Passed to SendMessage()

I think I'm losing strings in my calls to SendMessage() although if
that is truely the case, I would have expected AV's and I'm not
getting any.  What I do get however is nothing.

Basically what I've done is to subclass a Richedit in a remote
application.  This is the only way to get direct access to the
internal messages that are sent directly to the class.  Specifically I
needed to hook into EM_REPLACESEL which is sent whenever new text is
added to a richedit.  My subclass works fine, and in a trace log I
print out the new string being added in response to this message.  

Now, what I'm trying to do is respond by adding a line of text to the
same RichEdit whenever a particular word is encountered.  However,
what happens according to my trace log is I hit the first message, and
if the word is present I send the appropriate messages to add a second
line, but when this next line arrives, again, in the same subclassed
WndProc, the message string is empty.  (r1)

So I set out to figure out why...  Delphi's help comments that the use
of StrAlloc and StrDispose is for backwards compatibility only, but
how can we safely call SendMessage() for messages like WM_SETTEXT or
EM_REPLACESEL when the lParam is lpstr?  It's gets sticky with
Delphi's strict typechecking.  But, more importantly, if I use pchars
and try to get the memory for my strings by hand, I run into the
question of  when does it become safe to dispose of this memory?  Or
use it again?

It sounds like using Delphi's dynamics strings are the way to go if
the reference counting works when passing them to API calls, but the
compiler doesn't like:

tStrBuffer : string;
.
SendMessage(TargetHWND, WM_SETTEXT, 0, tStrBuffer);

I may be able to force it with a typecast, but so far my string hasn't
turned up.  So the larger question is why would I be losing the
string.  I get the second call to the RichEdit's EM_REPLACESEL but
lparam seems to be pointing to nothing.

Just to add clarification, I'm injecting my code into a remote address
space so all of this is all happening within the address space of a
seperate exe.  This has made it hard to debug when I hit snags in code
on that side.  But, to monitor things on that side, I put up a form
with a memo which I use to write trace strings.  Also, since this is a
remote exe, actually just a test/demo app called Target.exe, I need to
work around it and not affect its operation unnecessarily.  For
example, the target exe has a memo and an edit box, like an irc style
chat interface, and to get text into the memo you type in the edit box
then hit enter.

So, in my code I need to monitor text appearing in the target.exe's
richedit (my subclass which monitors EM_REPLACESEL) but when I want to
write my own text, I don't go around the Target.exe but I conform to
it so that internally it does what it's supposed to do.  Thus, I send
a WM_SETTEXT to the edit box followed by a WM_KEYDOWN/VK_RETU
RN message.  Then the target.exe takes it from there.  This may be
helpful to understand.  

Also, remember that (r1) back there, well notice I did indicate that
this second EM_REPLACESEL is arriving.  That's the stickler.  I am
going from EM_REPLACESEL in my subclasses WndProc for the RichEdit,
sending two messages to the Edit box, and then winding up back at
EM_REPLACESEL in the RichEdit but the string seems to be empty.  The
trace does not show this second EM_REPLACESEL message arriving when I
enter text into the RichEdit using the application (target.exe)  when
I send my messages.  So I'm assuming, as I think I can, that this
second message IS my generated message.  But where is my string?

I know this is lot, but it's some really interesting code (the
injection and all) so I'm hoping some Delphian might find this
interesting enough to help.  If you'd like to see the code I've posted
it here:

http://www.technologydynamics.com/software/remotesubclass_981105.zip

Thanks for any help,

-- Jim Burns
     Technology Dynamics
       Pearland, Texas
         281.485.7186 / 281.485.0410
           jimburns at technologydynamics dot com

 

Re:Help Needed: Delphi Strings Passed to SendMessage()


Jim,

I'm a bit busy at the moment to fully inspect your message, but the way I pass
a string to SendMessage is LPARAM(PChar(MyString)) Try that first.

Charles Johnson

Other Threads