I think you could do with some more experience in basic C before tackling advanced topics such as mailslots.
See comments and bolded sections inline.
Master:
Code:
#include <windows.h>
#include <iostream>
int main()
{
HANDLE hSlot1, hSlot2, hSlot3;
STARTUPINFO startup_info = {0};
PROCESS_INFORMATION pi = {0};
startup_info.cb = sizeof startup_info;
DWORD dwWritten; /* WriteFile wants a LPDWORD - give it one. ie. address of dwWritten. */
char toPrint = 'a';
CreateProcess("D:\\ipc\\opdracht\\debug\\printer.exe",
" \\\\.\\mailslot\\slot1", /* Pass the COMPLETE mailslot name here */
NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startup_info, &pi);
CreateProcess("D:\\ipc\\opdracht\\debug\\printer.exe",
" \\\\.\\mailslot\\slot2",
NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startup_info, &pi);
CreateProcess("D:\\ipc\\opdracht\\debug\\print.exe",
" mailslot 3 name here",
NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startup_info, &pi);
/* strcat("\\\\.\\mailslot\\",msname) will NOT work.
* Just use string literal mailslot names for now. */
hSlot1 = CreateFile("\\\\.\\mailslot\\slot1", GENERIC_WRITE, FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
/* Complete these lines.
hSlot2 = CreateFile("\\\\.\\mailslot\\slot2", ...);
hSlot3 = ...
*/
WriteFile(hSlot1, &toPrint, sizeof(toPrint), &dwWritten, NULL);
/* Complete these lines.
WriteFile(hSlot2, ...);
WriteFile(hSlot3, ...);
*/
CloseHandle(hSlot1);
/* Close hSlot2 and hSlot3 also. */
return 0;
}
Output Program:
Code:
#include <stdio.h> /* For standard headers use <...> */
#include <windows.h>
void showChar(char character) /* We take a single character as argument. */
{
printf("character= %c \n", character); /* Print the single character. */
}
int main(int argc,char * argv[]){
DWORD dwRead; /* ReadFile wants a LPDWORD so give it the address of a DWORD - not an int. */
/* As we have fixed the master program we know that argv[1] should contain
* the COMPLETE mailslot name.
* Check this by printing argv[1] with printf. */
printf("Mailslot name: %s\n", argv[1]);
HANDLE hSlot = CreateMailslot(argv[1], 0, MAILSLOT_WAIT_FOREVER, NULL);
while (TRUE)
{
/* We can not use argv[1] as our buffer for ReadFile.
* We must create our own. */
char buf[32];
ReadFile(hSlot, buf, sizeof(buf), &dwRead, NULL); /* Read into buf. */
showChar(buf[0]); /* Print the first character in buf. */
}
return 0;
}
>>- with the question wether the mailslot name (slot3) is correctly transferred.<<
You were not transferring the complete mailslot name. This meant the mailslot was not being created.
>>- Is the (LPDWORD)&n in the writeFile correct, i just made an var named n and then converted it in the function to a LPDWORD which seems to work for compilation, but i dont know if i need to use n somewhere....<<
I have renamed n to dwWritten for clarity. It must be provided to WriteFile so that it can return how many characters are actually written.
>>- instead of an n like in the main program i here used a var dwRead which isnt used anywhere, but i cant figure out IF i need it anywhere....<<
It must be provided to ReadFile, even if you do not need the value that is returned.
>>- i created the mailslot and am trying to retrieve information, but i am doubting if the name i gave in the main program came through...<<
I have added a printf to print out argv[1] so you can check this.
>>- an finally: i want to use showChar to print the character i specified in the main program, but how should i make this clear to the function? I dont think i can use arg[1] since that would be the name of the mailslot... right? I guess either argv[1] is OR the mailslot name OR the character to print... but i cant figure out the precise what and how...<<
As mentioned in my comments you can not use argv[1] as the buffer you pass to ReadFile.