You are currently browsing the archives for the Windows category


How to check Windows or process is 64bit

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
bool Is64BitWindows()
{
#if defined(_WIN64)
	return true;  // 64-bit programs run only on Win64
#elif defined(_WIN32)
 
	// 32-bit programs run on both 32-bit and 64-bit Windows
	// so must sniff
	BOOL f64 = FALSE;
	LPFN_ISWOW64PROCESS fnIsWow64Process;
 
	fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
	if (NULL != fnIsWow64Process)
	{
		return !!(fnIsWow64Process(GetCurrentProcess(), &f64) && f64);
	}
 
	return false;
#else
	return false; // Win64 does not support Win16
#endif
}

http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net

Other functions, for finding CPU and whether the process is 64bit.

typedef void(WINAPI *LPFN_GetNativeSystemInfo)(LPSYSTEM_INFO);
LPCWSTR GetPlatformW()
{
	static WCHAR sT[64];
	static bool done = false;
	if (done)
		return sT;
 
	LPFN_GetNativeSystemInfo fnGetNativeSystemInfo = (LPFN_GetNativeSystemInfo)GetProcAddress(
		GetModuleHandle(TEXT("kernel32")), "GetNativeSystemInfo");
 
	if (NULL == fnGetNativeSystemInfo)
	{
		lstrcpy(sT, L"Unknown");
	}
	else
	{
		SYSTEM_INFO si = { 0 };
		fnGetNativeSystemInfo(&si);
		switch (si.wProcessorArchitecture)
		{
		case PROCESSOR_ARCHITECTURE_AMD64:	lstrcpy(sT, L"AMD64");	break;
		case PROCESSOR_ARCHITECTURE_ARM:	lstrcpy(sT, L"ARM");	break;
		case PROCESSOR_ARCHITECTURE_IA64:	lstrcpy(sT, L"IA64");	break;
		case PROCESSOR_ARCHITECTURE_INTEL:	lstrcpy(sT, L"INTEL");	break;
		default:lstrcpy(sT, L"Unknown"); break;
		}
	}
	return sT;
}
 
bool Is64BitProcess()
{
#if defined(_WIN64)
	return true;
#else
	return false;
#endif
}

I tried to run this code only on AMD64. I don’t know what happens in ARM or IA64 platforms.

Refresh the taskbar of Windows 10

Sometimes windows 10 taskbar failed to draw the application icon. ie4uinit.exe can fix it. Create the following batch file and run it.

REM @echo off
set "UTIL=%SystemRoot%\System32\ie4uinit.exe"
 
IF NOT EXIST %UTIL% (
set "UTIL=%SystemRoot%\Sysnative\ie4uinit.exe"
)
 
%UTIL% -ClearIconCache
%UTIL% -show

Reading EPWING dictionaries with Dicregate

1,Obtain any EPWING dictionaries.
2,Open Dicregate.exe
3,Select [Dictionary]->[Add Dictionary]->[Electronic Dictionary]

4,Choose the dictionary folder, the folder that includes a file named “CATALOGS”

How to use VB regex in VC6

1,Open “OLE/COM object viewer” from Tool menu of VC6.
2,Find “Microsoft VBScript Regular Expressions 5.5” under “Type Libraries”.

3, Double click the node to open with “ITypeLib Viewer”.
4, Choose “Save As” and save as “vbscript.IDL”.

5, Run “midl vbscript.idl” in Command Propmt. midl.exe is typically located under “C:\Program Files\Microsoft Visual Studio\VC98\Bin”.

This creates “vbscript.tlb”.
6, Move this file into your VC6’s project directory.

7, By coding #import “vbscript.tlb” in your source file, VB regex will be available.

#include "stdafx.h"
 
#import "vbscript.tlb" no_namespace named_guids
 
_bstr_t vbregReplace(LPCTSTR source,
					 LPCTSTR replace, 
					 LPCTSTR regex,
					 BOOL bGlobal,
					 BOOL bCaseInsensitive)
{
	IRegExpPtr pReg;
	pReg.CreateInstance(CLSID_RegExp);
 
	_bstr_t bstrSource(source);
	_bstr_t bstrReplace(replace);
	_bstr_t bstrRegex(regex);
 
	pReg->put_Global(bGlobal?VARIANT_TRUE:VARIANT_FALSE); 
	pReg->put_IgnoreCase(bCaseInsensitive?VARIANT_TRUE:VARIANT_FALSE); 
	pReg->put_Pattern(bstrRegex); 
 
	_bstr_t bstrRet = pReg->Replace(bstrSource, bstrReplace);
 
	return bstrRet;
}
 
int main(int argc, char* argv[])
{
	CoInitialize(NULL);
	_bstr_t t = vbregReplace(
		"abcdefg",
		"x",
		"[aceg]",
		TRUE,
		TRUE);
 
	puts(t);
	CoUninitialize();
	return 0;
}

How to move file to a trash

Use SHFileOperation().

#include <windows.h>
#include <tchar.h>
#include <assert.h>
#include <malloc.h>
#include "SHDeleteFile.h"
 
BOOL SHDeleteFile(LPCTSTR lpFile)
{
	size_t len = _tcslen(lpFile);
	if(!lpFile || lpFile[0]==0 || len <= 3)
		return FALSE;
 
	// only fullpath allowed
	do
	{
#ifndef UNICODE
		if (IsDBCSLeadByte((BYTE)lpFile[0]))
			return FALSE;
#endif
 
		if( lpFile[0]==_T('\\') && lpFile[1]==_T('\\') )
			break;
 
		if( lpFile[1] == _T(':') && lpFile[2]==_T('\\') )
			break;
 
		return FALSE;
	} while(false);
 
	LPTSTR p = (LPTSTR)_alloca( (len+2)*sizeof(TCHAR) );
	if(!p)
		return FALSE;
	_tcscpy(p, lpFile);
	p[len+1]=0;
 
	SHFILEOPSTRUCT sfo = {0};
	sfo.hwnd = NULL;
	sfo.wFunc = FO_DELETE;
	sfo.pFrom = p;
	sfo.pTo   = NULL;  // ignored
	sfo.fFlags = FOF_ALLOWUNDO;
 
	int ret = SHFileOperation(&sfo);
	return ret==0;
}

This code is for single file. I did not know what happens when a folder is passed. sfo.pFrom and sfo.pTo must be a double-null terminated string.

Add a debug console to your windows gui application

Call AllocConsole() at the beginning of your application. Only one console can be allocated for a process.

Next, call WriteConsole to show a text.

void CMyApp::LogDebug(LPCSTR p)
{
	if(m_bAppDebug)
	{
		DWORD d;
		WriteConsoleA(
			GetStdHandle(STD_OUTPUT_HANDLE),
			(CONST VOID *)p,
			strlen(p),
			&d,
			NULL
			);
		WriteConsoleA(
			GetStdHandle(STD_OUTPUT_HANDLE),
			(CONST VOID *)"\r\n",
			2,
			&d,
			NULL
			);
 
	}
}

I don’t know why but WriteConsole has Anscii and Unicode definition. Is there any differences?
I didn’t do a research for that now.
And at the end of your application, call FreeConsole().

You can also call WriteFile instead of WriteConsole, in this case the function can be used for both the console and a file.

In both cases, there is a chance standard handle is not as what you expected because the calling process can inherit or put a security on it. For example, if the application using above code was run from cygwin, the GetStdHandle() would fail.