发送端:   VC2008 + XP   //字符集:使用多字节字符集 , 使用Unicode字符集无法编译,此程序使用MFC的共享动态库规则
    extern "C" BOOL PASCAL EXPORT cookie()
{
CString strTmp = "";
if (handle_cookie > 0)
{
CString strData = "in data ";
for (int i = 0 ;i < 10 ;i ++)
{ strTmp.Format("%s =%d",strData,i);             
COPYDATASTRUCT cds;
cds.dwData = 0;
cds.cbData = strData.GetLength()+ 1;
cds.lpData = (void *)strData.GetBuffer(cds.cbData);
SendMessage(handle_cookie, WM_COPYDATA, NULL, (LPARAM)&cds);
} }
return FALSE ;
}
  
接收端: VB + XP 
Private pData As COPYDATASTRUCT
Public pRecData As Long
Public sData As StringPublic Function RegMsg()
    preWinProc = GetWindowLong(Form1.hwnd, GWL_WNDPROC)
    ret = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf GetURLDataProc)
End Function
Option ExplicitPublic Type COPYDATASTRUCT
        dwData As Long
        cbData As Long
        lpData As Long
End TypeDeclare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Sub OutputDebugString Lib "kernel32" Alias "OutputDebugStringA" (ByVal lpOutputString As String)
Public preWinProc As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_COPYDATA = &H4A
Public Const WM_MY_COPYDATA = &H401
Public ret As LongPublic Function GetURLDataProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If Msg = WM_COPYDATA Then
        Dim cds_Data As COPYDATASTRUCT
        Dim sData$
        CopyMemory ByVal VarPtr(cds_Data), ByVal lParam, ByVal Len(cds_Data)
    
        sData = Space$(cds_Data.cbData)
        CopyMemory ByVal StrPtr(sData), ByVal cds_Data.lpData, ByVal cds_Data.cbData
        OutputDebugString "tp===" & sData
    End If
    GetURLDataProc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function现在出现的问题是发什么,接收什么都是乱码?
     

解决方案 »

  1.   

    既然送过来的不是 Unicode,你需要自己进行转换。
    OutputDebugString "tp===" & StrConv(sData, vbUnicode)
      

  2.   

        extern "C" BOOL PASCAL EXPORT cookie()
    {
    CString strTmp = "";
    if (handle_cookie > 0)
    {
    CString strData = "in data ";
    for (int i = 0 ;i < 10 ;i ++)
    {strTmp.Format("%s =%d",strData,i);           
    COPYDATASTRUCT cds;
    cds.dwData = 0;
    cds.cbData = strData.GetLength();
    cds.lpData = (void *)strData.GetBuffer(cds.cbData);
    SendMessage(handle_cookie, WM_COPYDATA, NULL, (LPARAM)&cds);

    Public Function GetURLDataProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If Msg = WM_COPYDATA Then
    '        Dim cds_Data As COPYDATASTRUCT
    '        Dim sData$
    '        CopyMemory ByVal VarPtr(cds_Data), ByVal lParam, ByVal Len(cds_Data)
    '
    '        sData = Space$(cds_Data.cbData)
    '        CopyMemory ByVal StrPtr(sData), ByVal cds_Data.lpData, ByVal cds_Data.cbData
    '        OutputDebugString "tp===" & sData
            Dim cds As COPYDATASTRUCT
            Dim buf(1 To 1024) As Byte
            Dim ret As String
            Dim ret2 As String
            Dim ret_str() As String
            Call CopyMemory(cds, ByVal lParam, Len(cds))
            Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
            ret = StrConv(buf, vbUnicode)
            ret = Left$(ret, InStr(1, ret, Chr$(0)) - 1)
            OutputDebugString "COPYDATA:" & ret
            ret_str = Split(ret, "+")
            Form1.ListView1.ListItems.Add , , ret_str(0)
            Form1.ListView1.ListItems.Item.SubItems
        End If
        GetURLDataProc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
    End Function通过一高手的指导问题已解决,主要的原因还是VB上内存操作的问题,通过消息传送的数据要先把其地址存起来,并用数组去存,再转换为unicode就行了; 经过测试英文、中文数据都没有问题,现把正确的代码贴出来方便大家学习;