Linux下的一个程序如何改写成vc++6.0的呢

C/C++ code
class FileLock {
public:
    FileLock();
    ~FileLock();

public:
    void Open(const string& fileName);
    void Close();
    void LockShared();
    void LockExclusive();
    void Unlock();
    off_t GetSize();

protected:
    int m_fd;
    string m_fileName;
};
FileLock::FileLock() :
    m_fd(-1)
{
}

FileLock::~FileLock() {
    if (m_fd) {
    close(m_fd);
    }
}

void FileLock::Open(const string& fileName) {
    if (m_fd) close(m_fd);
    m_fd = open(fileName.c_str(), O_RDWR | O_CREAT, 0666);
    if (m_fd < 0) {
    int p = fileName.rfind('/');
    if ((errno == ENOENT) && (p > 0)) {
        MakeDirs(fileName.substr(0, p));
        m_fd = open(fileName.c_str(), O_RDWR | O_CREAT, 0666);
        if (m_fd < 0) throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str());
    } else throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str());
    }
    m_fileName = fileName;
}

void FileLock::Close() {
    if (m_fd) {
    close(m_fd);
    m_fileName.clear();
    }
}

void FileLock::LockShared() {
    if (flock(m_fd, LOCK_SH)) throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
}

void FileLock::LockExclusive() {
    if (flock(m_fd, LOCK_EX)) throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
}

void FileLock::Unlock() {
    if (flock(m_fd, LOCK_UN)) throw SystemException("Unable to unlock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
}

off_t FileLock::GetSize() {
    struct stat st;
    
    if (fstat(m_fd, &st)) throw SystemException("Unable to stat file '%s', fstat failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
    return st.st_size;
}


我用vc改写部分如下:
C/C++ code

void FileLock::Open(const string& fileName) 
{
    if (m_fd) 
        _close(m_fd);
    m_fd = _open(fileName.c_str(), _O_RDWR | _O_CREAT);//, 0666);
    if (m_fd < 0) 
    {
        int p = fileName.rfind('/');
        if ((errno == ENOENT) && (p > 0)) {
            MakeDirs(fileName.substr(0, p));
            m_fd = _open(fileName.c_str(), O_RDWR | O_CREAT);//, 0666);
            if (m_fd < 0) 
                throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str());
        } 
        else 
            throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str());
    }
    m_fileName = fileName;
}

void FileLock::Close() {
    if (m_fd) 
    {
        _close(m_fd);
        //m_fileName.clear();    
   }
}

//这个不会改
void FileLock::LockShared() 
{//LOCK_SH (共享鎖定)、 LOCK_EX (互斥鎖定)
     if (flock(m_fd, LOCK_SH)) //lock
        throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
}

void FileLock::LockExclusive() 
{//这个不会改
     if (flock(m_fd, LOCK_EX)) 
         throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
}

void FileLock::Unlock() {
  //  if (flock(m_fd, LOCK_UN)) throw SystemException("Unable to unlock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
}

off_t FileLock::GetSize() 
{
    struct _stat st;
    //int fstat(int filedes, struct stat *buf); i
    if (_fstat(m_fd, &st))// fstat function returns information about an open file.
        throw SystemException("Unable to stat file '%s', fstat failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str());
    return st.st_size; // 总大小,字节为单位 
}


作者: gisupc   发布时间: 2011-01-08

说白了就是求高人指点如何将 if (flock(m_fd, LOCK_EX))
转换为windows下vc6.0程序,
谢谢!

作者: gisupc   发布时间: 2011-01-08

就是一个线程同步的问题,在windows有很多线程同步的方法啊,事件、信号量……

作者: miaolingshaohua   发布时间: 2011-01-08

引用 2 楼 miaolingshaohua 的回复:

就是一个线程同步的问题,在windows有很多线程同步的方法啊,事件、信号量……

具体这个,能给点具体的意见吗?谢谢!

作者: gisupc   发布时间: 2011-01-08