简体   繁体   中英

c# SendMessage and Skype woes

I'm trying to create an add-on to Skype with C#. I don't want to use Skype4COM, as I'd like the experience with messages and such. Unfortunately, the messages are tripping me up. I've got the pumps and such set up. They all work, and my app successfully sends the "APIDiscover" message to Skype, gets a "PendingAuth" response and then the "AttachSuccess" message. However, when I try to send "ping" to Skype (to which it should reply "pong") nothing happens.

The return code from SendMessage is 0 but Marshall.GetLastWin32Error is 1400 (Invalid handle). The handle was returned with the AttachSuccess method. The equivalent C++ code does work, so I'm at a loss.

First is the C++ code I'm using as a guide: Here's the (cut down) message pump. You can ignore everything but where I put the //<----

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);
}

and this is the (again dumbed down) "sending message" code

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;
}

And now here's my 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
    }

And here is my DLL import and Sending code

    [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 is:

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

I think that's everything. Let me know if I forgot something.

Any ideas why I'm getting the 1400?

Well, as I expected, it was something small. Skype API says (don't ask me why) The result of processing the message must be different from zero (0), otherwise Skype considers that the connection broken.

And, of course, the default return value of a message is 0. Skype would get Zero from the initial ApiConnectSuccess message, and immediately close the handle. Of course that generated a 1400 when I tried to use the handle! So simply setting

m.Result = new IntPtr(1);

fixed it. Now, I'm getting an "Access denied" error. What fun.

Edit: Fixed the "Access is Denied" (Win32 error 5) by replacing IntPtr.Zero with my handle in:

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

Two answers in one!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM