Created
February 6, 2022 14:28
-
-
Save sunzhuoshi/3b3a82e23f0b0cdaf750c7755b843aee to your computer and use it in GitHub Desktop.
Test UNC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <io.h> | |
#include <fcntl.h> | |
#include <Windows.h> | |
void print_last_error() | |
{ | |
LPVOID lpMsgBuf; | |
DWORD dw = GetLastError(); | |
FormatMessage( | |
FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
FORMAT_MESSAGE_FROM_SYSTEM | | |
FORMAT_MESSAGE_IGNORE_INSERTS, | |
NULL, | |
dw, | |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
(LPTSTR)&lpMsgBuf, | |
0, NULL); | |
std::cout << "code: " << dw << ", msg: " << (LPTSTR)lpMsgBuf; | |
} | |
int main() | |
{ | |
const char* file_path = "Z:\\Temp\\Test.txt"; | |
HANDLE handle = CreateFile(file_path, FILE_APPEND_DATA, | |
FILE_SHARE_WRITE | FILE_SHARE_READ, | |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
if (handle == INVALID_HANDLE_VALUE) | |
{ | |
print_last_error(); | |
exit(-1); | |
} | |
char buf[64] = "test"; | |
int fd = _open_osfhandle((intptr_t)handle, O_BINARY); | |
int ret = _write(fd, buf, strlen(buf)); | |
if (ret <= 0) | |
{ | |
print_last_error(); | |
exit(-1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment