Comment passer un CString sur un pipe ?
Bonjour tout le monde,
J'ai deux processus P1 et P2 qui tournent dans deux comptes différents sur le même pc (pour mon cas, l'un dans le compte SYSTEM et l'autre dans le compte user ) et qui veulent s'échanger des informations.
J'ai un exemple qui tourne avec un format de message
Citation:
char szBuffer[BUFFER_SIZE]
mais quand je veux faire cette communication en utilisant un CString au départ, ça passe pas ( en faite, je reçois une chaîne déformée.
Je vous mets le code pour plus de détails:
1) Code qui marche
******************************************************************************************************************
Code de l'émetteur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
char szBuffer[1024];
printf("\nEnter a message to be sent to the server: ");
gets(szBuffer);
BOOL bResult = WriteFile(
hPipe, // handle to pipe
&szBuffer, // buffer to write from
strlen(szBuffer)+1, // number of bytes to write, include the NULL
&cbBytes, // number of bytes written
NULL); // not overlapped I/O
if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
{
printf("\nError occurred while writing to the server: %d", GetLastError());
CloseHandle(hPipe);
return 1; //Error
}
else
{
printf("\nWriteFile() was successful.");
} |
Code du récepteur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
char szBuffer[1024];
BOOL bResult = ReadFile( // this function is gona wait until something is read from the pipe
hPipe, // handle to pipe
&szBuffer, // buffer to receive data
strlen(szBuffer), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ( (!bResult) || (0 == cbBytes))
{
printf("\nError occurred while reading from the client: %d", GetLastError());
CloseHandle(hPipe);
return 1; //Error
}
else
{
CString msg=szBuffer;
printf("\nReadFile() was successful.");
WriteMessageToCSVFile(msg);
} |
1) Code qui ne marche pas
******************************************************************************************************************
Code de l'émetteur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
CString msg="salut";
BOOL bResult = WriteFile(
hPipe, // handle to pipe
&msg, // buffer to write from
msg.GetLength(), // number of bytes to write, include the NULL
&cbBytes, // number of bytes written
NULL); // not overlapped I/O
if ( (!bResult) || (msg.GetLength() != cbBytes))
{
printf("\nError occurred while writing to the server: %d", GetLastError());
CloseHandle(hPipe);
return 1; //Error
}
else
{
printf("\nWriteFile() was successful.");
} |
Code du récepteur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
CString ReceivedMsg;
BOOL bResult = ReadFile( // this function is gona wait until something is read from the pipe
hPipe, // handle to pipe
&ReceivedMsg, // buffer to receive data
ReceivedMsg.GetLength(), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ( (!bResult) || (0 == cbBytes))
{
printf("\nError occurred while reading from the client: %d", GetLastError());
CloseHandle(hPipe);
return 1; //Error
}
else
{
printf("\nReadFile() was successful.");
WriteMessageToCSVFile(msg);
} |
Résultat: Erreur
Citation:
Error occurred while reading from the client: 234
le 234
Alors j'aimerai bien savoir où est l'erreur ? et comment la corriger ?
Merci d'avance :)