I've read interesting question from msdn forum.
He is going to Send WM_GETTEXT Message to other window's child control from CLI program.
I thought it'll very helpful for my study.
I use IneropServices namespace.
It's for call unmanaged function easily.
using namespace System::Runtime::InteropServices;
using namespace System::Text;
[DllImport("User32.dll", EntryPoint = "FindWindow",CallingConvention = CallingConvention::Cdecl, CharSet = CharSet::Ansi)]
IntPtr FindWindow(
System::String^ lpClassName, System::String^
lpWindowName);
[DllImport("User32.dll", EntryPoint =
"FindWindowEx",CallingConvention = CallingConvention::Cdecl, CharSet =
CharSet::Ansi)]
IntPtr FindWindowEx( IntPtr parent, IntPtr childAfter,
System::String^ lpClassName, System::String^ lpWindowName);
[DllImport("user32.dll", EntryPoint =
"SendMessage", CallingConvention =
CallingConvention::Cdecl, CharSet = CharSet::Ansi)]
IntPtr SendMessage(IntPtr
hWnd, System::UInt32 Msg, IntPtr wParam,
IntPtr lParam);
After declaring unmanaged function explicitly, You can easily using unmanaged function with InteropServices.
Important thing is Be aware of managed and unmanaged type compatible.
IntPtr is for HWND( == void*) type.
Then I'm going to find window and send WM_GETTEXT message to it's child window.
const UInt32 WM_GETTEXT = 0x000D;
IntPtr wp, lp;
Char str[100] = {0,}; //buffer for child control's text
Int32 len =20;
//Find Window caption is "FindWindowTest"
IntPtr Parenthwnd = FindWindow(nullptr, "FindWindowTest");
//Then Find Parenthwnd window's child Edit control
IntPtr childWnd = FindWindowEx(Parenthwnd, (IntPtr)0, "Edit", nullptr);
//Then Send WM_GETTEXT Message
IntPtr r = SendMessage(childWnd, WM_GETTEXT,
safe_cast
Then I got a some text from Edit box.
But It's broken..
Because, C++/CLI using Unicode, but Edit box is not.
So I should convert it.
C++/CLI support various converting classes.
UTF32Encoding^ u32LE = gcnew UTF32Encoding(false, true,true );
String^ UnicodeStr = gcnew String(str);
array
u32LE->GetBytes(UnicodeStr, 0, UnicodeStr->Length, myBytes,0 );
array
//myBytes still has problem.
//after encoding UTF32, there are double null character.
//below code eliminate double null character.
int destIdx = 0;
for (int idx=0; idx
{
if (idx+1 != myBytes->Length)
{
if(myBytes[idx] == 0 && myBytes[idx] == 0)
{
idx++;
}
else
{
newBytes[destIdx++] = myBytes[idx];
}
}
}
//newBytes is ASCII Code. Still can't get text
//It shoud be Decode.
array<Char>^ chars = gcnew array
Decoder^ d = Encoding::ASCII->GetDecoder();
int charLen = d->GetChars(newBytes, 0, newBytes->Length, chars, 0);
String^ ConvertedStr = gcnew String(chars);
//Finally we got text, we can read
No comments:
Post a Comment