2015年11月11日 星期三

Use Unicode and Hide the Console in Windows .


     If one would like to make his program being able to accept the arguments which is not English, the Unicode is requisite. That would make your software be more international. ( For me, it is instinct to use Chinese as file name and so forth.)

   To use Unicode in windows, One could set your program as unicode in Visual Studio:




That your could use unicode in your code.

If your program arguments could be Chinese, the main should be the form:


int wmain(int argc, wchar_t *argv[]) 


in windows with unicode.


but, if you would like to hide console (for example, if you use Qt librarie with Visual Studio), the well-known solution :


#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")


would not work.

there would be :


error LNK2001: unresolved external symbol _main


To solve it as valid is very tricky, that is :


#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")


To demostrate i, the code be :


#include <windows.h>

#include <locale.h>
#include <wchar.h>



#ifndef _DEBUG
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")
#endif

int wmain(int argc, wchar_t *argv[]) 
{
 int k;
 
 k = 1;
 while(k < argc)
 { 
  wchar_t bufferW[256];
  memset(&bufferW[0], 0, 256*sizeof(wchar_t));
  wcsncpy(&bufferW[0], argv[k], 256);  

#ifdef _DEBUG  
  setlocale(LC_ALL, "");
  wprintf(TEXT("%s\n"), &bufferW[0]);
  
#else
  MessageBox(NULL, &bufferW[0], TEXT("參數"), MB_OK);
#endif
  k++;
 }/*while*/

 return 0;
}/*wmain*/


and set the input argument as :



(Of course, do not forget set the character set as Unicode in the configuration properties -> General)

In the debug mode, the output would be in the console :



In the release mode, the console would be hidden, and the output be :