0

I'm with difficulties for insert a text in a textBox or RichtextBox without repeat.

This is code used:

 #include "windows.h" #pragma comment(lib, "user32") void actwnd() { wchar_t lastwindow[MAX_PATH]; wchar_t currentwindow[MAX_PATH]; HWND mainwindow; mainwindow = GetForegroundWindow(); GetWindowText(mainwindow,currentwindow,sizeof(currentwindow)); if(lastwindow==currentwindow) { } else if(lastwindow!=currentwindow) { strcpy ((char*)lastwindow,(char*)currentwindow); String^ strNew = gcnew String(currentwindow); // String^ wnd = gcnew String(reinterpret_cast(currentwindow)); textBox1->Text += strNew; } } // Set interval for 1000ms in test private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { actwnd(); } 

Any help will appreciated

1 Answer 1

0

if(lastwindow==currentwindow) does not compare the strings, it compares the addresses of the arrays, so they will never be equal. Use wcscmp to compare wchar_t arrays. Since these are wchar_t arrays, it is also invalid to be copying them with strcpy, which is only used with char arrays. Use wcscpy instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank @ScottMcP-MVP. Based in your clue, I achieved it make some changes of follow way => if(wcscmp(lastwindow,currentwindow)!=0) { wcscpy (lastwindow,currentwindow); String^ strNew = gcnew String(currentwindow); textBox1->Text += strNew; } .Work fine now!