2

我正在尝试使用 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?

4

1 回答 1

0

嗯,正如我所料,这是一件小事。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)

两个答案合二为一!

于 2012-06-04T19:24:51.107 回答