2012年1月9日 星期一

[C++] Create Process

bool CreateProcess(wstring wstrProcessName, wstring wstrParas, )
{
  WCHAR wszCmd [2048] = {0};
  swprintf(wszCmd, L"%ls %ls", wstrProcessName.c_str(), wstrParas.c_str());

  int nProcessPriority = BELOW_NORMAL_PRIORITY_CLASS;
  STARTUPINFOW si;
  PROCESS_INFORMATION pi;
  ZeroMemory(&si, sizeof(STARTUPINFOW));
  ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
  si.cb = sizeof(STARTUPINFOW);
  si.dwFlags = STARTF_USESHOWWINDOW;
  si.wShowWindow = SW_HIDE;

  if (CreateProcessW(NULL, wszCmd, NULL, NULL, FALSE, CREATE_NEW_CONSOLE|nProcessPriority, NULL, NULL, &si, &pi))
  {
    return true;
  }
  return false;
}

[C++] wstring to string

Here is an example for wstring to string transform.
HRESULT DSU_WcsToStr(wstring wstrSrc, string* strDst, UINT nCodepage)
{
 _DSU_ChkPtrRtn(strDst);

 int iStrLen = wstrSrc.length();
 int nCount = WideCharToMultiByte(nCodepage, 0, wstrSrc.c_str(), iStrLen, NULL, 0, NULL, FALSE);

 char* pCHAR = new char[nCount + 1]; 
 nCount = WideCharToMultiByte(nCodepage, 0, wstrSrc.c_str(), iStrLen, pCHAR, nCount + 1, NULL, FALSE);

 pCHAR[nCount] = NULL;
 *strDst = pCHAR ;
 delete [] pCHAR ; 

 return S_OK ;
}

[C++] string to wstring

Here is an example for string to wstring transform.
HRESULT DSU_StrToWcs( string strSrc, wstring* wstrDst, UINT nCodepage = CP_UTF8) ;

HRESULT DSU_StrToWcs(string strSrc, wstring* wstrDst, UINT nCodepage)
{
 if ( wstrDst == NULL ) 
  return E_POINTER;

 int iStrLen = strSrc.length() ;

 int nCount = MultiByteToWideChar(nCodepage, 0, strSrc.c_str(), iStrLen, NULL, 0) ;
 WCHAR* pWCHAR = (WCHAR*)alloca(sizeof(WCHAR) * (nCount + 1)) ;

 nCount = MultiByteToWideChar(nCodepage, 0, strSrc.c_str(), iStrLen, pWCHAR, nCount+1) ;
 pWCHAR[nCount] = NULL ;

 *wstrDst = pWCHAR ;

 return S_OK ;
}

2011年10月27日 星期四

[Android] expired debug certificate

http://developer.android.com/resources/faq/troubleshooting.html#signingcalendar

I can't compile my app because the build tools generated an expired debug certificate

If your development machine uses a locale that has a non-Gregorian calendar, you may encounter problems when first trying to compile and run your application. Specifically, you may find that the Android build tools won't compile your application because the debug key is expired.
The problem occurs because the Keytool utility — included in the JDK and used by the Android build tools — fails to properly handle non-Gregorian locales and may create validity dates that are in the past. That is, it may generate a debug key that is already expired, which results in the compile error.
If you encounter this problem, follow these steps to work around it:
  1. First, delete the debug keystore/key already generated by the Android build tools. Specifically, delete the debug.keystore file. On Linux/Mac OSX, the file is stored in ~/.android. On Windows XP, the file is stored in C:\Documents and Settings\\.android. On Windows Vista, the file is stored in C:\Users\\.android
  2. Next, you can either
    • Temporarily change your development machine's locale (date and time) to one that uses a Gregorian calendar, for example, United States. Once the locale is changed, use the Android build tools to compile and install your app. The build tools will regenerate a new keystore and debug key with valid dates. Once the new debug key is generated, you can reset your development machine to the original locale.
    • Alternatively, if you do not want to change your machine's locale settings, you can generate the keystore/key on any machine using the Gregorian calendar, then copy the debug.keystore file from that computer to the proper location on your development machine.
This problem has been verified on Windows and may apply to other platforms.
For general information about signing Android applications, see Signing Your Applications.

2011年9月28日 星期三

[Android] Dynamically change Options Menu Items in Android

Sometimes, we need to change options menu dynamically, you can follow this example.
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		// You can use mEnableMenu to enable meun
		if (mEnableMenu)
		{
			menu.add(0, 0, 0, "Setting");
		}
		return super.onPrepareOptionsMenu(menu);
	}

Reference: 

[Android] How to monitor sdcard mount/unmout?

In this example, we demo the sdcard mount/unmount monitor.
	public void onCreate() {
		super.onCreate();
		this.registerReceiver(this.myReceiver, getSDCardIntentFilter());
	}

	public IntentFilter getSDCardIntentFilter() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
		intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
		intentFilter.addDataScheme("file");
		return intentFilter;
	}

	private BroadcastReceiver myReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) 
		{
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED))
			{
			  // handle SD card unmounted
			}
			else if (action.equals(Intent.ACTION_MEDIA_MOUNTED))
			{
			  // handle SD card mounted
			}
		}
	};

[Android] How to block back and search button on Dialog?

It will cancel dialog If user press back or search button on dialog. Therefore, sometimes we need to block back and search button on dialog to ensure the logic of dialog can work normally.
		public void alertDialog(final String title, final String msg, final String btn1, final String btn2, final String btn3, final String cmd, final String data)
		{
			AlertDialog.Builder MyAlertDialog = new AlertDialog.Builder(self);
			MyAlertDialog.setIcon(android.R.drawable.ic_dialog_alert);
			MyAlertDialog.setTitle(title);
			MyAlertDialog.setMessage(msg);
			MyAlertDialog.setCancelable(false); // block back button

			MyAlertDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
			{
				@Override
				public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
				{
					if (keyCode==KeyEvent.KEYCODE_SEARCH)	// block search button
						return true;
					else
						return false;
				}
			});

			DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
				// Handle onClick
				}
			};
			if (btn1 != null)
				MyAlertDialog.setPositiveButton(btn1, OkClick );
			if (btn2 != null)
				MyAlertDialog.setNegativeButton(btn2, OkClick );
			if (btn3 != null)
				MyAlertDialog.setNeutralButton(btn3, OkClick );

			 MyAlertDialog.show();
		}