Accessing Sharepoint Object Model from a Windows Service is giving error

I have to write a windows service which gathers some information from sharepoint and sends appropriate emails. I don't want to write work flow for this. I am unable to access any sharepoint component from my windows service. The same code runs fine in a console application but is failing in the windows service. for e.g

SPSite ST = new SPSite("");
gives me the error
System.IO.FileNotFoundException: The Web application at could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
   at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
   at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
   at ExpenseReportAlerter.ExpenseReportAlerter.OnElapsedTime(Object source, ElapsedEventArgs e) in C:\Documents and Settings\vacodev\My Documents\Visual Studio 2008\Projects\ExpenseReportAlerter.cs:line 94

If I use Run with Elevated Privileges I get this error

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at ExpenseReportAlerter.ExpenseReportAlerter.OnElapsedTime(Object source, ElapsedEventArgs e) in C:\Documents and Settings\vacodev\My Documents\Visual Studio 2008\Projects\ExpenseReportAlerter\ExpenseReportAlerter\mycode.cs:line 98

Can I even access Sharepoint objects in a windows service or not? I am using code
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yes you can so long as you reference the correct dlls in your code.
Where are the web services hosted? Are they on a sharepoint web front end?
Meant to say that you also need to add a web reference in your code to point to the server where the servics are
Object model works only from the same server where SharePoint is installed. Yes you can compile code on non SharePoint machine as long as you have DLL references but can run code on those.
CMAKhanAuthor Commented:
QPR, I am not using web services any where. Sorry if my earlier post was not clear. I want to write a Windows Service which access Sharepoint data using the Object model.

mo1one, the code is residing on the server machine and the .dll's are properly referenced. the same code runs fine in a console application but breaks when I put it in a windowsservice.
CMAKhanAuthor Commented:
Ok I found the issue. I was starting the service as a local service for currenlty logged in user account which did not had rights on Share Point. That was also the reason it was giving me the error web application not found. I changed the service login to the user having proper rights and it's working now. I still can't understand one thing that why my imprsonation was not working as I was imprsonating before accessing SharePoint in the code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.