You can also play Skype--get started with the development shell based on the Skype API

Source: Internet
Author: User

Original: You can also play Skype--get started with the development shell based on the Skype API

Skype is by far the most powerful Im+voip software on the planet, and Skype has now changed the way 280 million people live in the world. You, deserve to have! :)

Skype Chinese official website: http://skype.tom.com/

Skype Global official website: http://www.skype.com/

Skype is also the most open and innovative IM tool in the world, he offers Skype API, skype4com, Skype4java Several forms of development interface for Skype enthusiasts to write Skype interactive programs or Skype plugins. You can use any language you are familiar with, such as C/C++,VB, C#,delphi,java or even php,vbscript. Use your expertise to influence 280 million of Skype users. You can do it too! :)

Skype Global developer Community: http://developer.skype.com/

Below we will show one of the simplest C + + code to access the Skype API:

Here you can download the corresponding VC6 project: Http://wh.hust.colin.googlepages.com/SkypeInteractiveDemo.rar

//
Copyright (c) 2004-2006, Skype Limited.
All rights reserved.
//
Redistribution and use in source and binary forms, with or without
Modification, is permitted provided that the following conditions
Is met:
//
* Redistributions of source code must retain the above copyright
Notice, this list of conditions and the following disclaimer.
* redistributions in binary form must reproduce the above
Copyright notice, this list of conditions and the following
Disclaimer in the documentation and/or other materials provided
With the distribution.
* Neither the name of the Skype Limited nor the names of its
Contributors May is used to endorse or promote products derived
From this software without specific prior written permission.
//
This software are provided by the COPYRIGHT holders and CONTRIBUTORS
' As is ' and any EXPRESS OR implied warranties, including, but not
LIMITED to, the implied warranties of merchantability and FITNESS
For A particular PURPOSE is disclaimed. In NO EVENT shall the
COPYRIGHT OWNER OR CONTRIBUTORS be liable for any DIRECT, INDIRECT,
Incidental, special, exemplary, OR consequential damages (including,
LIMITED to, procurement of substitute GOODS OR SERVICES;
LOSS of Use, DATA, OR profits; OR business Interruption) however
caused and on any theory of liability, WHETHER in contract, STRICT
Liability, or TORT (including negligence or OTHERWISE) arising in
Any-on-the-software, even IF advised of the
Possibility of SUCH DAMAGE.
//

tab size:2

#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <process.h>
#include <rpcdce.h>

HWND Hinit_mainwindowhandle; < handle to the window created by this program
HINSTANCE Hinit_processhandle; < process handle for this program
Char acinit_windowclassname[128]; < window class name created by this program
HANDLE hglobal_threadshutdownevent;
BOOL volatile fglobal_threadrunning=true;

Skype-defined message Id,skype informs the result of a request connection by sending such a message to a third-party program
UINT Uiglobal_msgid_skypecontrolapiattach;

Skype-defined message ID, when a third-party program wants to get Skype's interaction,
This message must be sent via broadcast (HWND_BROADCAST), and Skype will be prompted for the user after it is received
When the user allows, the interaction is set up.
UINT Uiglobal_msgid_skypecontrolapidiscover;

Windows handles for Skype
HWND Hglobal_skypeapiwindowhandle=null;

BOOL variable identifies whether to print more detailed message content
#if defined (_DEBUG)
BOOL volatile fglobal_dumpwindowsmessages=true;
#else
BOOL volatile fglobal_dumpwindowsmessages=false;
#endif
DWORD ulglobal_promptconsolemode=0;
HANDLE volatile hglobal_promptconsolehandle=null;

enum {
A third-party program is successfully connected, and you can get a handle to the Skype API window in wparam
Skypecontrolapi_attach_success=0,
Skype has received a connection request and has prompted a third-party program to request access to Skype
This time the connection is not successfully established, it must wait until the skypecontrolapi_attach_success message.
Skypecontrolapi_attach_pending_authorization=1,
Skype user denied access request for third-party programs
skypecontrolapi_attach_refused=2,
The API interface is not currently available. There are times when this happens, for example, there are no users logged in to Skype for the moment.
Third-party programs must wait for Skype to broadcast a skypecontrolapi_attach_api_available message before attempting to connect is valid
Skypecontrolapi_attach_not_available=3,
skypecontrolapi_attach_api_available=0x8001
};

Get a line of input into Pacpromptbuffer from the standard input window
BOOL Global_console_readrow (char *pacpromptbuffer, unsigned int uimaxlength)
{
HANDLE Hconsolehandle, Hduplicatedconsolehandle;
DWORD Ulcharactersread, Ulconsolemode;
unsigned int uinewlength;
BOOL Freadconsoleresult;
BOOL Freturnstatus;
Char Ccharacter;

Freturnstatus=false;
Gets the input buffer handle for the standard input window
while ((Hconsolehandle=getstdhandle (Std_input_handle))!=invalid_handle_value)
{
if (DuplicateHandle (GetCurrentProcess (), Hconsolehandle,
GetCurrentProcess (), &hduplicatedconsolehandle, 0, FALSE,
duplicate_same_access) ==false)
Break
Getconsolemode (Hduplicatedconsolehandle, &ulconsolemode);
Setconsolemode (Hduplicatedconsolehandle, enable_line_input| Enable_processed_input| Enable_echo_input);
Hglobal_promptconsolehandle=hduplicatedconsolehandle;
Ulglobal_promptconsolemode=ulconsolemode;
Freadconsoleresult=readconsole (Hglobal_promptconsolehandle,
(LPVOID) Pacpromptbuffer, Uimaxlength, &ulcharactersread, (LPVOID) 0);
if (hglobal_promptconsolehandle== (HANDLE) 0)
Break
hglobal_promptconsolehandle= (HANDLE) 0;
Setconsolemode (Hduplicatedconsolehandle, Ulconsolemode);
CloseHandle (Hduplicatedconsolehandle);
if (Freadconsoleresult==false | | ulcharactersread>uimaxlength)
Break
pacpromptbuffer[ulcharactersread]=0;
Uinewlength=ulcharactersread;
while (uinewlength!=0)
{
CCHARACTER=PACPROMPTBUFFER[UINEWLENGTH-1];
if (ccharacter!= '/R ' && ccharacter!= '/n ')
Break
uinewlength--;
}
pacpromptbuffer[uinewlength]=0;
Freturnstatus=true;
Break
}
if (Freturnstatus==false)
pacpromptbuffer[0]=0;
return (Freturnstatus);
}

Recycle cleanup of input consoles
void Global_console_cancelreadrow (void)
{
if (hglobal_promptconsolehandle!= (HANDLE) 0)
{
Setconsolemode (Hglobal_promptconsolehandle, Ulglobal_promptconsolemode);
CloseHandle (Hglobal_promptconsolehandle);
hglobal_promptconsolehandle= (HANDLE) 0;
}
}

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_destroy:
Hinit_mainwindowhandle=null;
PostQuitMessage (0);
Break
Case WM_COPYDATA:
Skype uses wm_copydata with third-party programs for message delivery
When Skype sends a message through Wm_copydata to all connected third-party programs, the Skype window handle is placed in Uiparam
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 the message type is Uiglobal_msgid_skypecontrolapiattach, it indicates the connection-related
if (Uimessage==uiglobal_msgid_skypecontrolapiattach)
{
Switch (Ulparam)
{
Case Skypecontrolapi_attach_success:
printf ("!!! Connected; To terminate issue #disconnect/n ");
Hglobal_skypeapiwindowhandle= (HWND) Uiparam;
Break
Case Skypecontrolapi_attach_pending_authorization:
printf ("!!! Pending authorization/n ");
Break
Case skypecontrolapi_attach_refused:
printf ("!!! Connection refused/n ");
Break
Case Skypecontrolapi_attach_not_available:
printf ("!!! Skype API not available/n ");
Break
Case Skypecontrolapi_attach_api_available:
printf ("!!! Try Connect Now (API available); Issue #connect/n ");
Break
}
lreturncode=1;
Break
}
Fissuedefproc=true;
Break
}
if (Fissuedefproc)
Lreturncode=defwindowproc (Hwindow, Uimessage, Uiparam, Ulparam);
if (fglobal_dumpwindowsmessages)
{
printf ("windowproc:hwindow=0x%08x, mainwindow=0x%08x, message=%5u, wparam=0x%08x, lparam=0x%08x; return=%ld%s/n ",
Hwindow, Hinit_mainwindowhandle, Uimessage, Uiparam, Ulparam, Lreturncode, Fissuedefproc? "(Default)": "");
}
return (Lreturncode);
}

BOOL Initialize_createwindowclass (void)
{
unsigned char *paucuuidstring;
Rpc_status Luuidresult;
BOOL Freturnstatus;
UUID Ouuid;

Freturnstatus=false;
Luuidresult=uuidcreate (&AMP;OUUID);
Hinit_processhandle= (HINSTANCE) openprocess (Process_dup_handle, FALSE, GetCurrentProcessId ());
if (hinit_processhandle!=null && (LUUIDRESULT==RPC_S_OK | | luuidresult==rpc_s_uuid_local_only))
{
if (UuidToString (&ouuid, &paucuuidstring) ==RPC_S_OK)
{
Wndclass Owindowclass;
Generate window class name (with UUID)
strcpy (Acinit_windowclassname, "skype-api-test-");
Strcat (Acinit_windowclassname, (char *) paucuuidstring);

Owindowclass.style=cs_hredraw|cs_vredraw|cs_dblclks;
The message callback function for the specified window is Skypeapitest_windows_windowproc
Owindowclass.lpfnwndproc= (WNDPROC) &SkypeAPITest_Windows_WindowProc;
owindowclass.cbclsextra=0;
owindowclass.cbwndextra=0;
Owindowclass.hinstance=hinit_processhandle;
Owindowclass.hicon=null;
Owindowclass.hcursor=null;
Owindowclass.hbrbackground=null;
Owindowclass.lpszmenuname=null;
Owindowclass.lpszclassname=acinit_windowclassname;
Register window class
if (RegisterClass (&owindowclass)!=0)
Freturnstatus=true;

Rpcstringfree (&paucuuidstring);
}
}
if (Freturnstatus==false)
CloseHandle (Hinit_processhandle), hinit_processhandle=null;
return (Freturnstatus);
}

void Deinitialize_destroywindowclass (void)
{
Logoff window class
Unregisterclass (Acinit_windowclassname, Hinit_processhandle);
CloseHandle (Hinit_processhandle), hinit_processhandle=null;
}

BOOL Initialize_createmainwindow (void)
{
Create a window and save the handle to Hinit_mainwindowhandle
Hinit_mainwindowhandle=createwindowex (ws_ex_appwindow| Ws_ex_windowedge,
Acinit_windowclassname, "", ws_border| ws_sysmenu| Ws_minimizebox,
Cw_usedefault, Cw_usedefault, 0, hinit_processhandle, 0);
Return (Hinit_mainwindowhandle!=null true:false);
}

void Deinitialize_destroymainwindow (void)
{
Destroying Windows
if (hinit_mainwindowhandle!=null)
DestroyWindow (Hinit_mainwindowhandle), hinit_mainwindowhandle=null;
}

void Global_messageloop (void)
{
Message processing loops
MSG omessage;
while (GetMessage (&omessage, 0, 0, 0)!=false)
{
TranslateMessage (&omessage);
DispatchMessage (&omessage);
}
}

This is a thread function that is used primarily to receive the user's input instructions and perform
void __cdecl global_inputprocessingthread (void *)
{
static Char acinputrow[1024];
BOOL fprocessed;

When a thread starts executing, it defaults to connecting to Skype, sending a request to access the Skype API message. Since we didn't know the Skype API window handle value at first, we can only
Broadcast this message via HWND_BROADCAST to all systems in the foreground window, and if the Skype window exists, you will naturally receive it.
if (SendMessage (Hwnd_broadcast, Uiglobal_msgid_skypecontrolapidiscover, (WPARAM) hinit_mainwindowhandle, 0)!=0)
{
Accept user Input Instructions
while (Global_console_readrow (Acinputrow, sizeof (Acinputrow)-1))
{
Exit program
if (stricmp (Acinputrow, "#quit") ==0 | |
STRICMP (Acinputrow, "#exit") ==0)
Break
Fprocessed=false;
Turn on display message details
if (stricmp (Acinputrow, "#dbgon") ==0)
{
printf ("skypecontrolapiattach=%u, skypecontrolapidiscover=%u, hinit_mainwindowhandle=0x%08lx/n",
Uiglobal_msgid_skypecontrolapiattach, Uiglobal_msgid_skypecontrolapidiscover, Hinit_mainwindowhandle);
Fglobal_dumpwindowsmessages=true,fprocessed=true;
}
Turn off display message details
if (stricmp (Acinputrow, "#dbgoff") ==0)
Fglobal_dumpwindowsmessages=false,fprocessed=true;
Request access to the Skype API
if (stricmp (Acinputrow, "#connect") ==0)
{
SendMessage (Hwnd_broadcast, Uiglobal_msgid_skypecontrolapidiscover, (WPARAM) hinit_mainwindowhandle, 0);
Fprocessed=true;
}
Stop accessing the Skype API. As can be seen by the code, this is a fake disconnect,
The WM_COPYDATA message sent by this window is no longer processed and printed because just setting hglobal_skypeapiwindowhandle is empty
if (stricmp (Acinputrow, "#disconnect") ==0)
{
Hglobal_skypeapiwindowhandle=null;
printf ("!!! disconnected/n ");
Fprocessed=true;
}
Other user input, it is sent directly to Skype for processing as a Skype API command
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 ");
}
}
}
}
}
PostMessage (Hinit_mainwindowhandle, wm_close, 0, 0);
SetEvent (hglobal_threadshutdownevent);
Fglobal_threadrunning=false;
}

void Main (void)
{
Gets the two types of message IDs that Skype registers to the system
Uiglobal_msgid_skypecontrolapiattach=registerwindowmessage ("Skypecontrolapiattach");
Uiglobal_msgid_skypecontrolapidiscover=registerwindowmessage ("Skypecontrolapidiscover");
if (uiglobal_msgid_skypecontrolapiattach!=0 && uiglobal_msgid_skypecontrolapidiscover!=0)
{
Registering a window class and creating a window
if (Initialize_createwindowclass ())
{
if (Initialize_createmainwindow ())
{
Hglobal_threadshutdownevent=createevent (null, TRUE, FALSE, NULL);
if (hglobal_threadshutdownevent!=null)
{
Start a process individually to receive user input instructions and process
if (_beginthread (&global_inputprocessingthread, 64*1024, NULL)! = (unsigned long)-1)
{
Main thread in this loop processes window messages
Global_messageloop ();
Garbage collection and cleanup work
Global_console_cancelreadrow ();
WaitForSingleObject (Hglobal_threadshutdownevent, INFINITE);
}
CloseHandle (hglobal_threadshutdownevent);
}
Deinitialize_destroymainwindow ();
}
Deinitialize_destroywindowclass ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.