我正在尝试使用C#创建Skype的附加组件。我不想要使用Skype4COM,因为我喜欢有关消息等的体验。不幸的是,这些消息让我感到沮丧。我有泵和这样的设置。它们全部工作,我的应用程序成功地向Skype发送“APIDiscover”消息,得到“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?
答案 0 :(得分:0)
嗯,正如我所预料的那样,它很小。 Skype API说(不要问我原因)处理邮件的结果必须与零(0)不同,否则Skype会认为连接已断开。
当然,消息的默认返回值为0. Skype将从初始ApiConnectSuccess消息中获取Zero,并立即关闭句柄。当然,当我尝试使用手柄时产生了1400!所以只需设置
即可m.Result = new IntPtr(1);
修好了。现在,我收到“拒绝访问”错误。真有趣。
编辑:通过将IntPtr.Zero替换为我的句柄来修复“访问被拒绝”(Win32错误5):
SendMessageA(mHelper.SkypeAPIWindowHandle, MsgHelper.WM_COPYDATA, mHelper.MyHandle, ref cda)
一个中有两个答案!