I am trying to set up a mailslot that I can write my values and read from. In my case, I have two processes, which work with each other. The first process gets information from a user and sends the information another process to calculate the result. The second process should read the data from mailslot, but the process was freezing when the function ReadFile() has been called and I get nothing. I tried to find some information about the mailslot, but I didn't get that I wanted. Who knows about this mailslot and could help me?
The first process:
int main()
{
HANDLE m = CreateMutex(NULL, FALSE, L"MyMutex");
if (m == 0)
return -1;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CreateProcess(L"c:\\Users\\user\\Desktop\\Lab3\\Procces1\\Debug\\Procces1.exe", NULL,
NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi) == TRUE) {
bool flagEnd = true;
double a,b,c;
HANDLE file = CreateFile(L"\\\\.\\mailslot\\c:\\Users\\user\\Desktop\\Lab3\\mail", GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == 0)
return -1;
while (true) {
WaitForSingleObject(m, INFINITE);
cout << "Please enter A and B values\n";
cout << "A = ";
cin >> a;
cout << "B = ";
cin >> b;
WriteFile(file, &a, sizeof(double), NULL, NULL);
WriteFile(file, &b, sizeof(double), NULL, NULL);
ReleaseMutex(m);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(m);
}
else {
cout << "Process has not been created\n";
}
return 0;
}
The second process:
int main()
{
HANDLE mut = OpenMutex(MUTEX_ALL_ACCESS, TRUE, L"MyMutex");
if (mut == 0)
return -1;
HANDLE mail = CreateMailslot(L"\\\\.\\mailslot\\c:\\Users\\user\\Desktop\\Lab3\\mail", 0, MAILSLOT_WAIT_FOREVER, NULL);
if (mail == 0)
return -1;
double a, b, c;
DWORD byte;
while (true) {
WaitForSingleObject(mut, INFINITE);
cout << "Reading\n"; //this works
ReadFile(mail, &a, sizeof(double), &byte, NULL);
ReadFile(mail, &b, sizeof(double), &byte, NULL);
cout << "I have read " << a << " " << b << endl; //this message doesn't send
ReleaseMutex(mut); //and therefore releasemutex wont be called
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire