PC MAPI: How to Find a Currently Logged on User

Last reviewed: October 10, 1997
Article ID: Q136957
3.00 3.20 3.50 WINDOWS kbtool kbcode

The information in this article applies to:

  • Microsoft Mail for PC Networks, versions 3.0, 3.2, and 3.5

SUMMARY

By using a Simple Messaging Application Programming Interface (MAPI) application, you can find a user that is currently logged on to Microsoft Mail. The following code illustrates one way that this can be done using Microsoft Visual C++ version 1.5 or any other C compiler.

MORE INFORMATION

NOTE: The following code implements MAPI.DLL as a static library.

/* WHOAMI.C  */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <mapi.h>
#include <string.h>

int WhoAmI();

long err; LHANDLE lhSession;

MapiRecipDesc recip[1]; lpMapiMessage FAR *lppMessage; lpMapiMessage lpMessage;

char szSeedMessageID[512];
char szMessageID[512];
char szbuffer[80];

LPSTR lpszSeedMessageID=&szSeedMessageID[0]; LPSTR lpszMessageID=&szMessageID[0];

MapiMessage note;

int main()
{

 WhoAmI();

 return(0);
}

int WhoAmI()
{
  char szMsg1[80];
  char szMsg2[80];

  recip[0].ulReserved = 0;
  recip[0].ulRecipClass = MAPI_TO;
  recip[0].lpszName = "Anybody";
  recip[0].lpszAddress = NULL;
  recip[0].ulEIDSize = 0;
  recip[0].lpEntryID = NULL;

  note.ulReserved = 0;
  note.lpszSubject = "WhoAmI message";
  note.lpszNoteText = "Test";
  note.lpszMessageType = "IPC.WhoAmI.Note";
  note.lpszDateReceived = NULL;
  note.lpszConversationID = NULL;
  note.flFlags = 0;
  note.lpOriginator = 0;
  note.nFileCount = 0;
  note.lpFiles = NULL;
  note.nRecipCount = 1;
  note.lpRecips = recip;

  strcpy(szMsg1, "Logged in user name:  ");
  strcpy(szMsg2, "Logged in user address:  ");

  /************  Logon  **********************/
  err = MAPILogon(0L, "", "", MAPI_LOGON_UI, 0L,
  &lhSession);
  if(err != SUCCESS_SUCCESS)
  {
     MessageBox(0, "Error logging on", "Error", MB_OK);
     return(0);
  }

  /*********** Save Message ******************/
  err = MAPISaveMail(lhSession, 0L, &note, 0L, 0L, "");
  if(err != SUCCESS_SUCCESS)
 {
    MessageBox(0, "Error saving message", "Error",
    MB_OK);
    err = MAPILogoff(lhSession, 0L, 0L, 0L);
    return(0);
  }

  /********* Find Message ********************/
  *lpszSeedMessageID = '\0';

  // reset MAPIFindNext back to the top again
  err = MAPIFindNext(lhSession, 0L, "IPC.WhoAmI.Note",
  lpszSeedMessageID, 0L, 0L, lpszMessageID);

  do
  {
    err = MAPIFindNext(lhSession, 0L, "IPC.WhoAmI.Note",
    lpszSeedMessageID, 0L, 0L, lpszMessageID);
    if(err != SUCCESS_SUCCESS)
    {
      MessageBox(0, "Error finding message", "Error",
      MB_OK);
      err = MAPILogoff(lhSession, 0L, 0L, 0L);
      return(0);
    }
    lppMessage=(lpMapiMessage FAR *) &lpMessage;

    /******** Read Message *************/
    err = MAPIReadMail(lhSession, 0L, lpszMessageID,
    MAPI_PEEK, 0L, lppMessage);
    if(err != SUCCESS_SUCCESS)
    {
      MessageBox(0, "Error during message read", "Error",
      MB_OK);
      err = MAPILogoff(lhSession, 0L, 0L, 0L);
      return(0);
    }

    /* copy the user data into the buffers */
    _fstrcpy(szbuffer, lpMessage->lpszMessageType);

    /* Message Types compare               */
    if(strcmp(szbuffer,"IPC.WhoAmI.Note") == 0)
    {
      _fstrcpy(szbuffer, lpMessage->lpOriginator->
      lpszName);
      strcat(szMsg1, szbuffer);
      MessageBox(0, szMsg1, "Currently logged in user",
      MB_OK);
      _fstrcpy(szbuffer, lpMessage->lpOriginator->
      lpszAddress);
      strcat(szMsg2, szbuffer);
      MessageBox(0, szMsg2, "Currently logged in user
      address", MB_OK);

    /* Delete the message since we are done with it */
      err = MAPIDeleteMail(lhSession, 0L, lpszMessageID,
      0L, 0L);
      if(err != SUCCESS_SUCCESS)
      {
        MessageBox(0, "Error deleting message", "Error",
        MB_OK);
      }

      err = MAPIFreeBuffer(lpMessage);
      if(err != SUCCESS_SUCCESS)
      {
        MessageBox(0, "Error freeing memory", "Error",
        MB_OK);
      }
      err = MAPILogoff(lhSession, 0L, 0L, 0L);
      return(0);
    }
    //if necessary, get next message ID.
    lstrcpy(lpszSeedMessageID, lpszMessageID);


  }while(err == SUCCESS_SUCCESS);


 /************** Logoff  ***************/
  err = MAPILogoff(lhSession, 0L, 0L, 0L);

  return(0);
 //End whoami function
}


Additional reference words: 3.00 3.20 3.20a 3.50
KBCategory: kbtool kbcode
KBSubcategory: MailPCMAPI


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.