By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

Problem using SendMessage Win api c#

P: n/a
Hi,

I am trying to find text in a window. I can get the handle to the
window but when I try using SendMessage the return value is very large
number. When I use Spy++ to see the return value it is FFFFFFFF. I
don't think SendMessage is working correctly to find the text. Can
somebody help me out here with the parameters that need to be passed
and if I am passing them correctly.

How do i specify the pointer to string member in the FindText
structure in C#?

My code is as follows:
public long FindTextW()
{
RichEdit.FindText tEx;

sText = "John\0";
IntPtr lstrPtr = Marshal.StringToCoTaskMemAuto(sText);;
//tEx is the FINDTEXT structure
tEx.lpstrText =lstrPtr;
tEx.chrg = tCR;
tEx.chrg.cpMax = -1;
tEx.chrg.cpMin = 0;
IntPtr lPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(tEx));
Marshal.StructureToPtr(tEx, lPtr, true);
lR = RichEdit.SendMessage((IntPtr)m_hWnd,RichEdit.EM_FI NDTEXT,0,lPtr);
return lR;
}

The value I get for lR is 9222812406911074303 which doesn't seem
right!!

Any help will be really appreciated!
Thanks,
jbhan
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The value I get for lR is 9222812406911074303 which doesn't seem
right!!


Then you've probably declared the return type as long, which is
incorrect. It should be more like

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam,
IntPtr lParam);

Also, I don't thing Windows will marshal the parameters of this
message cross process boundaries, so you can probably only expect it
to work if you send it to a control in your own process.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2

P: n/a
Thanks for the info. I changed the return type for SendMessage to IntPtr and I get a return value of -1
I am basically trying to FindText in an open Outlook window (I am able to get the handle to the window) from my windows application.I can even get the total number of text lines in the Outlook window using SendMessage. But the same api fails for FINDTEXT
Do you have an idea what is wrong with my parameters and if there is any other way I can pass them

Really appreciate your help

Thanks
jbha
Nov 15 '05 #3

P: n/a
Do you have an idea what is wrong with my parameters and if there is any other way I can pass them?


There's probably nothing wrong with your parameters. It just doesn't
work to send this message cross process boundaries. You'd have to get
code running inside the Outlook process one way or another to do this.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #4

P: n/a
I am running this code in an Outlook addin, so I presume it is running in the same process as Outlook
Even there I get a constant error "Object reference not set to an instance of an object"
What am I missing here?
Any pointers would be really great

Thanks
jbhan.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.