Convert Error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Convert Error

  1. #1
    Join Date
    Aug 2006
    Posts
    35

    Convert Error

    Hey guys...

    Fairly new to the whole Win32 programming scene. Not sure why this error has come up:
    ------ Build started: Project: MemoryStatus, Configuration: Debug Win32 ------
    Compiling...
    MemoryStatus.cpp
    c:\documents and settings\administrator\my documents\visual studio 2008\projects\memorystatus\memorystatus\memorystatus.cpp(17) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(237) : see declaration of 'fopen'
    c:\documents and settings\administrator\my documents\visual studio 2008\projects\memorystatus\memorystatus\memorystatus.cpp(28) : error C2440: '=' : cannot convert from 'const char [13]' to 'LPWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    c:\documents and settings\administrator\my documents\visual studio 2008\projects\memorystatus\memorystatus\memorystatus.cpp(52) : error C2664: 'RegisterServiceCtrlHandlerW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\MemoryStatus\MemoryStatus\Debug\BuildLog.htm"
    MemoryStatus - 2 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Heres the code I'm attempting to compile:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #define SLEEP_TIME 5000
    #define LOGFILE "C:\\MyServices\\memstatus.txt"
    
    SERVICE_STATUS ServiceStatus; 
    SERVICE_STATUS_HANDLE hStatus; 
     
    void  ServiceMain(int argc, char** argv); 
    void  ControlHandler(DWORD request); 
    int InitService();
    
    int WriteToLog(char* str)
    {
    	FILE* log;
    	log = fopen(LOGFILE, "a+");
    	if (log == NULL)
    		return -1;
    	fprintf(log, "%s\n", str);
    	fclose(log);
    	return 0;
    }
    
    void main() 
    { 
        SERVICE_TABLE_ENTRY ServiceTable[2];
        ServiceTable[0].lpServiceName = "MemoryStatus";
        ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
    
        ServiceTable[1].lpServiceName = NULL;
        ServiceTable[1].lpServiceProc = NULL;
        // Start the control dispatcher thread for our service
        StartServiceCtrlDispatcher(ServiceTable);  
    }
    
    
    void ServiceMain(int argc, char** argv) 
    { 
        int error; 
     
        ServiceStatus.dwServiceType        = SERVICE_WIN32; 
        ServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
        ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
        ServiceStatus.dwWin32ExitCode      = 0; 
        ServiceStatus.dwServiceSpecificExitCode = 0; 
        ServiceStatus.dwCheckPoint         = 0; 
        ServiceStatus.dwWaitHint           = 0; 
     
        hStatus = RegisterServiceCtrlHandler(
    		"MemoryStatus", 
    		(LPHANDLER_FUNCTION)ControlHandler); 
        if (hStatus == (SERVICE_STATUS_HANDLE)0) 
        { 
            // Registering Control Handler failed
            return; 
        }  
        // Initialize Service 
        error = InitService(); 
        if (error) 
        {
    		// Initialization failed
            ServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
            ServiceStatus.dwWin32ExitCode      = -1; 
            SetServiceStatus(hStatus, &ServiceStatus); 
            return; 
        } 
        // We report the running status to SCM. 
        ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
        SetServiceStatus (hStatus, &ServiceStatus);
     
        MEMORYSTATUS memory;
        // The worker loop of a service
        while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
    	{
    		char buffer[16];
    		GlobalMemoryStatus(&memory);
    		sprintf(buffer, "%d", memory.dwAvailPhys);
    		int result = WriteToLog(buffer);
    		if (result)
    		{
    			ServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
    			ServiceStatus.dwWin32ExitCode      = -1; 
    			SetServiceStatus(hStatus, &ServiceStatus);
    			return;
    		}
    
    		Sleep(SLEEP_TIME);
    	}
        return; 
    }
     
    // Service initialization
    int InitService() 
    { 
        int result;
        result = WriteToLog("Monitoring started.");
        return(result); 
    } 
    
    // Control handler function
    void ControlHandler(DWORD request) 
    { 
        switch(request) 
        { 
            case SERVICE_CONTROL_STOP: 
                 WriteToLog("Monitoring stopped.");
    
                ServiceStatus.dwWin32ExitCode = 0; 
                ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
                SetServiceStatus (hStatus, &ServiceStatus);
                return; 
     
            case SERVICE_CONTROL_SHUTDOWN: 
                WriteToLog("Monitoring stopped.");
    
                ServiceStatus.dwWin32ExitCode = 0; 
                ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
                SetServiceStatus (hStatus, &ServiceStatus);
                return; 
            
            default:
                break;
        } 
     
        // Report current status
        SetServiceStatus (hStatus,  &ServiceStatus);
     
        return; 
    }
    Any ideas?

    D

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Convert Error

    It appears that you are compiling a UNICODE build. You need to use wide strings by explicitly adding the 'L' prefix, or better yet, using the _T() macro:

    Code:
    void main() 
    { 
        SERVICE_TABLE_ENTRY ServiceTable[2];
        ServiceTable[0].lpServiceName = L"MemoryStatus";
        ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
    
        ServiceTable[1].lpServiceName = NULL;
        ServiceTable[1].lpServiceProc = NULL;
        // Start the control dispatcher thread for our service
        StartServiceCtrlDispatcher(ServiceTable);  
    }

  3. #3
    Join Date
    Aug 2006
    Posts
    35

    Re: Convert Error

    Thanks guys.

    Fixed with a couple of other errors popping up.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center