Link to home
Start Free TrialLog in
Avatar of rishab2905
rishab2905

asked on

debugging errors in Vsual studio.net

hi Can anyone please help em with the debugging of the following errors in visualstudio.net. Anvil1 is the name of my service and it beeps the system speakers at regular intervals.Can anyone please help me in this regard?I am unable to build it because of the following errors.
#include "stdafx.h"
#include "Windows.h"
#include "Winsvc.h"
#include "time.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 main(int argc, char* argv[])
{
  if(argc>1)
  {
    if(strcmp(argv[1],"-i")==0)
    {
      if(InstallService())
        printf("\n\nService Installed Sucessfully\n");
      else
        printf("\n\nError Installing Service\n");
    }
    if(strcmp(argv[1],"-d")==0)
    {
      if(DeleteService())
        printf("\n\nService UnInstalled Sucessfully\n");
      else
        printf("\n\nError UnInstalling Service\n");
    }
    else
    {
      printf("\n\nUnknown Switch Usage\n\nFor Install use Srv1 -i\n\nFor UnInstall use Srv1 -d\n");
    }
  }
  else
  {
    SERVICE_TABLE_ENTRY DispatchTable[]=
                {{"Anvil1",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("Anvil1", 
                                            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);
    Beep(450,150);//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()
{
  char strDir[1024];
  HANDLE schSCManager,schService;
  GetCurrentDirectory(1024,strDir);
  strcat(strDir,"\\Srv1.exe"); 
  schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
  if (schSCManager == NULL) 
    return false;
  LPCTSTR lpszBinaryPathName=strDir;
 
  schService = CreateService(schSCManager,"Anvil1", 
        "Anvil1", // 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()
{
  HANDLE schSCManager;
  SC_HANDLE hService;
  schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
  if (schSCManager == NULL)
    return false;
  hService=OpenService(schSCManager,"Anvil1",SERVICE_ALL_ACCESS);
  if (hService == NULL)
    return false;
  if(DeleteService(hService)==0)
    return false;
  if(CloseServiceHandle(hService)==0)
    return false;
 
return true;
 
 
 
________________________________________________________________________________________________________________________________When i try to build this code, i am getting the following errors. Can anyone please help me in debugging these errors. Anvil1 is the name of my service and i this service tries to beep the system speakers at regular intervals.Please help me with the debugging of the following errors.
 
 
1>------ Build started: Project: Anvil1, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(43) : error C2440: 'initializing' : cannot convert from 'const char [7]' to 'LPWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(62) : error C2664: 'RegisterServiceCtrlHandlerW' : cannot convert parameter 1 from 'const char [7]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(112) : error C2664: 'GetCurrentDirectoryW' : cannot convert parameter 2 from 'char [1024]' to 'LPWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(118) : error C2440: 'initializing' : cannot convert from 'char [1024]' to 'LPCTSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(131) : error C2664: 'CreateServiceW' : cannot convert parameter 1 from 'HANDLE' to 'SC_HANDLE'
1>        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(136) : error C2664: 'CloseServiceHandle' : cannot convert parameter 1 from 'HANDLE' to 'SC_HANDLE'
1>        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(148) : error C2664: 'OpenServiceW' : cannot convert parameter 1 from 'HANDLE' to 'SC_HANDLE'
1>        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>Build log was saved at "file://c:\Documents and Settings\rssaddaX\My Documents\Visual Studio 2008\Projects\Anvil1\Anvil1\Debug\BuildLog.htm"
1>Anvil1 - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

It looks like you are compiling this for (wide) Unicode but you're using narrow data types. Try chaning your char types to be TCHAR or disable the Unicode setting in your project file.
^^^Also, if you do compile using Unicode you won't be able to use narrow C functions (such as strcmp) you'll need to use the wide alternatives (such as wcscmp).
Open your project settings, go to 'Configuration Properties', choose 'General' and set 'Character Set' to 'Use Multi-Byte Character Set' and you'll get rid of all these errors.
Avatar of rishab2905
rishab2905

ASKER

Hi Thanks a lot for your response.I am quite new to Visual studio.net.Could you please expalin me how i can disable the Unicode setting of my project file.
Hi Thanks a lot for your suggestions , i was able to get rid of the majority of the errors.But, i am still getting the following error messages.Can you please clarify?


1>------ Build started: Project: Anvil1, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(51) : warning C4101: 'status' : unreferenced local variable
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(52) : warning C4101: 'specificError' : unreferenced local variable
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(113) : warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\string.h(79) : see declaration of 'strcat'
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(131) : error C2664: 'CreateServiceA' : cannot convert parameter 1 from 'HANDLE' to 'SC_HANDLE'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(136) : error C2664: 'CloseServiceHandle' : cannot convert parameter 1 from 'HANDLE' to 'SC_HANDLE'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\documents and settings\rssaddax\my documents\visual studio 2008\projects\anvil1\anvil1\stdafx.cpp(148) : error C2664: 'OpenServiceA' : cannot convert parameter 1 from 'HANDLE' to 'SC_HANDLE'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>Build log was saved at "file://c:\Documents and Settings\rssaddaX\My Documents\Visual Studio 2008\Projects\Anvil1\Anvil1\Debug\BuildLog.htm"
1>Anvil1 - 3 error(s), 3 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
MS has changed their compilers to be more strict and seperated the data types in a cleaner way, thus the errors. Try the following, it compiles without errors:

#include "stdafx.h"
#include <stdio.h>
#include "Windows.h"
#include "Winsvc.h"
#include "time.h"
 
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusSC_HANDLE;
 
BOOL bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlSC_HANDLEr(DWORD Opcode);
BOOL InstallService();
BOOL DeleteService();
int main(int argc, char* argv[])
{
  if(argc>1)
  {
    if(strcmp(argv[1],"-i")==0)
    {
      if(InstallService())
        printf("\n\nService Installed Sucessfully\n");
      else
        printf("\n\nError Installing Service\n");
    }
    if(strcmp(argv[1],"-d")==0)
    {
      if(DeleteService())
        printf("\n\nService UnInstalled Sucessfully\n");
      else
        printf("\n\nError UnInstalling Service\n");
    }
    else
    {
      printf("\n\nUnknown Switch Usage\n\nFor Install use Srv1 -i\n\nFor UnInstall use Srv1 -d\n");
    }
  }
  else
  {
    SERVICE_TABLE_ENTRY DispatchTable[]=
                {{"Anvil1",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_ServiceStatusSC_HANDLE = RegisterServiceCtrlHandler("Anvil1", 
                                            ServiceCtrlSC_HANDLEr); 
  if (m_ServiceStatusSC_HANDLE == NULL)
  {
    return;
  }
  m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  m_ServiceStatus.dwCheckPoint = 0;
  m_ServiceStatus.dwWaitHint = 0;
  if (!SetServiceStatus (m_ServiceStatusSC_HANDLE, &m_ServiceStatus))
  {
  }
 
  bRunning=true;
  while(bRunning)
  {
    Sleep(3000);
    Beep(450,150);//Place Your Code for processing here....
  }
  return;
}
 
void WINAPI ServiceCtrlSC_HANDLEr(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_ServiceStatusSC_HANDLE,&m_ServiceStatus);
      bRunning=false;
      break;
    case SERVICE_CONTROL_INTERROGATE:
      break; 
  }
  return;
}
 
BOOL InstallService()
{
  char strDir[1024];
  SC_HANDLE schSCManager,schService;
  GetCurrentDirectory(1024,strDir);
  strcat(strDir,"\\Srv1.exe"); 
  schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
  if (schSCManager == NULL) 
    return false;
  LPCTSTR lpszBinaryPathName=strDir;
 
  schService = CreateService(schSCManager,"Anvil1", 
        "Anvil1", // 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,"Anvil1",SERVICE_ALL_ACCESS);
  if (hService == NULL)
    return false;
  if(DeleteService(hService)==0)
    return false;
  if(CloseServiceHandle(hService)==0)
    return false;
 
return true;
}

Open in new window

Hi Jkr, Thanks a lot for your valuable suggestions. You are really a genious in your own self and you are the driving force behind my zeal to learn to new things. Even though the code has been executed succesfully and built, i am not able to find my service in the list of services that come with the OS. Should i follow anymore steps to find my service in the list.Please dont mind as i might be bothering you with the same question.This is my first job.But, i have been trying to install it from along time.Please clarify.Your suggestions would be widely appreciated as i am very close to accomplishing something good for my project.I followed exactly what was stated in the link that you sent me earlier.
Hm, did you run the command

myservice -i

to install it?

BTW, change the following snippet to include your actual service executable's name:



  GetCurrentDirectory(1024,strDir);
  strcat(strDir,"\\my_service_name.exe");  // <---- change here!

Open in new window

Hi Jkr, I changed the name to Anvil2.exe because that is the name of the service that i want to see in the list and when i ran the command, it says it is not a recognised command in the command prompt.Can you please help?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi JKr, Thanks a ton for your valuable suggestions. I was succesful in installing my service in the list. I am so happy for you suggestions. Is there a way where i can give you more points than 500. I am not even quaified enough to assign you the marks, but out of my jubiliation, i just want to give more points than 500. CAn you please tell me?
Unfortunately, more than 500 is not possible, and that's OK with me. If you want to do me a favour, just close your othre open Qs if they're answered ;o)