I have problem with this code,I want to print names of the windows in .txt file and it compile with no errors but it snaps in runtime.

this function int x = GetWindowText(hWnd,title, 256); for the second arg require char* and cant deal with WCHAR and if i cast title to (char*), function wcscpy(oldTitle, title) wont work

how to solve this?

char c[10];
  WCHAR title[128];
WCHAR oldTitle[128];
  
  
  HWND hWnd;

  hWnd = GetForegroundWindow();
  
  int x = GetWindowText(hWnd,title, 256);
  
  if(wcscmp(oldTitle, title) && wcscmp(L"", title))
	
  {
      for(int i = 0; i < x; i++)
      fprintf(File, "%c", title[i]);
    fprintf(File, "\n");
    
    
  }

  wcscpy(oldTitle, title);

ok,thank you!

tell me which is equivalent function with TCHAR for wcscpy(),for copying strings,and the others I will find in google?

tell me which is equivalent function with TCHAR for wcscpy(),for copying strings,and the others I will find in google?

Forget the Google, instead look up the particular function in MSDN, for example wcscpy(). Then, on that page scroll down to where you see "TCHAR.H routine", there you'll find the equivalent _tcs-prefixed routine name (in this case it is: _tcscpy).

thank you mitmkar Ive changed types and functions to TCHAR/_tcs() and now it compiles with no errors but I have 30 warnings and app crashes in runtime

here are some of the warnings:


Warning 2 warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Warning 4 warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Warning 8 warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Warning 30 warning LNK4076: invalid incremental status file '.\Debug/nnn.ilk'; linking nonincrementally nnn nnn


as I can see its something about chars/strings...maybe some preprocesor command or linker???

here are some of the warnings:
warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

This warning C4996 is pretty self-explanatory i.e. it is saying that you should consider using the suggested function instead, which is supposed to be more secure/robust. But it is up to you which one you use, perhaps lookup fopen_s() and its TCHAR-aware counterpart _tfopen_s().

Furthermore, you can suppress this warning (C4996) by adding the _CRT_SECURE_NO_WARNINGS preprocessor definition in your project's properties.

So, these warnings are only suggestions to use safer alternatives, hence I guess that the run-time crash occurs because there is something seriously wrong with your code.


[EDIT]
The linker's warning LNK4076 is something that you can safely ignore.

thank you for advice!

Warning 30 warning LNK4076: invalid incremental status file '.\Debug/nnn.ilk'; linking nonincrementally nnn nnn

what I did is deleted .ilk file and all warnings cleared,and my app doesnt crash and runs but partly

the thing is,it wont print chars,fprintf(),sprintf(),strcmp() wont work,and I need to keep those variables as char

the TCHAR variables and functions work

what could it be?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.