tags:

views:

140

answers:

3

I am using VB6 SP6 This code has work correctly for years but I am now having a problem on a WIN7 to WIN7 network. It also works correctly on an XP to Win7 network.

Open file for random as ChannelNum LEN =90
 'the file is on the other computer on the network

RecNum = (LOF(ChannelNum) \ 90) + 2

Put ChannelNum, RecNum, MyAcFile 
'(MyAcFile is UDT that is less than 90 long)

.......... other code that does not reference file or RecNum - then

RecNum = (LOF(ChannelNum) \ 90) + 2
Put ChannelNum, RecNum, MyAcFile
Close ChannelNum

The second record overwrites the first.

We had a similar problem in the past with OpportunisticLocking so we turn that off at install - along with some other keys that cause errors in data in Windows networks.

However we have had no problems like this for years, so I think MS have some new "better" option that they think will "improve" networking.

Thanks for your help

A: 

Hmmm... is file (as per in the open statement at the top of the code sample) UNC filename or similar to x:\ where x is the mapped drive? Are you not incrementing RecNum? Judging by the code, the RecNum is unchanged and hence appears to overwrite the first record...Sorry for sounding ummm no pun intended... basic...It would be of help to show some more code here...

Hope this helps, Best regards, Tom.

tommieb75
LOF() is supposed to return the size of the file, so RecNum *will* change in that code segment.
paxdiablo
@paxdiablo: Ok, just looked up what LOF() does, thanks for the heads up...@rickdick69: is there something that is locking on the file somewhere that is preventing your program from advancing further to write a second record. Lame I know...There's a program that you can use that is called unlocker here @ http://ccollomb.free.fr/unlocker/. Hope this will be of further help.
tommieb75
No its not locked as it writes to it correctly.The LOF is just not returning the correct lengthI will fudge the code and close the file and reopen it - but I would really like to find out what's different.
rickdic69
I have been using Unlocker as a very good tool for some years - in fact that's how I found the problem of HP printer drivers locking files!
rickdic69
A: 

It can be just timing issue. In some runs your LOF() function returns more updated information than in other runs. The file system API is asynchronous, for example when some write function is called it will not be immediately reflected as the increazed size.

In short: you code have shown an old bug, which is just easier to reproduce on Windows 7.

To fix the bug the cheapest way: you may decide to add a delay (it can be significant delay of say 5 seconds).

More elaborate fix is to force the size update by closing and reopening file.

RocketSurgeon
As I mentioned - we have already handled the strange behaviour of Windows files with the OpportunisticLocking etc changes and we have had no problems for years This code is executed 100,000's of times every day without error.And as I said it works correctly even from XP to Win7Adding a delay would be slower than closing and reopening but we haven't done this as it slows the code and it works perfectly except Win7 to Win7I feel there is something new MS have added for Win7 that is causing the problem
rickdic69
+1  A: 

I doubt there is any "bug" here except in your approach. The file metadata that LOF() interrogates is not meant to be updated immediately by simple writes. A delay seems like a silly idea, prone to occasional failure unless a very long delay is used and sapping performance at best. Even close/reopen can be iffy: VB6's Close statement is an async operation. That's why the Reset statement exists.

This is also why things like FlushFileBuffers() and SetEndOfFile() exist at the API level. They are also relatively expensive operations from a performance standpoint.

Track your records yourself. Only rely on LOF() if necessary after you first open the file.

Bob Riemersma