Solved

HELP: Can't write a text file from a NT Service

Posted on 2002-07-17
11
197 Views
Last Modified: 2010-04-01
I'm trying to open and write to a text file to be used as a service logfile, the file is opened when entering the service and should be available to all the threads the service creates.
I've create a CLogFile that simply opens the file anda provides a method to format the text before writing, it uses the CreateFile and WriteFile functions.
Somehow nothing happens, it doesn't open the file, doesn't write... even when the file is opened inside a thread the result is the same, creates a 0 byte file and the WriteFile function doesn't write.
Like the old Beetles song: HELP :)
0
Comment
Question by:JoaoMota
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 7159104
what does WriteFile return ? have you checked what GetLastError() return ?

can post the code where you are writing to file
0
 

Author Comment

by:JoaoMota
ID: 7159130
I forgot to mention: when running the service as normal app the logfile works as expected, only when running as a service it does nothing.

i use the following code in the OpenLog method:
hLogFile = CreateFile(acFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS,  FILE_ATTRIBUTE_NORMAL, NULL );

and the following in the WriteLog method:
WriteFile((HANDLE)hLogFile, cLogString, strlen(cLogString), &nWritten, NULL);
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 7159325
Are you using an absolute path name in 'acFile'? If not, take a look into '%WINNT%\System32', you might find the file there :o)
0
 

Expert Comment

by:gemme
ID: 7159542
Try closing the file and see if it then populates it.  I had a similiar issue with NT and found that the file will not write until it is closed.
0
 

Author Comment

by:JoaoMota
ID: 7159954
guys.... thank you all, but i've found the problem (some $#%&#$#$$#% hard-coded a absolute path in one of the custom lib we use here... once again thank you all.

Joao - portuguese food&wine fan
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:jkr
ID: 7159972
>>thank you all, but i've found the problem

Then you should reject the proposed "answer" and ask CS to delete this Q...
0
 
LVL 30

Expert Comment

by:Axter
ID: 7160433
Hi (gemme), welcome to EE.

All of the experts here, for the most part have learn from other experts as to the proper etiquette for posting answer.

An answer should not be posted as an answer, if other experts have previously posted possible answers as comments, and/or have already made contributions to the question.

There are many experts who never post answers as answer.  Instead, they post their answers as comments.

If you read the following link, you'll see why this is the preferred method for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp

Hi (JoaoMota):
Feel free to click the [Reject Answer] button near (Answer-poster's) response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other experts.  Later, you can click the [Select Comment as Answer] button on any response.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7376080
Dear JoaoMota

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to

     "refund the points and delete this question"

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 86

Expert Comment

by:jkr
ID: 7376653
Me:
>>Are you using an absolute path name in 'acFile'?

JoaoMota:
>>guys.... thank you all, but i've found the problem (some
>>$#%&#$#$$#% hard-coded a absolute path in one of the
>>custom lib we use here

It sounded like "answered" to me :o)
0
 
LVL 11

Expert Comment

by:griessh
ID: 7376870
Sorry, I missed your comment about the absolute path name.

I will suggest to accept

    "jkr"

comment as an answer.

======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7419907
Force accepted

** Mindphaser - Community Support Moderator **

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 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

9 Experts available now in Live!

Get 1:1 Help Now