сервер:
Код: Выделить всё
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
int main()
{
// Return code from functions
BOOL fReturnCode;
// Size of the message in bytes
DWORD cbMessages;
// Number of messages in the Mailslot channel
DWORD cbMsgNumber;
// Identifier of the Mailslot channel
HANDLE hMailslot1;
HANDLE hMailslot2 = {0};
// Name of the server Mailslot channel
LPSTR lpszReadMailslotName = (LPSTR)"\\\\.\\mailslot\\$Channel1$";
// Name of the client Mailslot channel
LPSTR lpszWriteMailslotName = (LPSTR)"\\\\*\\mailslot\\$Channel2$";
// Buffer for data transmission through the channel
char szBuf[512];
// Number of bytes of data received through the channel
DWORD cbRead;
// Number of bytes of data sent through the channel
DWORD cbWritten;
DWORD total = 0;
// Buffer for error message, result
char message[80] = { 0 };
// File descriptor
FILE* hdl;
printf("Mailslot server demo\n");
// Create a Mailslot channel with the name lpszReadMailslotName
hMailslot1 = CreateMailslot(
lpszReadMailslotName, 0,
MAILSLOT_WAIT_FOREVER, NULL);
// If an error occurs, output the error code and terminate the application
if (hMailslot1 == INVALID_HANDLE_VALUE)
{
fprintf(stdout, "CreateMailslot: Error %ld\n",
GetLastError());
_getch();
return 0;
}
// Output a message about the creation of the channel
fprintf(stdout, "Mailslot created\n");
// Wait for a connection from the client
// Loop to receive commands through the channel
while (1)
{
total = 0;
// Check the state of the Mailslot channel
fReturnCode = GetMailslotInfo(
hMailslot1, NULL, &cbMessages,
&cbMsgNumber, NULL);
if (!fReturnCode)
{
fprintf(stdout, "GetMailslotInfo: Error %ld\n",
GetLastError());
_getch();
break;
}
// If there are Mailslot messages in the channel,
// read the first one and display it on the screen
if (cbMsgNumber != 0)
{
if (ReadFile(hMailslot1, szBuf, 512, &cbRead, NULL))
{
// Output the received string to the console
printf("Received: \n", szBuf);
// If the command "exit" is received, terminate the application
if (!strcmp(szBuf, "exit"))
break;
// Otherwise, assume that the received string is a file name
else
{
// Open a channel with the MSLOTCLIENT process
hMailslot2 = CreateFile(
lpszWriteMailslotName, GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
// If an error occurs, output the error code and terminate the application
if (hMailslot2 == INVALID_HANDLE_VALUE)
{
fprintf(stdout, "CreateFile for send: Error %ld\n",
GetLastError());
_getch();
break;
}
if (hdl = fopen(szBuf, "rt")) {
// Loop to read until the end of the file
while (!feof(hdl)) {
// Read one character from the file
if ((char)fgetc(hdl) == 0x20) total++;
}
// Output an error message
sprintf(message, "(Server): file:%s, spaces = %d\n", szBuf, total);
WriteFile(GetStdHandle(STD_ERROR_HANDLE), message, strlen(message), &cbWritten, NULL);
// Send a message to the channel
sprintf(message, "%d", total);
WriteFile(hMailslot2, message, strlen(message) + 1, &cbWritten, NULL);
printf("Bytes sent %d\n", cbWritten);
// Close the file
fclose(hdl);
}
else {
// Send a message to the channel
sprintf(message, "(Server)Can't open %s!", szBuf);
WriteFile(GetStdHandle(STD_ERROR_HANDLE), message, strlen(message) + 1, &cbWritten, NULL);
printf("\n");
WriteFile(hMailslot2, message, strlen(message) + 1, &cbWritten, NULL);
printf("Bytes sent %d\n", cbWritten);
}
}
}
else
{
fprintf(stdout, "ReadFile: Error %ld\n",
GetLastError());
_getch();
break;
}
}
// Sleep for 500 milliseconds
Sleep(500);
// End of the while loop
}
CloseHandle(hMailslot1);
CloseHandle(hMailslot2);
return 0;
}
Код: Выделить всё
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
DWORD main(int argc, char* argv[])
{
// Identifiers of Mailslot channels
HANDLE hMailslot1, hMailslot2;
// Name of the client Mailslot channel
LPSTR lpszReadMailslotName = (LPSTR)"\\\\.\\mailslot\\$Channel2$";
// Buffer for the Mailslot channel name
char szMailslotName[256];
// Buffer for data transmission through the channel
char szBuf[512];
// Return code from functions
BOOL fReturnCode;
// Size of the message in bytes
DWORD cbMessages;
// Number of messages in the Mailslot2 channel
DWORD cbMsgNumber;
// Number of bytes sent through the channel
DWORD cbWritten;
// Number of bytes of data received through the channel
DWORD cbRead;
printf("Mailslot client demo\n");
printf("Syntax: mslotclient [servername]\n");
// If a server name was specified at startup,
// include it in the Mailslot channel name
if (argc > 1)
sprintf(szMailslotName, "\\\\%s\\mailslot\\$Channel1$",
argv[1]);
// If no server name was specified, create a channel
// with the local process
else
strcpy(szMailslotName, "\\\\.\\mailslot\\$Channel1$");
// Open a channel with the MSLOTSERVER process
hMailslot1 = CreateFile(
szMailslotName, GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
// If an error occurs, output the error code and terminate the application
if (hMailslot1 == INVALID_HANDLE_VALUE)
{
fprintf(stdout, "CreateFile for send: Error %ld\n",
GetLastError());
_getch();
return 0;
}
// Output a message about the creation of the channel with the server
fprintf(stdout, "Connected. Type 'exit' to terminate\n");
// Create a Mailslot channel with the name lpszReadMailslotName
hMailslot2 = CreateMailslot(
lpszReadMailslotName, 0,
MAILSLOT_WAIT_FOREVER, NULL);
// If an error occurs, output the error code and terminate the application
if (hMailslot2 == INVALID_HANDLE_VALUE)
{
fprintf(stdout, "CreateMailslot for reply: Error %ld\n",
GetLastError());
CloseHandle(hMailslot2);
_getch();
return 0;
}
// Output a message about the creation of the channel
fprintf(stdout, "Mailslot for reply created\n");
// Loop to send commands through the channel
while (1)
{
// Output a prompt for command input
printf("cmd>");
// Read the input string
gets_s(szBuf);
// Send the entered string to the server process as a command
if (!WriteFile(hMailslot1, szBuf, strlen(szBuf) + 1,
&cbWritten, NULL))
break;
// If the command "exit" is received, terminate the data exchange loop
if (!strcmp(szBuf, "exit"))
break;
// Wait for a reply
fprintf(stdout, "Waiting for reply...\n");
// Check the state of the Mailslot2 channel
fReturnCode = GetMailslotInfo(
hMailslot2, NULL, &cbMessages,
&cbMsgNumber, NULL);
if (!fReturnCode)
{
fprintf(stdout, "GetMailslotInfo for reply: Error %ld\n",
GetLastError());
_getch();
break;
}
// If there are Mailslot messages in the channel,
// read the first one and display it on the screen
if (cbMsgNumber != 0)
{
if (ReadFile(hMailslot2, szBuf, 512, &cbRead, NULL))
{
// Output the received string to the console
printf("Received: \n", szBuf);
}
}
}
// Close the channel handle
CloseHandle(hMailslot1);
CloseHandle(hMailslot2);
return 0;
}
Код: Выделить всё
#include "pch.h"
#include
#include
#include
#include
#include
#define BUF_SIZE 512
//#define PIPE_NAME TEXT("\\\\.\\pipe\\MyNamedPipe")
extern "C" __declspec(dllexport) int ProcessFile(wchar_t* inputFileName, wchar_t replacementChar) {
HANDLE hIn, hOut;
DWORD nIn, nOut;
wchar_t Buffer[BUF_SIZE];
std::wstring outputFileName;
// Determine the name of the output file
wchar_t* pos = wcsstr(inputFileName, L".");
if (pos == NULL) {
outputFileName = inputFileName; // If there is no dot, just copy the name
outputFileName += L".txt2"; // Add .OUT to the file name
}
else {
size_t letter = pos - inputFileName; // Find the position of the dot
outputFileName.assign(inputFileName, letter); // Copy the name up to the dot
outputFileName += L".txt2"; // Add .OUT
}
// Open the input file
hIn = CreateFileW(inputFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hIn == INVALID_HANDLE_VALUE) {
std::wcerr
Подробнее здесь: [url]https://stackoverflow.com/questions/79158927/how-do-i-make-any-language-work-with-my-code[/url]