0

I have a .NET application that I launch from an MFC application using WinAPI's CreateProcess function. The .NET application only has text labels and buttons. The problem is that the text of these labels and buttons is in Italian. I need to translate them into English. I solve this problem by finding the windows for these labels and buttons using FindWindowEx and then replacing the text in them with the SetWindowText function. The text of Labels was translated just fine. The problem is with the buttons. The old text remains displayed in the buttons. Even though when the GetWindowTextW function is called on the button window, the translated value is returned. The Spy++ utility also returns a new value. I give below the source code of the function in the MFC application, which launches and translates the windows of the .NET application:

STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.wShowWindow = SW_HIDE; //I launch .NET app hidden before translating
si.dwFlags = STARTF_USESHOWWINDOW;

PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));

//Full path to .NET app
CString ingran_name = DotNetAppFullPath;

if (!CreateProcess(NULL,    // No module name (use command line). 
    (LPWSTR)(LPCWSTR)ingran_name,           // Command line.     
    NULL,                   // Process handle not inheritable.     
    NULL,                   // Thread handle not inheritable.     
    FALSE,                  // Set handle inheritance to FALSE.     
    0,                      // No creation flags.     
    NULL,                   // Ambiente di esecuzione                                                               
    NULL,                   // Use parent's starting directory.     
    &si,                    // Pointer to STARTUPINFO structure.    
    &pi))                   // Pointer to PROCESS_INFORMATION structure.{   
{
    return;
}

Sleep(500);

//Main window .NET app
HWND ingran_main_hwnd = FindTopWindow(pi.dwProcessId);
CWnd* ingran_main_pWnd = CWnd::FromHandle(ingran_main_hwnd);

//Translate Label
CWnd* label_pWnd = FindWindowEx(ingran_main_hwnd, NULL, NULL, _T("OriginalTextLabel")); 
CString reset_btn;
label_pWnd->GetWindowTextW(reset_btn);
label_pWnd->SetWindowText(_T("TranslatedTextLabel"));
label_pWnd->GetWindowTextW(reset_btn);

//Translate button
CWnd* button_pWnd = FindWindowEx(ingran_main_hwnd, NULL, NULL, _T("OriginalTextButton"));
button_pWnd->UnlockWindowUpdate();
button_pWnd->GetWindowTextW(reset_btn);                 //here I get "OriginalTextButton"
button_pWnd->SetWindowText(_T("TranslatedTextButton"));
button_pWnd->GetWindowTextW(reset_btn);                 //here I get "TranslatedTextButton"
button_pWnd->UpdateData();    //That didn't help
button_pWnd->UpdateWindow();  //And that didn't help too

//Now I show the main window
ingran_main_pWnd->ShowWindow(SW_SHOW);

I don't have the source code for the application that I need to localize. For my experiments with translation, in order to eliminate the influence of hidden factors that can prevent text replacement, I created my own MFC and .NET applications. In the MFC, when I click on the button, I run the above code. After running this code the .NET applications starts and the label text is replaced, the button text remains the same. Although if run under a debugger, then button_pWnd-> GetWindowTextW(reset_btn); returns the replaced text - TranslatedTextButton. Also, when viewing the properties of the button window by the Spy++ utility, the title of the TranslatedTextButton window is displayed.

My MFC running My .NET

enter image description here

enter image description here

  • 1
    Checking the [Win API docs](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowtexta) of SetWindowText, in the Remarks section: *To set the text of a control in another process, send the WM_SETTEXT message directly instead of calling SetWindowText.* So I guess you need to utilize `SendMessage`for this. – Doc Brown Jun 19 '23 at 13:35
  • What kind of C# application? Winforms or WPF? – Pepijn Kramer Jun 19 '23 at 14:00
  • 3
    There will be no reliable way to translate an arbitrary third party application. After all, the application might draw text directly. So the reliable way to do this would be ask the developer if you can pay to have it translated. Or to deliver it in the form of a UI-less library, and supply your own, translated, UI. – JonasH Jun 19 '23 at 14:01
  • I used Winforms – Andrey Angerchik Jun 19 '23 at 14:02
  • 1
    "I"!? So its your Net App? And you don't simply translate it? – Ralf Jun 19 '23 at 14:14
  • Especially for experimenting with translation, I created my own very simple .NET application with a text label and a button – Andrey Angerchik Jun 19 '23 at 14:24
  • @AndreyAngerchik But that's now how one translates one's own application. There are _much_ nicer ways to do that are built into WinForms. – PMF Jun 19 '23 at 14:52
  • 3
    Strange question: if the .NET app is already localizable (which is good practise), there's no source to recompile, you can just create a [iso lang] folder and put the myapp.resources.dll (with proper keys) in there, like fr\myapp.resources.dll for french. https://learn.microsoft.com/en-us/dotnet/core/extensions/localization#resource-files the rest is all hack. May work or not. – Simon Mourier Jun 19 '23 at 16:03
  • 1
    Another thing to consider: even _if_ you can reliably manage to change every text, you still may have the issue of controls not being large enough to contain the translated text. – Christian.K Jun 20 '23 at 04:40
  • @Christian.K there is no such problem in the utility whose resources I need to translate. There is problem only with showing new assigned text. Spy++ and GetWindowTextW returned the new text from button window. – Andrey Angerchik Jun 20 '23 at 09:23
  • @DocBrown I tried using SendMessage WM_SETTEXT - it didn't help. – Andrey Angerchik Jun 20 '23 at 14:20
  • @AndreyAngerchik: from the comments above it is not clear if you have the source code of the real application you want to translate under your control, or if the Winforms application you mentioned is a second one created by you for making the screenshots in this question. Could you edit the question and tell us a few words why you cannot simply localize/translate the app by making the necessary changes in the source code? And if SendMessage / WM_TEXT did not help, that belongs also into the question, with a short sample what you tried. – Doc Brown Jun 22 '23 at 06:08
  • @DocBrown Thanks for your interest in my issue. I have added the description. – Andrey Angerchik Jun 22 '23 at 07:16
  • Did you try [How to change the button text of another programs window](https://stackoverflow.com/questions/11748901/how-to-change-the-button-text-of-another-programs-window)? It is a C# solution, but in case it works for your case, maybe you can port it to C++/MFC. – Doc Brown Jun 23 '23 at 05:09

0 Answers0