SamLogic Social Media Components - Home
SLTwitCtrl ActiveX Control for Twitter - Example Code
Example 1 - Visual C++ - How to add the Twitter ActiveX control to a project

Before you can use the Twitter component you must add the component to your Visual C++ project. Detailed information about how to add the SLTwitCtrl ActiveX Control to your Visual C++ project is available in this blog post on our blog.
Example 2 - Visual C++ - Preparing the Twitter control for communication
Before you can send a tweet from the control to Twitter you must specify some information that Twitter requires for communication. You must specify an application name, a consumer key, a consumer secret, a username and a password. You can do this by setting corresponding properties in the ActiveX control.
You can do it via the Properties window in Microsoft Visual C++ (as shown in the picture to the right) or by specifying the information programmatically, as shown in the Visual C++ example below:
Properties window in Visual C++
// Specify application name
SLTwitCtrl1.SetAppName("My Application");
// Specify consumer key and consumer secret
// Specify username and password
A consumer key and consumer secret can be retrieved from Twitter's website for developers. On the same website you can register your application (which is necessary before you can send tweets to Twitter). Open this web page to read more.
Example 3 - Visual C++ - Send a message (a tweet) to Twitter

This example shows how to send a message (a tweet) to Twitter using Visual C++ code. It also shows how to handle errors, if something goes wrong.
// Post a tweet
BOOL bStatus = SLTwitCtrl1.TwitPostMessage("This is a tweet!");
// Check if an error occurred
if (bStatus == false) {
    // Store error message in this string variable
    char sErrMsg[1024];
    // Get error code
    long nErrorCode = SLTwitCtrl1.TwitGetLastError();
    // Create a string with the error code
    wsprintf(sErrMsg,"Error #%d occurred!",nErrorCode);
    // If error code is > 1000, Twitter may have a message for us
    if (nErrorCode > 1000) {
        // Read message from Twitter
        BSTR sReplyMsg;
        CString csReplyMsg = (CString)sReplyMsg;
        strcat(sErrMsg," Twitter returned: ");
    else {
        // Show message box with error message
        MessageBox(sErrMsg, "Error");
else {
    // Show information message box - the message want sent to Twitter
    MessageBox("The message was successfully sent to Twitter!", "Information");
Example 4 - Visual C++ - How to use Bitly to shorten a URL in a message

This example in Visual C++ shows how to shorten a URL in a message by using the TwitShortenURLInMessage method in the SLTwitCtrl ActiveX control. The Bitly service is used.
char sMessageOriginal[1024];
' Create a message that contains a URL
 "Read more about MultiMailer here:");
' Now shorten the URL in the message
CString csMessageShortenedURL = SLTwitter.TwitShortenURLInMessage("",1);
' Now display the updated message in a message box
MessageBox(csMessageShortenedURL.GetBuffer(1), "Message with shortened URL");
The message text "Read more about MultiMailer here:" will be updated to have the following contents: "You can read more about the Twitter component here:". The image below shows how the message box that is opened in the last line of the example code above look like:
The message box shows the updated message with a shortened URL

< Go back
Visual C++