|
SQL Format function string to convert date as "17/02/14 06:52:12 PM"
FORMAT(DateField, 'dd\"/\"MM\"/\"yy hh:mm:ss AM/PM') AS MyDate
Note: By default date and time separator are used from regional settings (Windows Control Pannel), to fix your own separator irrespective of OS setting add in quote char. Above code expression is C#
|
|
|
|
|
public static String normalizeFileSeparator(String path)
{
return path.replaceAll("(\\\\|/){1,}", Matcher.quoteReplacement(File.separator));
}
public static int getFileSeparatorCount(String path){
return path.replaceAll("[^\\\\|/]{1,}", "").length();
}
|
|
|
|
|
We can use _tsetlocale to set the locale used by CRT functions like wcstod to convert string to double etc.
Call to _tsetlocale
_tsetlocale(LC_NUMERIC, _T("German_Germany.1252"));
Current OS Locale string can be retrieved using following function:
CString GetLocaleStr()
{
CString localeStrBuf;
TCHAR strBuf[128];
memset(strBuf, 0, sizeof(strBuf));
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, strBuf, 127);
localeStrBuf = strBuf;
memset(strBuf, 0, sizeof(strBuf));
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SENGCOUNTRY, strBuf, 127);
if (*strBuf)
{
localeStrBuf += _T("_");
localeStrBuf += strBuf;
}
memset(strBuf, 0, sizeof(strBuf));
if ((GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTANSICODEPAGE, strBuf, 127)
|| GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE, strBuf, 127))
&& *strBuf)
{
localeStrBuf += _T(".");
localeStrBuf += strBuf;
}
return localeStrBuf;
}
Also we can change the locale to environment locale as following:
TCHAR* lcl = _tcsdup(::_tsetlocale(LC_NUMERIC, NULL));
::_tsetlocale(LC_NUMERIC, _T(""));
::_tsetlocale(LC_NUMERIC, lcl);
free(lcl);
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|
Algo for wildcard base search.
BOOL CheckPattern(LPTSTR pattern, LPTSTR str)
{
LPTSTR pStrCurPos = NULL;
LPTSTR pPatCurPos = NULL;
LPTSTR pStrMatchPos = NULL;
LPTSTR pPatMatchPos = NULL;
BOOL fIsAsterisk = FALSE;
pStrMatchPos = pStrCurPos = str;
pPatMatchPos = pPatCurPos = pattern;
while (*pStrCurPos != '\0')
{
if (*pPatCurPos == _T('*'))
{
fIsAsterisk = TRUE;
pStrMatchPos = pStrCurPos;
pPatMatchPos = ++pPatCurPos;
if (*pPatMatchPos == '\0')
{
return TRUE;
}
continue;
}
else if (*pPatCurPos == _T('?'))
{
}
else if (*pStrCurPos != *pPatCurPos)
{
if (fIsAsterisk == FALSE)
{
return FALSE;
}
pStrCurPos = ++pStrMatchPos;
pPatCurPos = pPatMatchPos;
continue;
}
++pStrCurPos;
++pPatCurPos;
}
if (*pPatCurPos == _T('*'))
++pPatCurPos;
return (*pPatCurPos == _T('\0'));
}
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|
Here are some simple steps for remote debugging of a 32 bit WinForm application:
Remote Machine:
1. Copy “x86 ” folder from “C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger ” on remote machine.
2. Right click on “.\x86\msvsmon.exe ” and click on “Run as…” and specify the user name for the system where Visual Studio is running.
3. Share your executable folder and place pdb files or you can specify the location pdb files by right click on your module during debugging in modules windows.
VS2008 Machine:
1. In Visual Studio, choose Project Properties and select Debug tab.
2. In the “Start Action” setting, select “Start External Program” and specify the executable on the remote computer (e.g. “\\manishdesktop\Application\MyApp.exe ”.)
3. Under “Start Options” in the working directory box, type the directory where the executable is located. (e.g. \\manishdesktop\Application )
4. Select “Use Remote Machine” and specify the remote machine name. You can specify any command line arguments to pass to the application.
5. Start debugging from Debug Menu.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|
Good article. Very helpful in debugging some machine specific issue !
fgf
|
|
|
|
|
Generally I have seen that for singleton implementation, we declare a static pointer mSingletonP and static method GetInstance() . Inside GetInstance() , we check if mSingletonP is NULL, allocate it using new and return the pointer otherwise if it is already allocated simply return the pointer. Typically we implement like this:
class MySingleton
{
public:
static MySingleton *GetInstance(){
if (mMySingletonP == NULL) {
mMySingletonP = new MySingleton;
}
return mMySingletonP;
}
private:
MySingleton(){
}
MySingleton(const MySingleton & ){
}
MySingleton & operator=(const MySingleton & ) {
return *((MySingleton *)NULL);
}
~MySingleton(){
}
static MySingleton *mMySingletonP;
};
Further if we want that at the end destructor must invoke, we use atexit() , C Run Time library function and register some function which will take care of delete mMySingltonP .
Recently I have a seen a nice implementation of Singleton. This implementation is good if want to invoke destructor without using atexit. This is not thread safe and this is based on a static keyword. Implementation is as below:
class Singleton
{
public:
static Singleton &GetInstance(){
static Singleton instance;
return instance;
}
private:
Singleton(){
}
Singleton(const Singleton & ){
}
Singleton & operator=(const Singleton & ) {
return *this;
}
~Singleton(){
}
};
Above is the best implementation for non thread safe singleton and below is the best implementation for thread safe singleton.
For thread safe implementation of GetInstance() we use double NULL checking and locking in our traditional singleton implementation i.e.
static MySingleton *GetInstance() {
if (mMySingletonP == NULL) {
if (mMySingletonP == NULL) {
mMySingletonP = new MySingleton;
}
}
return mMySingletonP;
}
Please share your views on the same.
Usage:
int _tmain(int argc, _TCHAR* argv[])
{
int nTmp1 = 10;
Singleton &firstRef = Singleton::GetInstance();
Singleton &otherRef = Singleton::GetInstance();
return 0;
}
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|
Microsoft CRT has new good security enhancements. I specially miss the sprintf_s on UNIX/ Mac
I have to write my own version of sprintf_s for Apple Macintosh something like this-
int sprintf_s1(char* buffer, size_t bufferSize, const char *formattingString, ...)
{
int size = -1;
if (bufferSize < 1 || buffer == NULL) {
return size;
}
va_list argptr;
va_start(argptr, formattingString);
size = vfprintf(stderr, formattingString, argptr);
if (size > bufferSize) {
return -1;
}
size = vsprintf(buffer, formattingString, argptr);
buffer[(size > 0) ? (size-1) : 0] = '\0';
va_end(argptr);
return size;
}
Any better way to calculate the size of output apart from using stderr or nul device
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|
Activation codes used to stop unauthorized software copy. One way to generate activation codes, gather some unique information (like CPU id, hard disk id, network card id etc.) from the system and write somewhere in the disk or use ImageAddCertificate() API on Windows to put this information in your executable itself.
Read your activation code by reading activation data through ImageGetCertificateData() and validate it. But what to do if due to some reason user change his network card or CPU etc.
If we need to install the software again ? No, generally all professional software take care of this situation.
I think, one idea could be gather such 4 - 5 unique IDs and make a rule that if any of 2 ids are same, continue execution otherwise show error unauthorized copy .
There could some more better ways to deal this. Starting this thread to discuss more about activation codes generation.
Manish Agarwal
manish.k.agarwal @ gmail DOT com
|
|
|
|
|