Problem using SendMessage Win api c#

  • Thread starter Thread starter jbhan
  • Start date Start date
J

jbhan

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_FINDTEXT,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
 
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
 
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
 
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
 
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.
 
Back
Top