Re: problem in using mfc in windows service
This is the code of my service...
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include "afxwin.h"
// TODO: reference additional headers your program requires here
// WinService.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include "Winsvc.h"
#include "resource.h"
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
BOOL InstallService();
BOOL DeleteService();
int _tmain(int argc, _TCHAR* argv[])
{
if(argc>1)
{
if(_tcscmp(argv[1],L"-i")==0)
{
if(InstallService())
_tprintf(L"\n\nService Installed Sucessfully\n");
else
_tprintf(L"\n\nError Installing Service\n");
}
if(_tcscmp(argv[1],L"-d")==0)
{
if(DeleteService())
_tprintf(L"\n\nService UnInstalled Sucessfully\n");
else
_tprintf(L"\n\nError UnInstalling Service\n");
}
else
{
_tprintf(L"\n\nUnknown Switch Usage\n\nFor Install use Srv1 -i\n
\nFor UnInstall use Srv1 -d\n");
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[]=
{{L"ATSService",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
return 0;
}
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
//DWORD status;
//DWORD specificError;
m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
m_ServiceStatusHandle = RegisterServiceCtrlHandler(L"ATSService",
ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
return;
}
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
{
}
bRunning=true;
while(bRunning)
{
Sleep(3000);
//Place Your Code for processing here....
}
return;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
case SERVICE_CONTROL_INTERROGATE:
break;
}
return;
}
BOOL InstallService()
{
CString errStr,logStr;
FILE* logFile;
int err = _tfopen_s(&logFile, L"C:\\rohitlog.txt", _T("w+"));
SYSTEMTIME st;
GetLocalTime(&st);
errStr.LoadString(IDS_TEST);
logStr.Format(errStr, st.wMonth, st.wDay, st.wYear, st.wHour,
st.wMinute, st.wSecond);
_ftprintf_s(logFile, logStr);
fclose(logFile);
TCHAR strDir[1024];
SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);
_tcscat(strDir,L"\\WinService.exe");
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return false;
LPCTSTR lpszBinaryPathName=strDir;
schService = CreateService(schSCManager,L"ATSService",
L"Avon Transport Service", // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
return false;
CloseServiceHandle(schService);
return true;
}
BOOL DeleteService()
{
SC_HANDLE schSCManager;
SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return false;
hService=OpenService(schSCManager,L"ATSService",SERVICE_ALL_ACCESS);
if (hService == NULL)
return false;
if(DeleteService(hService)==0)
return false;
if(CloseServiceHandle(hService)==0)
return false;
return true;
}
On Feb 19, 4:29 pm, Janma <rohi...@gmail.com> wrote:
Hi,
I have created a windows service from app wizard win32 console
application. I have selected the "Use MFC in Shared DLL option". But
when i use CString::LoadString to load a string from the resource
table, then LoadString returns failure. But if i dont use MFC and use
CAtlString from atlstr.h, then LoadString works fine.
What could be the reason for this issue?
THanks,
Rohit