我正在尝试使用C#创建Skype的附件。我不想使用Skype4COM,因为我希望有消息之类的经验。不幸的是,这些消息使我震惊。我有水泵和这样的装置。它们都正常工作,并且我的应用程序成功将“ APIDiscover”消息发送到Skype,获得了“ PendingAuth”响应,然后获得了“ AttachSuccess”消息。但是,当我尝试向Skype发送“ ping”(应该向其回复“ pong”)时,没有任何反应。

SendMessage的返回码为0,但Marshall.GetLastWin32Error为1400(无效句柄)。使用AttachSuccess方法返回了该句柄。等效的C ++代码确实可以工作,所以我很茫然。

首先是我用作指导的C ++代码:
这是(减少的)消息泵。您可以忽略所有内容,但可以放在//

static LRESULT APIENTRY SkypeAPITest_Windows_WindowProc(
HWND hWindow, UINT uiMessage, WPARAM uiParam, LPARAM ulParam)
{
LRESULT lReturnCode;
bool fIssueDefProc;

lReturnCode=0;
fIssueDefProc=false;
switch(uiMessage)
    {
    case WM_COPYDATA:
        if( hGlobal_SkypeAPIWindowHandle==(HWND)uiParam )
            {
            PCOPYDATASTRUCT poCopyData=(PCOPYDATASTRUCT)ulParam;
            printf( "Message from Skype(%u): %.*s\n", poCopyData->dwData, poCopyData->cbData, poCopyData->lpData);
            lReturnCode=1;
            }
        break;
    default:
        if( uiMessage==uiGlobal_MsgID_SkypeControlAPIAttach )
            {
            switch(ulParam)
                {
                case SKYPECONTROLAPI_ATTACH_SUCCESS:
                    printf("!!! Connected; to terminate issue #disconnect\n");
                    hGlobal_SkypeAPIWindowHandle=(HWND)uiParam;//<---- Right here is where we receive  the handle from Skype.
                    break;
    }
if( fIssueDefProc )
    lReturnCode=DefWindowProc( hWindow, uiMessage, uiParam, ulParam);
return(lReturnCode);
}


这是(再次愚蠢的)“发送消息”代码

void __cdecl Global_InputProcessingThread(void *)
{
static char acInputRow[1024];
bool fProcessed;

if( SendMessageTimeout( HWND_BROADCAST, uiGlobal_MsgID_SkypeControlAPIDiscover, (WPARAM)hInit_MainWindowHandle, 0, SMTO_ABORTIFHUNG, 1000, NULL)!=0 )
    {
    while(Global_Console_ReadRow( acInputRow, sizeof(acInputRow)-1))
        {
        if( fProcessed==false && hGlobal_SkypeAPIWindowHandle!=NULL )
            {
            COPYDATASTRUCT oCopyData;

            // send command to skype
            oCopyData.dwData=0;
            oCopyData.lpData=acInputRow;
            oCopyData.cbData=strlen(acInputRow)+1;
            if( oCopyData.cbData!=1 )
                {
                if( SendMessage( hGlobal_SkypeAPIWindowHandle, WM_COPYDATA, (WPARAM)hInit_MainWindowHandle, (LPARAM)&oCopyData)==FALSE )
                    {
                    hGlobal_SkypeAPIWindowHandle=NULL;
                    printf("!!! Disconnected\n");
                    }
                }
            }
        }
    }
SendMessage( hInit_MainWindowHandle, WM_CLOSE, 0, 0);
SetEvent(hGlobal_ThreadShutdownEvent);
fGlobal_ThreadRunning=false;
}


现在是我的C#

    public bool PreFilterMessage(ref Message m)
    {
        Console.WriteLine(m.ToString());
        if (m.Msg == WM_COPYDATA && SkypeAPIWindowHandle == m.WParam)
        {
            SkypeMessage(m);
            return true;
        }
        if (m.Msg == MsgApiAttach)
        {
            switch (m.LParam.ToInt32())
            {
                case (int)SkypeControlAPIAttach.SUCCESS:
                    SkypeAPIWindowHandle = m.WParam; //Here's where we set the Skype Handle
                    AttachSuccess(m);
                    return true;
            }
        }
        return false; //Defer all other messages
    }


这是我的DLL导入和发送代码

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessageA(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref MsgHelper.COPYDATASTRUCT lParam);

    public static void Command(string c)
    {
        if (c.Last() != '\0')
            c += "\0"; //Make string null terminated
        Console.WriteLine();
        MsgHelper.COPYDATASTRUCT cda = new MsgHelper.COPYDATASTRUCT();
        cda.dwData = new IntPtr(0);
        cda.lpData = c;
        cda.cbData = c.Length + 1;
        Marshal.GetLastWin32Error(); //Clear last error
        Console.WriteLine(SendMessageA(mHelper.SkypeAPIWindowHandle, MsgHelper.WM_COPYDATA, IntPtr.Zero, ref cda));
        Console.WriteLine(Marshal.GetLastWin32Error());
    }


COPYDATASTRUCT是:

    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;
        public int cbData;
        [MarshalAs(UnmanagedType.LPStr)]
        public string lpData;
    }


我认为这就是一切。让我知道我是否忘记了什么。

我为什么要买1400的任何想法?

最佳答案

好吧,正如我所期望的,它很小。 Skype API说(不要问我为什么),处理消息的结果必须不同于零(0),否则Skype认为连接已断开。

并且,当然,消息的默认返回值为0。Skype将从初始ApiConnectSuccess消息中获取零,然后立即关闭句柄。当然,当我尝试使用手柄时会产生1400!所以只需设置

m.Result = new IntPtr(1);


固定它。现在,我收到“访问被拒绝”错误。真有趣

编辑:通过将IntPtr.Zero替换为我的句柄,修复了“访问被拒绝”(Win32错误5):

SendMessageA(mHelper.SkypeAPIWindowHandle, MsgHelper.WM_COPYDATA, mHelper.MyHandle, ref cda)


两个答案合而为一!

关于c# - C#SendMessage和Skype的困境,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10876296/

10-15 03:53