Solved

Handliing c++ standard output in a Windows service

Posted on 2009-05-10
5
365 Views
Last Modified: 2013-12-03
Hello everybody,
I'm trying to port a server of my own from Linux to Windows. After compiling the code under Cygwin, I was able to start the server in a dos console.
Then I wanted the server to run as a Windows service. Based on this tutorial, http://www.devx.com/cplus/Article/9857, I made some additional change to the code and installed the service successfully with this command:
sc create <service name> binpath= "<path with parameters>".
The service appears in the Windows service panel, but when I try to start it fails with the error 0xffffffff.
- What does this error code mean?
- Is this failure due to the use of standard ouput streams in the service?
- Under Linux the output streams were redirected in a log file within a bash script. What is the best way to achieve the same functionality under Windows?

Thanks,
Thierry Feuzeu
0
Comment
Question by:kamiche
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 24354731
The error code is simply '-1' - is it possible that your code returns that somewhere?
Regarding the output streams, what are you using them for?
If it is only log output, 'freopen()' could be used to redirect them to a file from your code.
0
 

Author Comment

by:kamiche
ID: 24355129
The output streams are only used for logging purpose. I'll use freopen to redirect std output streams. With ouputs in a file I'll certainly be able to debug the program.
By now, I can't not tell where the error code -1 comes from.
0
 
LVL 86

Expert Comment

by:jkr
ID: 24355417
Can you try to add some diagnostic output, e.g. like the following to see how far your service gets in starting up? That output can be watched using 'DebugView' (http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx).
oid
__cdecl
DbgReport ( char* pszFormat, ...) {
 
    static char s_acBuf [ 2048];
 
    va_list args;
 
    va_start ( args, pszFormat);
 
    vsprintf ( s_acBuf, pszFormat, args);
 
    OutputDebugStringA ( s_acBuf);
 
    va_end ( args);
}
 
// ...
 
DbgReport("In 'service_main()', command line is %s\n", GetCommandLine());

Open in new window

0
 

Author Comment

by:kamiche
ID: 24372846
I'm now able to output strings using the function above. I could'nt achieve this using standard io, even if they were redirected to a file with freopen. The output file alway remains empty. So I had to replace all calls to io functions with calls to DbgReport.
However, the service doesn't start, and it seems to me that the program halts on "select" system call.
Is there any issue or restriction to take into account when using socket in windows services? I recall that the program is being ported from Linux to Windows using Cygwin.

I'm going to do more tests and report any useful information.
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 24375573
>>and it seems to me that the program halts on "select" system call.
>>Is there any issue or restriction to take into account when using socket in
>>windows services?

Yup, here we are: Your problem is that services running under the 'LocalSystem' account have almost OS privileges, but are not allowed to use _any_ networking facilities. To overcome this, make sure to assign your service to run under a predefined user account (preferrably one belonging to the admin group) which does have network access (this is usually done by specifying an account name&password in the call to 'CreateService()', but you're also able to change this from the 'services' control panel applet).
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can i Install a new Data Source on Visual Studio 2 99
ios - android compatible database app 3 92
WPF issue with Trigger 2 106
SonarQube on Linux vs Windows 3 26
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question