User32 API SendMessage to open a file from open file dialogbox in Win10 OS

markandbhatt
1
Reputation point
I have written the following c# statement to open a file
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
private const int WM_SETTEXT = 12;
private void OpenFile()
{
var text = @"c:\devops\dev.log";
SendMessage(ptr.ToInt32(), WM_SETTEXT, text.Length, new StringBuilder(text));
}
The above code works for the following open file dialog box. Opens C:\devops\dev.log
The same code fails for the following open file dialog box.
I noticed that the above dialog box has "Search" text box. Will it make any difference?
EDIT
After sending file path to open file dialog box, I tried to read from the dialog box to make sure the dialog box has correct file path text. But, I discovered that the output is "search this pc" and not the file path to open.
int WM_GETTEXTLENGTH = 0xE;
int WM_GETTEXT = 0xD;
int textLength = SendMessage(ptr.ToInt32(), WM_GETTEXTLENGTH, 0, 0);
if (textLength == 0)
{
return string.Empty;
}
var text = new StringBuilder(textLength + 1);
int nRet = SendMessage(ptr.ToInt32(), WM_GETTEXT, text.Capacity, text);
if (nRet == 0) return
string.Empty;
Console.WriteLine(text.ToString());
{count} votes
Both from Win 10. Yes, UI changes have no effect on us, but certainly have effect on user32 programming.
Do you know what could be wrong with my code? Or any suggestions to make it work for both types of open file dialog boxes.
The code snippet that you posted does not enable anyone to reproduce your issue. We can see that you are sending a WM_SETTEXT message but we have no idea what HWND is represented by ptr.ToInt32(). So a complete example for both types of dialog boxes would be helpful.
I just checked the 2 Dialog styles and the Control IDs are the same (0x47C)
So, you just have to use the right handle (or use the usual way, with UIAutomation (AutomationId = 1148))
Maybe ptr is wrong?
Why do you need such kind of interventions to dialogs?
After sending file path to open file dialog box, I tried to read from the dialog box to make sure the dialog box has correct file path text. But, I discovered that the output is "search this pc". and not the file path to open. Added code sample in the question
This probably means that you need another ptr.
Another ptr? you mean ptr of file path text field?
It seems that ptr is not the filename control. (The filename control looks like a combobox, not a textbox). Maybe ptr was obtained incorrectly?
Sign in to comment
Activity