You can also switch to Skype-get started with shell program development based on Skype API

Source: Internet
Author: User

Skype is currently the most powerful IM + VoIP Software on the planet, and now it has changed the way of life for 0.28 billion people around the world. You deserve it! :)

Official site of Skype: http://skype.tom.com/

Skype Global Site: http://www.skype.com/

Skype is also the most open and innovative im tool in the world. It provides the Skype API, skype4com, several forms of development interfaces of skype4java provide Skype enthusiasts with Skype interaction programs or Skype plug-ins. You can use any language you are familiar with, such as C/C ++, VB, C #, Delphi, Java or even PHP, VBScript. With your professional knowledge, you can influence 0.28 billion of Skype users. You can also do it! :)

Skype global developer community: http://developer.skype.com/

The following is a simple C ++ code for accessing Skype APIs:

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
// Modification, are permitted provided that the following conditions
// Are met:
//
// * Redistributions of source code must retain the above Copyright
// Notice, this list of conditions and the following disclawing.
// * 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 be used to endorse or promote products derived
// From this software without specific prior written permission.
//
// This software is 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 special purpose are disclaimed. In no event shall
// Copyright owner or contributors be liable for any direct, indirect,
// Incidental, special, exemplary, or consequential damages (including,
// But not 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 way out of the use of this software, even if advised of
// 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_main1_whandle; // Hinstance hinit_processhandle; // <Process Handle of the program
Char acinit_windowclassname [128]; // <name of the window class created in this program
Handle hglobal_threadshutdownevent;
Bool volatile fglobal_threadrunning = true;

// The Message ID defined by Skype. Skype sends this type of message to a third-party program to notify the request connection result.
Uint uiglobal_msgid_skypecontrolapiattach;

// The Message ID defined by Skype. When a third-party program wants to obtain Skype interaction,
// The message must be sent through the broadcast (hwnd_broadcast). After receiving the message, the Skype prompts the user.
// When the user permits the interaction, the interaction is established.
Uint uiglobal_msgid_skypecontrolapidiscover;

// Skype window handle
Hwnd hglobal_skypeapi?whandle = NULL;

// Whether the 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 {
// The third-party program is successfully connected. You can obtain the Skype API window handle in wparam.
Skypecontrolapi_attach_success = 0,
// Skype has received the connection request and prompts the user to request a third-party program to access Skype
// The connection is not established at this time. You must wait until the skypecontrolapi_attach_success message is sent.
Skypecontrolapi_attach_pending_authorization = 1,
// The Skype user rejects the access request from a third-party program
Skypecontrolapi_attach_refused = 2,
// The API is currently unavailable. This situation sometimes occurs. For example, no user is logged on to Skype.
// A third-party program must wait until Skype broadcasts the skypecontrolapi_attach_api_available message and then try to connect.
Skypecontrolapi_attach_not_available = 3,
Skypecontrolapi_attach_api_available = 0x8001
};

// Obtain a row of input from the standard input window to pacpromptbuffer
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;
// Obtain the input buffer handle of 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 );
}

// Enter the recycle bin in the console.
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_main1_whandle = NULL;
Postquitmessage (0 );
Break;
Case wm_copydata:
// Use wm_copydata for message transmission between Skype and third-party programs
// When Skype sends messages to all connected third-party programs through wm_copydata, the Skype window handle will be placed in uiparam
If (hglobal_skypeapi?whandle = (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
If (uimessage = uiglobal_msgid_skypecontrolapiattach)
{
Switch (ulparam)
{
Case skypecontrolapi_attach_success:
Printf ("!!! Connected; To terminate issue # Disconnect/N ");
Hglobal_skypeapi?whandle = (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_main1_whandle, 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 (& 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 a window class name (including UUID)
Strcpy (acinit_windowclassname, "Skype-API-test -");
Strcat (acinit_windowclassname, (char *) paucuuidstring );

Owindowclass. Style = cs_hredraw | cs_vredraw | cs_dblclks;
// The message callback function of 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 the 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)
{
// Logout 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_main1_whandle
Hinit_main1_whandle = create1_wex (ws_ex_appwindow | ws_ex_1_wedge,
Acinit_windowclassname, "", ws_border | ws_sysmenu | ws_minimizebox,
Cw_usedefault, cw_usedefault, 128,128, null, 0, hinit_processhandle, 0 );
Return (hinit_main1_whandle! = NULL? True: false );
}

Void deinitialize_destroymainwindow (void)
{
// Destruction window
If (hinit_main1_whandle! = NULL)
Destroywindow (hinit_main1_whandle), hinit_main1_whandle = NULL;
}

Void global_messageloop (void)
{
// Message processing cycle
MSG omessage;
While (getmessage (& omessage, 0, 0, 0 )! = False)
{
Translatemessage (& omessage );
Dispatchmessage (& omessage );
}
}

// This is a thread function used to receive and execute user input commands.
Void _ cdecl global_inputprocessingthread (void *)
{
Static char acinputrow [1024];
Bool fprocessed;
 
// When the thread starts execution, it connects to Skype by default and sends a message requesting to access the Skype API. Since we didn't know the Skype API window handle value at the beginning
// Broadcast the message through hwnd_broadcast to the front-end windows in all systems. If the Skype window exists, you will naturally receive the message.
If (sendmessage (hwnd_broadcast, uiglobal_msgid_skypecontrolapidiscover, (wparam) hinit_main1_whandle, 0 )! = 0)
{
// Receive user input commands
While (global_console_readrow (acinputrow, sizeof (acinputrow)-1 ))
{
// Exit the program
If (stricmp (acinputrow, "# Quit") = 0 |
Stricmp (acinputrow, "# exit") = 0)
Break;
Fprocessed = false;
// Enable display of Message Details
If (stricmp (acinputrow, "# dbgon") = 0)
{
Printf ("skypecontrolapiattach = % u, skypecontrolapidiscover = % u, hinit_main1_whandle = 0x % 08lx/N ",
Uiglobal_msgid_skypecontrolapiattach, uiglobal_msgid_skypecontrolapidiscover, hinit_main1_whandle );
Fglobal_dumpwindowsmessages = true, fprocessed = true;
}
// Disable Message Details
If (stricmp (acinputrow, "# dbgoff") = 0)
Fglobal_dumpwindowsmessages = false, fprocessed = true;
// Request to access the Skype API
If (stricmp (acinputrow, "# connect") = 0)
{
Sendmessage (hwnd_broadcast, uiglobal_msgid_skypecontrolapidiscover, (wparam) hinit_main1_whandle, 0 );
Fprocessed = true;
}
// Stop accessing the Skype API. The Code shows that this is a false disconnection,
// Because the hglobal_skypeapi?whandle is set to null, The wm_copydata message sent in this window is no longer processed or printed.
If (stricmp (acinputrow, "# Disconnect") = 0)
{
Hglobal_skypeapi?whandle = NULL;
Printf ("!!! Disconnected/N ");
Fprocessed = true;
}
// The content entered by other users is directly sent to Skype as a Skype API command.
If (fprocessed = false & hglobal_skypeapi?whandle! = 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_skypeapi?whandle, wm_copydata, (wparam) hinit_main=whandle, (lparam) & ocopydata) = false)
{
Hglobal_skypeapi?whandle = NULL;
Printf ("!!! Disconnected/N ");
}
}
}
}
}
Postmessage (hinit_main1_whandle, wm_close, 0, 0 );
Setevent (hglobal_threadshutdownevent );
Fglobal_threadrunning = false;
}

Void main (void)
{
// Obtain the two types of message IDs registered to the system by Skype
Uiglobal_msgid_skypecontrolapiattach = registerwindowmessage ("skypecontrolapiattach ");
Uiglobal_msgid_skypecontrolapidiscover = registerwindowmessage ("skypecontrolapidiscover ");
If (uiglobal_msgid_skypecontrolapiattach! = 0 & uiglobal_msgid_skypecontrolapidiscover! = 0)
{
// Register the window class and create a window
If (initialize_createwindowclass ())
{
If (initialize_createmainwindow ())
{
Hglobal_threadshutdownevent = createevent (null, true, false, null );
If (hglobal_threadshutdownevent! = NULL)
{
// Start a separate process to receive and process user input commands
If (_ beginthread (& global_inputprocessingthread, 64*1024, null )! = (Unsigned long)-1)
{
// The main thread processes messages in this loop.
Global_messageloop ();
// Garbage collection and cleaning
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.