Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

07 January, 2015

How to create a file extension association with the program in Windows

Assume that you have a program named "table-explorer.exe".
And you want to open all ".csv" and ".sas7bdat" files using  this program by default in Windows.

First you need to create a file extension association with the program via these commands:


reg add HKCU\SOFTWARE\Classes\.csv /d "table-explorer" /f
reg add HKCU\SOFTWARE\Classes\.sas7bdat /d "table-explorer" /f


And then register the application:


reg add HKCU\SOFTWARE\Classes\table-explorer\shell\open\command /d "\"C:\Path\to\table-explorer.exe\"" /f

15 June, 2008

HowTo batch remove temporaty files from directory and subdirectories

Just create .bat file with this content:

@set path=
@for /R %%i in (*.tmp *.bak) do @del %%i


and update (*.tmp *.bak) section with your set of files or masks.

09 January, 2008

Windows XP hibernate mode problem

Do you have a problem with Hibernate mode in Windows XP?
And you can't turn on Hibernate mode from Power Options Panel, it says: "The process cannot access the file because it is being used by another process".
I found one crasy method how to resolve this problem on my computer (AMD64/1G RAM/MB Asus M2NE/Video Asus N7600/IDE HDD).

  • Delete file: WINDOWS\system32\drivers\atapi.sys (it will be restored by Windows).

  • Manualy enable hibernate mode (now it isn't throw error message).

  • Switch computer to Hibernate mode.


Is it helpful?

29 August, 2007

Windows Service Simple Sample

Some years ago I was C/C++ developer and I wrote several windows services.
I know that writing of service programs is quite difficult for beginners.

In this post I'm publishing simple working example.
This service only beeps with one second's interval after starts.


service.c

#include <windows.h>

HANDLE hServerStopEvent = NULL;
VOID ServiceBody();
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
VOID WINAPI ServiceControlHandler(DWORD dwCtrlCode);
BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint);


SERVICE_STATUS ssStatus;
SERVICE_STATUS_HANDLE sshStatusHandle;

void _cdecl main(int argc, char **argv)
{
SERVICE_TABLE_ENTRY dispatchTable[] =
{{"", (LPSERVICE_MAIN_FUNCTION)ServiceMain }, { NULL, NULL }};
StartServiceCtrlDispatcher(dispatchTable);
}

void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
sshStatusHandle = RegisterServiceCtrlHandler( "", ServiceControlHandler);

if (!sshStatusHandle)
goto cleanup;

ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode = 0;

if (!ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000))
goto cleanup;

ServiceBody();

cleanup:
if (sshStatusHandle)
(VOID)ReportStatusToSCMgr(SERVICE_STOPPED, NO_ERROR, 0);
return;
}

VOID WINAPI ServiceControlHandler(DWORD dwCtrlCode)
{
if(dwCtrlCode==SERVICE_CONTROL_STOP)
{
ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
if ( hServerStopEvent )
SetEvent(hServerStopEvent);
}
ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
}

BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
{
if (dwCurrentState == SERVICE_START_PENDING)
ssStatus.dwControlsAccepted = 0;
else
ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

ssStatus.dwCurrentState = dwCurrentState;
ssStatus.dwWin32ExitCode = dwWin32ExitCode;
ssStatus.dwWaitHint = dwWaitHint;

if ( ( dwCurrentState == SERVICE_RUNNING )
|| ( dwCurrentState == SERVICE_STOPPED ) )
ssStatus.dwCheckPoint = 0;
else
ssStatus.dwCheckPoint++;

return SetServiceStatus( sshStatusHandle, &ssStatus);
}


//This sample only beeps.
VOID ServiceBody ()
{
hServerStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!hServerStopEvent)
goto cleanup;

if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0))
goto cleanup;

do
{
MessageBeep(-1);
} while (WaitForSingleObject(hServerStopEvent, 1000 )== WAIT_TIMEOUT);

cleanup:
if (hServerStopEvent)
CloseHandle(hServerStopEvent);
}



Script for compiling with Visual C++ 6.0:

make_vc6.bat
@call "D:\Program Files\Microsoft Visual Studio\VC98\Bin\vcvars32.bat"
@cl /nologo service.c user32.lib advapi32.lib
@del service.obj

If you use Visual C++ Express, you need additional Platform SDK from Microsoft.

Install (register) service via command:

service_create.bat
sc create "XQX Service" binPath= %CD%\service.exe


Start service via command:

service_start.bat
net start "XQX Service"


Stop service via command:

service_stop.bat
net stop "XQX Service"


Uninstall (remove) service via command:

service_delete.bat
sc delete "XQX Service"

09 August, 2007

HowTo remove service in Windows

Sometimes it is necessary to remove service from the Service List in the Windows Managment Console.
In the past I used program, which I compiled from the MSDN's sources (MSDN Library - October 2001: Samples\VC98\sdk\winbase\winnt\service).

But I found another way: Windows XP contains special program SC.exe, which can communicates with the Service Controller and installed services (start, stop, remove, create, query, etc.).
After that I use this command in my shell scripts.

E.g.: for removal of MySQL service, execute command:
sc delete MySQL
(! You may restore MySQL service via command mysqld-nt.exe --install)

But if you want to do this from your C++ program, you may use sample's sources from MSDN.

Part of these is listed below:


service.c
...
void CmdRemoveService()
{
SC_HANDLE schService;
SC_HANDLE schSCManager;

schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if ( schSCManager )
{
schService = OpenService(schSCManager, TEXT(SZSERVICENAME), SERVICE_ALL_ACCESS);

if (schService)
{
// try to stop the service
if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
{
_tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME));
Sleep( 1000 );

while( QueryServiceStatus( schService, &ssStatus ) )
{
if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
_tprintf(TEXT("."));
Sleep( 1000 );
}
else
break;
}

if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
_tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
else
_tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );

}

// now remove the service
if( DeleteService(schService) )
_tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
else
_tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256));


CloseServiceHandle(schService);
}
else
_tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));

CloseServiceHandle(schSCManager);
}
else
_tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
}
...