Solved

Handliing c++ standard output in a Windows service

Posted on 2009-05-10
5
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wireless name in LAN adapter 14 91
Visual Studio hangs on running project 6 84
PDF edit software 2 62
Migrate Thunderbird to Outlook 6 91
Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
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…
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

734 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