我有这个结构:
typedef struct tagCOPY_PACKET
{
TCHAR szFile[_MAX_PATH];
GUID guidSignature;
} S_COPY_PACKET;
我准备WM_COPYDATA
像这样发送数据:
CString strFile = GetFileToOpenFromFileExplorerPath();
S_COPY_PACKET sCopyDataPacket;
_tcscpy_s(sCopyDataPacket.szFile, _MAX_PATH, strFile);
sCopyDataPacket.guidSignature = CopyData_Signature;
COPYDATASTRUCT cds;
cds.dwData = COPYDATA_TYPE_MSA;
cds.cbData = sizeof(sCopyDataPacket);
cds.lpData = &sCopyDataPacket;
DWORD_PTR dwResult;
if (SendMessageTimeout(hOtherInstance, WM_COPYDATA,
NULL, (LPARAM)(LPVOID)&cds, SMTO_BLOCK, 2000, &dwResult) != 0)
{
}
在另一端:
BOOL CMeetingScheduleAssistantDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
if (pCopyDataStruct->dwData == COPYDATA_TYPE_MSA)
{
S_COPY_PACKET* pCopyPacket = (S_COPY_PACKET*)(pCopyDataStruct->lpData);
if (pCopyPacket->guidSignature != CopyData_Signature)
{
// Bad GUID
return FALSE;
}
if (GetLastActivePopup() != this) // Popup windows!
{
// TODO: Tell user?
return FALSE;
}
theApp.SetFileToOpenFromFileExplorer(pCopyPacket->szFile);
OpenFileFromFileExplorer();
return TRUE;
}
return FALSE;
}
它工作正常。我只是想知道这样改变我的结构是否可以接受:
typedef struct tagCOPY_PACKET
{
LPCTSTR lpszFile;
GUID guidSignature;
} S_COPY_PACKET;
然后使用:
S_COPY_PACKET sCopyDataPacket;
sCopyDataPacket.lpszFile = strFile.GetBufferSetLength(_MAX_PATH);
sCopyDataPacket.guidSignature = CopyData_Signature;
COPYDATASTRUCT cds;
cds.dwData = COPYDATA_TYPE_MSA;
cds.cbData = sizeof(sCopyDataPacket);
cds.lpData = &sCopyDataPacket;
...
并且,一旦消息被发布和处理:
strFile.ReleaseBuffer();
使用这种LPCTSTR
方法是否安全WM_COPYDATA
,如果是,为什么?