How to instrument and get code coverage of a Windows service with Visual Studio? - c++

I am trying to get the code coverage for a Windows service written in C++ (let's call it hello.exe) with Visual Studio 2012 Premium tools. So far I have attempted to use the method outlined in the answer to this post: How to use MS code coverage tool in command line?
For your convenience, the steps given were to run:
vsinstr /coverage hello.exe
start vsperfmon /coverage /output:mytestrun.coverage
hello.exe
vsperfcmd /shutdown
I am able to instrument, test and get the .coverage file for hello.exe when I start it as an executable in the command prompt no problem. But if I instead try to start it as a service in step 3 (either through services.msc manually or win32serviceutil.StartService in Python), nothing registers and the coverage file is empty. Is there something i'm missing here?
Extra info: In an effort to solve this I came across what seems to be another code coverage utility called CodeCoverage.exe (Microsoft Visual Studio 11.0\Team Tools\Dynamic Code Coverage Tools). Is that what I'm supposed to be using for this task? Also, I found something seemingly related here https://msdn.microsoft.com/en-us/library/dd255408.aspx, but (forgive me for my ignorance) am not sure whether a .NET service is the same thing as a Windows service.

Answering my own question here, after a lot of trying and pouring through documents I found this:
https://blogs.msdn.microsoft.com/hilliao/2008/08/19/code-coverage-using-visual-studio-2008s-code-coverage-tools/
Basically, a windows Service is different from a process so we have to enable the cross session flag and specify the user when monitoring.
start vsperfmon /coverage /output:mytestrun.coverage /cs /user:”Everyone”
So just instrument the exe as usual, start monitoring with above command, start the service, do testing, stop the service, and shutdown the monitoring.
Hope it helps someone in the future.

Related

How to run Google tests (C++ unit tests) in a QNX Virtual machine, using resharper?

Currently how I run my unit tests:
I have a visual studio project, which builds a C++ google test (unit test) executable.
I then copy this exe to my Virtual Machine with QNX and run it manually, to get the unit test results.
Can I use resharper to automate this?
Resharper is able to detect and list tests from test file. But on running them, it throws error as it is not able to execute the tests in Windows.
Below is the exact error, I get, on running any of the tests.
ReSharper Ultimate – Error while running unit tests:
Invalid path
Can I set up resharper and Visual studio environment, so that, I can run tests directly from visual studio, which will copy the exe to the Virtual Machine and give me the execution results?
This will avoid me manually copy pasting the exe and running command to execute the tests?
I searched a lot regarding this in google. But couldn't find much help.
I'm new to google test and resharper. So any help would be greatly appreciated.
Thanks in advance.
No, I'm afraid ReSharper cannot do that out of the box.
Though it is quite powerful, so if you manage to write a script that does the VM specific part, ReSharper should be able to cope with it. What that script needs to do is:
It must be run on the host (Windows).
Copy the test binary to the QNX system in the VM.
Execute the test.
Capture the test's output and redirect it to the host's stdout.
The ReSharper options can be set here:
Menu ReSharper > Options...
In the left pane scroll down to Tools > Unit Testing > C++ Tests
In the text field Command: you need to enter the path to the script.
See https://www.jetbrains.com/help/resharper/Reference_Options_Tools_Unit_Testing_CPP_Tests.html for more details.
I think you'll find that the msg box showing "ReSharper Ultimate – Error while running unit tests: Invalid path" has been caused by the last update to VS 2017, which has broken things. I usually run my google tests through resharper c++, and until a few days ago, it worked. Now, I see this exact same error.

How to start program before login in windows in c++? [duplicate]

I've written a console program that "does stuff" - mainly using boost. How do I convert it to a Windows Service?
What should I know about Windows Services beforehand?
There's a good example on how to set up a minimal service on MSDN. See the parts about writing the main function, entry point and also the example code.
Once you've got a windows service built and running, you'll discover the next major gotcha: it's a pain to debug. There's no terminal (and hence no stdout/stderr) and as soon as you try to run the executable it actually launches the service then returns to you.
One trick I've found very useful is to add a -foreground option to your app so that if you run with that flag then it bypasses the service starter code and instead runs like a regular console app, which makes it vastly easier to debug. In VS.Net set up the debugging options to invoke with that flag.
There's a really good example on msdn here
It's a boiler plate C++ service project that has self install/uninstall functionality and logs service start and stop events to the windows event log. It can be stopped and started through the services app (snapin) like other services. You may want to initially give it LocalSystem rights to see it working , as on xp at least it doesn't have enough rights to start with the project provided rights of LocalService. The Visual Studio 2008 project otherwise runs out of the box despite the downloaded instructions implying otherwise.
A bit late but I hope this helps someone else.
You might be able to 'wrap it' using this tool from CodeProject:
http://www.codeproject.com/KB/system/xyntservice.aspx
Worth a look.
The simplest solution might be to create a new Windows Service project in Visual Studio and copy across your code to the new project.
If you refactor your code so that you've split the UI (in this case the console) from the logic you could create a library that does the work and then call that from both the Console project and the Service Project.
You can configure an application to run as a service by using the Srvany tool, which is a part of the Windows Server 2003 Resource Kit Tools.
Srvany allows only one service at same time. So I write my srvany (sFany) to make nginx and php-cgi run as windows service together. Here is the source https://github.com/stxh/sFany

How to run C++ test on visual studio online

I have test cases that are executable (*.exe files). There is no user interface involved.
How do I use Team center /visual studio online to run these test cases on server.
For now, either on demand running or scheduled running will work for me.
(Currently I have no test case that runs on server. So you may mention the basic setup. )
I have written some test cases (they are exe files). I can run them locally line any other exe file.
My code is in C++.
My test cases are in C++.
You could run them as part of your build. Just configure a build in VSO for your solution, and then modify the msbuild project file to call your tests and send the output to the build folder so it gets uploaded as part of the drop. If you are using VS, you would get a better experience using the VS unit testing support (i.e., get results in VS): http://www.visualstudio.com/get-started/run-tests-with-builds-vs.

Debug a GTest in VS2010

I have a solution in VS2010 (C++ code) that includes test files directly in the src and include folders for the project of a plug-in. I am using Google Test framework. The plug-in, let's call it myPlugin, is running on a software, say mySoft.
######################################################################
Here is the issue:
I can run tests with command line, ok.
But i would like to debug inside the GTESTs. Why ? Because the tests are not really unit tests, they are setting a lot of parameters, and i would like to check step by step that everything is set ok. I know this is not mandatory to solve this and that i can debug the application itself. Anyway, this is my question, i want to debug inside a gtest.
I try to config VS2010 properties so as to launch the tests from VS, and this is not working.
############################################################################
More details:
To run the tests, i have to have an executable launched. In other words, i am running tests while an application, mySoft.exe, is running. The plug-in is running using another executable, the sdk_mySoft.exe.
The command lines (that work) are
mySoft.exe -sdkRun=pluginLevel ;pluginDirName;version;myPlugin_x64.dll
--gtest_output=xml
-sdkRun is in-house command offered by the sdk to set up GTests. So here i launch my application, run the plug-in, get some logs from google test, ok.
Then, I try to configure VS with these debugging properties:
Command $(SolutionDir)\bin64\mySoft.exe
Command Arguments -sdkRun=pluginLevel ;pluginDirName;version;myPlugin_x64.dll
--gtest_output=xml
and here this is not working.
Error is
Debugging information for 'mysoft.exe' cannot be found or does not
match. Cannot find or open the pdb files.
Any idea ?
How to debug in gtest, taking into account the special conditions here: have to launch an application?
Thanks
To debug the GTest, the debugger needs to be linked to application.
Launching the application, click on CTRL gives access to the VS debugger.

Problems with DevPartner

I have installed DevPartner and I am using visual studio 2010.
However, when I launch 'Start with coverage analysis', nothing happens.
In the videos, they show that in the under Options->DevPartner->Code review, there should be a list of projects. But I do not see such list.
It will be great if you could please help me with it.
Many thanks.
Chintan
Coverage analysis ans Code review are 2 separate products in the Devpartner Suite.
Coverage Analysis is to used to determine which lines of code are executed in a run of your project. You can also merge several runs to get an overall idea of how much code you have exercised. Coverage analysis works for both Native (C, C++) and managed languages.
When you launch your application with coverage analysis you will only see a small toolbar appear that allows you to take snapshots or stop recording coverage information. After your program exits a final session will be collected and displaying in Visual Studio. Unless something is wrong with your license. Another problem would be when launching Visual Studio and opening your project you may see an error unable similar to Devpartner start session control service.
Code Review is used as static analysis of Managed (.Net) code. It does not run your project but analysis the code in your projects and determines if any of that code violates the rule set you selected to run against.