File: winproc.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (78 lines) | stat: -rw-r--r-- 2,326 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifdef _WIN32
#include <Windows.h>
#include <string>
#include <conio.h>
#include <limits.h>
#include <io.h>

int ExecuteProcessWIN(const std::string& commandline)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    char* cmdline = strdup(commandline.c_str());
    CreateProcess( NULL, TEXT(cmdline), NULL, NULL, FALSE, 0,
                   NULL, NULL, &si, &pi );

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
    DWORD ret;
    GetExitCodeProcess( pi.hProcess, &ret );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return ret;
}

void WriteContent( const std::string& logfile, const std::string& filename, const std::string& flags )
{
    // Open the file
    HANDLE hFile = ::CreateFile(logfile.c_str(),
                                GENERIC_WRITE,
                                FILE_SHARE_WRITE,
                                NULL,
                                OPEN_ALWAYS,
                                FILE_ATTRIBUTE_NORMAL,
                                NULL);
    if( !hFile )
        return;

    OVERLAPPED ov;
    memset(&ov, 0, sizeof(ov));
    ov.hEvent = 0x0;
    ov.Offset = UINT_MAX;
    // Obtain the lock over the file
    BOOL res = ::LockFileEx(hFile,
                            LOCKFILE_EXCLUSIVE_LOCK,
                            0,                       // must be 0
                            UINT_MAX,                // number of bytes to lock
                            0,                       // crap, set it 0
                            &ov);
    //int error = GetLastError();
    if ( !res ) {
        ::CloseHandle(hFile);
        return;
    }

    // Move the write pointer to the end
    ::SetFilePointer(hFile, 0, NULL, FILE_END);

    char cwd[1024];
    memset(cwd, 0, sizeof(cwd));
    ::getcwd(cwd, sizeof(cwd));

    std::string line = filename + "|" + cwd + "|" + flags + "\n";

    DWORD dwBytesWritten = 0;
    ::WriteFile(hFile, line.c_str(), line.length(), &dwBytesWritten, NULL);

    // Unlock the entire file
    ::UnlockFile(hFile, 0, 0, UINT_MAX, 0);
    ::CloseHandle(hFile);
}

#endif