• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

Debugging a service and a DLL in C on Windows 2003 X64

Hi Experts,
I have a service program (named "NoeService") in native C which calls a native dll in C. It was built with Visual Studio 6 on Windows 2003 32 bits, and has been working well for 6 years.
I have upgraded the service and the DLL to a Windows 2003 x64 system with Visual Studio 2008.
The build looks OK (no error) and the installation of the service works well. Also starting the service through the console or through the SCM is OK.
Now I stop the service and I want to debug in detail the DLL by defining the service as the executable from the DLL, but so far without success.
Having built the DLL in debug mode, I set the command line in the DLL Properties  Settings->Configuration->Debugging tab to
<path>NoeService.exe -start (the command which works in the console).
But the IDE says "unable to attach: process <path>noeservice.exe is not running."
What does that mean and what can I do ?

Thanks
Verdan
0
Daniel Verney
Asked:
Daniel Verney
  • 8
  • 6
1 Solution
 
sarabandeCommented:
you need to debug the executable (service) and then you can step into the dll.

go to Debug - Build - Start Debug - Attach To Process ... and attach the debugger to the service process. after that it should break at any breakpoint you defined for the service and/or dll. you may make the .pdb files available for both the service and dll.

the way you tried would only work if your service can run alternatively as a console program. i made this to my service programs that i could start them with -debug option and they didn't install as a service but call the service_main instead.

Sara
0
 
Daniel VerneyConsultantAuthor Commented:
Hi Sara,
3 questions :
1) How do I make the pdb files available for both the service and the dll ?
2) In which project IDE should I do the "attach process"?
3) My service has 2 entry points : a service_main() and a main(). I understand that I should not have isntalled the service when I want to debug ?

I'm not sure I understood...

Thanx
Verdan
0
 
sarabandeCommented:
1) if the projects are in same workspace (.dsw) the debugger should find them. if not, i would copy the dll and its pdb to the folder where the service exe is.

2) i would use a workspace which has both service and dll as projects. the service should be 'current'.

3) no, the service must be installed and running when you 'attach'. it shouldn't wait on service control manager or on socket i/o, cause you would need to 'catch' it somewhere in the code.

if you can run the service as a console program, you need to handle option 'debug' in main and would not 'install' the service but call service_main from main. you then could add the service call to debug commandline of the dll project and start it by f5 from dll ide.

Sara
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
sarabandeCommented:
note, currently you get 'process <path>noeservice.exe is not running' because the noeservice was not really executed when you start it by debugger from the ide but only 'installed'. the execution of the installed service happens by service control manager deferred depending on start modus.

Sara
0
 
Daniel VerneyConsultantAuthor Commented:
The command line in the dll properties->debugging tab contains the correct parameter to start the service [<path>noeservice -start]. In any case, "process not unning" happens even if the executable is an ordinary console project (not a service, as I have already tried. I think that I do something wrong in the debugging settings. Without debugging, the process works fine in the different configurations!

Anyway, I will apply the advice of your previous comment and will giet back to you ths coming monday.

Verdan
0
 
sarabandeCommented:
what happens if you type noeservice -start at a command window? does it start a console and runs? or does it install the service?

can you post code of your main function?

Sara

0
 
Daniel VerneyConsultantAuthor Commented:
Yes the service has been installed by "noeservice -install". It starts at a command window with "noeservice - start "and runs Ok, calling the dll, loading it, and so on. It can be stopped by "noeservice -stop" and uninstalled with "noeservice -uninstall". All that has worked for years in 32 b its and seems to work fin in 64 bits (I need to check in debug the excution of functions in the dll).

Verdan
noeservice-mains.txt
0
 
sarabandeCommented:
ok, that's exactly as i used to do it myself beside of the keyword -debug instead of -start.

in the main function i am used to compare the argv argument like

if (argc >= 1 && strcmp(argv[1], "-debug") == 0) // console mode

Open in new window



but your code should work as well. did you try to debug the service in the ide from service project? i assume that would work and it doesn't work from dll project because of wrong assumptions regarding the paths. you should know that the debugger doesn't use the paths from PATH environment variable but from paths in the VC directories - executables.

Sara
0
 
Daniel VerneyConsultantAuthor Commented:
Still working on Sara's comments
0
 
Daniel VerneyConsultantAuthor Commented:
Hi Sara,

I've not yet succeeded in debugging the dll from the service
Here is how I proceeded
1) I have put the 2 projects into the same solution (sln)
2) I've buit the solution as x64 Debug and copied the dll into the directory where noeservice.exe is, along with the dll pdb file
3) I set the service project as startup project
4) I've installed the service through the console [noeservice -install] - (debugging is Ok as well as installation)
5) Then I executed the service in debug mode : it stops into all service breakpoints but not into the dll breakpoints. The service however has been correctly started, and my logs say that the dll has been correctly loaded and its internal log messages have been recorded in its log file at the right steps.
What did I wrong?

If I try to set the dll as startup project (with noeservice.exe -start in the debugging command line) it still displays "unable to attach, process [noeservice] not running

Note 1: after each service start I stopped the service (since my dll accepts only one instance loaded)

Note 2: when looking at the noeservice.exe with right button click, it appears as a win32 service, although it has been built as an X64 project. Is there something wrong in the CreateService call:
SC_HANDLE hSCMService =	CreateService (
hSCMMgr,	
SERVICE_NAME,
SERVICE_DESCRIPTION, 
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,	// I've not found an X64 value for this parameter
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
szFilePath,
NULL,										
NULL,											    NULL,
NULL,
NULL);

Open in new window

0
 
sarabandeCommented:
did you try to step into a dll function call from service? after you once got the debugger to step into, it also should be able to recognize breakpoints.

if that doesn't work you might exit the visual studio and delete all temporary files (.ncb, .ilk, .aps, .obj, .pdb, .sbr, .dll, .lib) in project and project/debug folders. then do rebuild, copy the dll and pdb additionally into service/debug folder and try again.

i also have had such debugging problems in the past and never found exactly the reason why it worked at some environments and didn't work at others. the 64/32-bit also could be an issue.

as a workaround for debugging you may think of making a static library from your dll code and link against it.

Sara
0
 
Daniel VerneyConsultantAuthor Commented:
1) As you advised, I cleaned the solution, deleted all temp files, but with no avail.
2) the service does actually step into the dll entry function, since I know by logs that that function is executed... but the debugger does not! (even though I've copied the dll file + .pdb to the folder where the service excutable resides)
3) As a new run I proceeded as follows:
- I reverted to separate solutions for service and dll, and rebuilt them in debug mode.
- I set a breakpoint into the service (before call to dll) and start it : the service stops at that breakpoint
 - I set a breakpoint at the first line of the entry function of the dll, then I go to the debug menu->attach to process and atttach to the debugging service process (devenv.exe - NoeService debugging):  the breakpoint immediately becomes disabled = white circle with the message "the breakpopint will not currently be hit. No symbols were loaded for this document"
 - then I go back to NoeService and hit F5, the service is started (dll executed, buit still no stopping at the first dllbreakpoint, as before)
The only progress is that the dll breakpoins displays a message.

Note: I have had exactly the same behaviour with a simple console process (not a service) calling a function of the dll. I think it's a problem of settings of the debugger in both projects.
0
 
Daniel VerneyConsultantAuthor Commented:
Hi Sara,
I've seen a zillion posts on the Web about the same problem, most of them did not found any solution.

None of the suggested solutions worked for me, even uninstalling and re-installing VS 2008. That looks like a VS 2008 bug (since I did not have any problem debugging with VS 6), or something related to my system.

Verdan
0
 
Daniel VerneyConsultantAuthor Commented:
The experts did not provide a complete solution to my question, only some useful tips
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now