Skip to content

Instantly share code, notes, and snippets.

@sunzhuoshi
Created February 6, 2022 14:28
Show Gist options
  • Save sunzhuoshi/3b3a82e23f0b0cdaf750c7755b843aee to your computer and use it in GitHub Desktop.
Save sunzhuoshi/3b3a82e23f0b0cdaf750c7755b843aee to your computer and use it in GitHub Desktop.
Test UNC
#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