2024-04-07 データファイルをプリンターに出力するプログラム #include <iostream>#include <Windows.h> void PrintFile(const std::string& filename) { // デフォルトのプリンターを取得 DWORD bufsize = 256; char printerName[256]; GetDefaultPrinter(printerName, &bufsize); // プリンターデバイスコンテキストを取得 HDC hdc = CreateDC("WINSPOOL", printerName, NULL, NULL); // ファイルを開く HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { std::cerr << "Failed to open file: " << filename << std::endl; return; } // ファイルの内容を読み取ってプリンターに送信 DWORD bytesRead; BYTE buffer[4096]; while (ReadFile(hFile, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0) { DWORD bytesWritten; WritePrinter(hdc, buffer, bytesRead, &bytesWritten); } // リソースを解放 ClosePrinter(hdc); CloseHandle(hFile);} int main() { std::string filename = "example.txt"; // 出力するファイル名を指定 PrintFile(filename); // 指定したファイルをプリンターに出力 return 0;}