Solved

Handliing c++ standard output in a Windows service

Posted on 2009-05-10
5
355 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

A list of useful business intelligence software.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now