Search This Blog

Saturday, January 31, 2009

Web.config error - The entry already added

if you get such error as your connection string already added or any key in your web.config file.
you just have to know that your there is a web.config file that already in use , with the same key or connection string that you try to override , to take the key or connection string in your current web.config file add the node
<clear/>
this will clear any exist settings before.

Tuesday, January 13, 2009

Command line arguments - LPWSTR to char*

if you want to see the command line arguments of you can use the argc and argv parameter , in my program i create a service with ATL (vc++) and the argc / argv are missing .
so to get the arguments in this case i used the next code snippets:

LPWSTR *szArglist;
int nArgs;
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
.......
To convert the character from LPWSTR to char* i use the next macro:
W2A - (vice versa A2W) you will have to add in your function the macro
USES_CONVERSION
e.g.
char *pp = W2A(szArglist[1]);
LocalFree(szArglist);// Free memory allocated
Yaniv

Sunday, January 11, 2009

How to Handle the ctrl-c or ctrl-break event in c++ application

the next tip tested on windows platform via c++ console application.
sometimes there is a need to handle the ctrl-c or ctrl break event on your console to clean some data or even to release an event, this is the way to do that.
1. create the next function:

BOOL CtrlHandler( DWORD fdwCtrlType )
{
switch( fdwCtrlType )
{
// Handle the CTRL-C and others signal.
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
printf( "Ctrl-C or break event arrived clean my stuff \n\n" );
char buff[200];
CleanMyStuff(buff);
return( FALSE ); // its important to return false to send this event to the next chain
}
return FALSE;
};

return FALSE from this method is important if you want to send this event to the next chain.

2. in your console app in your main function set the next line .

SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) ;

Yaniv