Utilisation de la copie de données
L’exemple suivant montre comment envoyer des informations entre deux applications à l’aide du message WM_COPYDATA .
L’application d’envoi affiche une boîte de dialogue à l’utilisateur qui demande certaines informations. L’application empaquete les informations dans une structure de données privée, inclut un pointeur vers la structure dans la structure COPYDATASTRUCT et envoie les informations à l’application de réception à l’aide du message WM_COPYDATA . L’application réceptrice a une fenêtre masquée avec le nom de classe Disp32Class.
// ************ Globals ************
//
#define MYDISPLAY 1
typedef struct tagMYREC
{
char s1[80];
char s2[80];
DWORD n;
} MYREC;
COPYDATASTRUCT MyCDS;
MYREC MyRec;
HRESULT hResult;
BOOL CALLBACK InfoDlgProc( HWND, UINT, WPARAM, LPARAM );
// ************ Code fragment ****************
// Get data from user. InfoDlgProc stores the information in MyRec.
//
DialogBox( ghInstance, "InfoDlg", hWnd, (DLGPROC) InfoDlgProc );
//
// Copy data into structure to be passed via WM_COPYDATA.
// Also, we assume that truncation of the data is acceptable.
//
hResult = StringCbCopy( MyRec.s1, sizeof(MyRec.s1), szFirstName );
if (hResult != S_OK)
return False;
hResult = StringCbCopy( MyRec.s2, sizeof(MyRec.s2), szLastName );
if (hResult != S_OK)
return False;
MyRec.n = nAge;
//
// Fill the COPYDATA structure
//
MyCDS.dwData = MYPRINT; // function identifier
MyCDS.cbData = sizeof( MyRec ); // size of data
MyCDS.lpData = &MyRec; // data structure
//
// Call function, passing data in &MyCDS
//
hwDispatch = FindWindow( "Disp32Class", "Hidden Window" );
if( hwDispatch != NULL )
SendMessage( hwDispatch,
WM_COPYDATA,
(WPARAM)(HWND) hWnd,
(LPARAM) (LPVOID) &MyCDS );
else
MessageBox( hWnd, "Can't send WM_COPYDATA", "MyApp", MB_OK );
L’application réceptrice a une fenêtre masquée qui reçoit les informations de WM_COPYDATA et les affiche à l’utilisateur.
// ************ Globals ************
//
#define MYDISPLAY 1
typedef struct tagMYREC
{
char s1[80];
char s2[80];
DWORD n;
} MYREC;
PCOPYDATASTRUCT pMyCDS;
void WINAPI MyDisplay( LPSTR, LPSTR, DWORD );
//
// ************ Code fragment ****************
//
case WM_COPYDATA:
pMyCDS = (PCOPYDATASTRUCT) lParam;
switch( pMyCDS->dwData )
{
case MYDISPLAY:
MyDisplay( (LPSTR) ((MYREC *)(pMyCDS->lpData))->s1,
(LPSTR) ((MYREC *)(pMyCDS->lpData))->s2,
(DWORD) ((MYREC *)(pMyCDS->lpData))->n );
}
break;
Rubriques connexes
-
Informations de référence
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour