Sending mail in an unmanaged VC++ application

An oft posted question in NGs is how to send mail from an unmanaged VC++ application. If you are using VC++ 2003, then you can use the ATL7 mailing classes. I list below some bare-minimum steps to send a mail out, but you might want to look up further documentation on MSDN.

  • Include the required header file
    #include <atlsmtpconnection.h>
  • Call CoInitialize
    CoInitialize(0);
  • And write code similar to below snippet
    CMimeMessage msg;
    
    msg.SetSender(strSender);
    msg.AddRecipient(strRecepient);
    
    /*  Optional
    
    msg.SetSubject(strSubject);
    msg.AddText(strBody);
    msg.AttachFile(strAttachFile);
    
    */
    
    CSMTPConnection conn;
    conn.Connect(server);
    BOOL bSuccess = conn.SendMessage(msg);
    conn.Disconnect();
  • Call CoUninitialize
    CoUninitialize();

Blogger’s Block

For a 2 week gap, you might have noticed the lack of any new entries on my blog. No, I wasn’t imprisoned for using the cell phone while driving drunk nor was I ill in bed with flu; but I had Blogger’s Block! It’s the most frustrating state of mind that a blogger can get into, and I can tell you this, I was really annoyed with myself and also angry, but all my efforts to produce a blog entry failed. :grrr:

I don’t think I am the only blogger who gets Blogger’s Block, for my good friend and fellow-blogger Jambo Johnson (better known as Mister .NET) is a serious sufferer of Blogger’s Block. In fact I just noticed that DataGrid Girl has not blogged in a while either, which just goes on to prove that Blogger’s Block is not a gender specific problem. :hmmm:

Well, finally I did the only thing I could do to get out of the painful rut. I wrote another CP article (that C++/CLI Arrays article) and then blogged about it. That somehow cured me and well this is my third blog entry in three days. I feel good, I tell ya, really really good.

Oh, and I also wanted to show off my new blog smilies :nerd:

Acknowledgement

I’d like to thank my friend Colin Davies, for it was he who mailed me asking me why I wasn’t blogging at all this month, and when I told him I was suffering from acute Blogger’s Block, Colino suggested that I blog about Blogger’s Block.

BTW, if you want to use these smilies in your comments, just hover the mouse over the smilies to see the smiley-codes.🙂

Direct manipulation of CLI arrays using native pointers

Here’s some code that shows how you can directly manipulate the contents of an array using native pointers. The first sample is for a single dimensional array and the second is for a jagged array.

Natively accessing a single-dimensional array

void Test1()
{
    array<int>^ arr = gcnew array<int>(3);
    arr[0] = 100;
    arr[1] = 200;
    arr[2] = 300;

    Console::WriteLine(arr[0]);
    Console::WriteLine(arr[1]);
    Console::WriteLine(arr[2]);

    /*
    Output :-
      100
      200
      300
    */

    // Modifying the array using a native int*
    // that points to a pinned pointer in GC'd heap
    pin_ptr<int> p1 = &arr[0];
    int* p2 = p1;
    while(*p2)
    {
        (*p2)++;
        p2++;
    }

    Console::WriteLine(arr[0]);
    Console::WriteLine(arr[1]);
    Console::WriteLine(arr[2]);

    /*
    Output :-
      101
      201
      301
    */
} 

Natively accessing a jagged array

void Test2()
{
    array<array<int>^>^ arr = gcnew array<array<int>^>(2);
    arr[0] = gcnew array<int>(2);
    arr[1] = gcnew array<int>(2);
    arr[0][0] = 10;
    arr[0][1] = 100;
    arr[1][0] = 20;
    arr[1][1] = 200;

    Console::WriteLine(arr[0][0]);
    Console::WriteLine(arr[0][1]);
    Console::WriteLine(arr[1][0]);
    Console::WriteLine(arr[1][1]);

    /*
    Output:
      10
      100
      20
      200
    */


    // Copying the managed jagged array to
    // a native array of pointers and accessing
    // the members using the native array
    pin_ptr<int> p1 = &arr[0][0];
    pin_ptr<int> p2 = &arr[1][0];
    int* p3[2];
    p3[0] = p1;
    p3[1] = p2;

    Console::WriteLine(p3[0][0]);
    Console::WriteLine(p3[0][1]);
    Console::WriteLine(p3[1][0]);
    Console::WriteLine(p3[1][1]);

    /*
    Output:
      10
      100
      20
      200
    */

    // Assigning the native array of pointers
    // to a native int** and modifying the array
    int** p4 = p3;
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            p4[i][j] += 3;

    Console::WriteLine(arr[0][0]);
    Console::WriteLine(arr[0][1]);
    Console::WriteLine(arr[1][0]);
    Console::WriteLine(arr[1][1]);

    /*
    Output:
      13
      103
      23
      203
    */
}

Essentially we use a pinning pointer to the GC’d heap and then treat the casted native pointer as if it were pointing to a native array. Gives us a really fast method to manipulate array content!

Another CP article – Arrays in C++/CLI

I just finished my latest C++/CLI article for CodeProject :-

The article exposes the new array syntax available in C++/CLI for the declaration and use of CLI arrays, and covers single-dim arrays, multi-dim arrays, jagged arrays, direct initialization, arrays as function arguments, arrays as return types, array covariance, parameter arrays and the System::Array connection. Please give me a shout if you have any feedback for me🙂