Solved

Handliing c++ standard output in a Windows service

Posted on 2009-05-10
5
361 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This video demonstrates basic masking and how to edit the mask to reveal the desired image.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

24 Experts available now in Live!

Get 1:1 Help Now