Freigeben über


Introduction to the SMS 2003 Management Point (MP) API

The MP API was introduced in the second release of the SMS SDK (we are currently in the fourth release, version 3.1). This API is very powerful and can be used to submit discovery, inventory, status, and metering data from a SMS or non-SMS client. It can also be used to request information from an MP, like policies or the location of a distribution point based on an IP subnet or AD site. 

This example shows how to submit a location request to an MP via the MP API. This can be useful for a non-SMS client to be able to find out where to install an SMS package from. A common scenario would be a new install of the OS (maybe right after an image is applied). You could use the MP API to find out where to install the software needed on the image in order to get the machine up to a baseline. I created a win32 console application in Visual Studio and checked the box to add MFC support. For this application to work the smsmsgapi.dll must be registered on the machine (regsvr32 smsmsgapi.dll). Also, make sure smsmsgapi.h and smsmsgapi_i.c are in your project folder. All of these files are included in the SMS SDK.

 

 // LocationRequest.cpp : Defines the entry point for the console application.
// Example of how to submit a location request using the MP API

#include "stdafx.h"
#include "LocationRequest.h"
#include "SmsMsgAPI.h"
#include "SmsMsgAPI_i.c"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CWinApp theApp;

using namespace std;

//  Macros to perform error handling and make the code cleaner.
#define _BEGIN \
    HRESULT __hrRetVal = S_OK; \
    { \
 
#define _END \
    } \
    __End: \
 
#define _RETVAL __hrRetVal
 
#define _CHECKHR(expr) if(FAILED(expr)) { __hrRetVal=expr; goto __End; }

//Main
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        nRetCode = 1;
    }
    else
    {
        if(argc < 4)
        {
            printf("Usage:LocationRequest.exe <https://mpname> <CliSubnet> <CliIP> <PkgID> <ADSite>\n");
        }
        else
        {
            CString sMPIP, sClientSubnet, sClientIP, sPackageID, sADSite;
            ISmsMessaging               *pMessaging = NULL;
            ISmsMessage                 *pRequest = NULL;
            ISmsMessage                 *pReply = NULL;
            WCHAR                       *pszRequestMessage = NULL;
            WCHAR                       *pszReplyMessage = NULL;
            CString                        sNewLocationRequest;
            BSTR                        sLocationRequest = NULL;
            BSTR                        sMPIPAddress;

            //Get command line arguments
            sMPIP = argv[1];
            sClientSubnet = argv[2];
            sClientIP = argv[3];
            sPackageID = argv[4];
            sADSite = argv[5];

            //Format XML based Location Request
            sNewLocationRequest = "<ContentLocationRequest SchemaVersion=\"1.0\"> \n \
<Package ID=\"" + sPackageID + "\" Version=\"*\"/> \n \
<AssignedSite SiteCode=\"*\"/> \n \
<ClientLocationInfo> \n \
<ADSite Name=\"" + sADSite + "\"/> \n \
<IPAddresses> \n \
<IPAddress SubnetAddress=\"" + sClientSubnet + "\" Address=\"" + sClientIP + "\"/> \n \
</IPAddresses> \n \
</ClientLocationInfo> \n \
</ContentLocationRequest>";

            printf("\nLocation Request:\n%s",sNewLocationRequest);  //Print request to command line

            sLocationRequest = sNewLocationRequest.AllocSysString();
            sMPIPAddress = sMPIP.AllocSysString();
            CoInitialize(0);

        _BEGIN

            //Create root messaging object
            _CHECKHR( ::CoCreateInstance(
                        CLSID_SmsMessaging,
                        NULL,
                        CLSCTX_INPROC,
                        IID_ISmsMessaging,
                        (LPVOID*)&pMessaging) );

            //Create message object for request
            _CHECKHR( pMessaging->CreateMessage(&pRequest) );

            //Set the target of the message to the Location Manager endpoint
            _CHECKHR( pRequest->SetTargetEndpoint(L"MP_LocationManager") );

            //Set the message body with the request
            _CHECKHR( pRequest->SetBodyFromString(sLocationRequest) );

            // Invoke the targeted endpoint on the local machine with the request,
            // and retrieve the reply.
            _CHECKHR( pMessaging->Invoke(sMPIPAddress, pRequest, &pReply) );

            // Extract body from reply message.
            _CHECKHR( pReply->GetBodyToString(&pszReplyMessage) );

            wprintf(L"\n\nReply from MP:\n%s", (wchar_t *) pszReplyMessage);

        _END

            printf("\n\n%s%X", "Return Code=", __hrRetVal);

            if(pszReplyMessage)
            {
                ::CoTaskMemFree(pszReplyMessage);
            }
            if(pReply)
            {
                pReply->Release();
            }
            if(pszRequestMessage)
            {
                ::CoTaskMemFree(pszRequestMessage);
            }
            if(pRequest)
            {
                pRequest->Release();
            }
            if(pMessaging)
            {
                pMessaging->Release();
            }
        }
    }
    return nRetCode;
}

Comments

  • Anonymous
    May 17, 2006
    Where can I find smsmsgapi.dll? I installed the SMS2003, with SP1, but still can not find that dll available on the hard disk

  • Anonymous
    May 17, 2006
    It's in the SDK:
    http://www.microsoft.com/downloads/details.aspx?FamilyId=58833CD1-6DBB-45BB-BB77-163446068EF6&displaylang=en

    I'll be posting more examples on using the MP API in June.  I'm waiting on an update to some of the files in the SDK which are needed in order to use the MP API to submit data (although the Location Request works fine with the current SDK).

  • Anonymous
    May 17, 2006
    That is the SDK installed on my system, but I can not find the dll file in my SDK installation at all. Would you please tell me where should be this dll installed after SDK installation? Thanks.

  • Anonymous
    May 17, 2006
    Under the SDK directoryRedistributablesSMSMPAPI there is an mpapi.msi file.  Install this, then the dll should be located under the c:SMS_SDK directory.  This files to add to your project are under SamplesOtherSMSMPAPI.

    - Russ

  • Anonymous
    May 17, 2006
    Thank you. Got it!

  • Anonymous
    May 17, 2007
    I can't get this to compile under Visual Studio 2005. I get link errors. I created a new C++ project and checked MFC, left everything else defaults. Pasted in the code and had to change how the message string was concatenated together (used .Append versus +). Any help is greatly appreciated. ------ Build started: Project: LocationRequest, Configuration: Debug Win32 ------ Linking... smsmsgapi_i.obj : error LNK2005: _IID_ISmsMessaging already defined in LocationRequest.obj smsmsgapi_i.obj : error LNK2005: _IID_ISmsMessage already defined in LocationRequest.obj smsmsgapi_i.obj : error LNK2005: _IID_ISmsMessageAttachment already defined in LocationRequest.obj smsmsgapi_i.obj : error LNK2005: _LIBID_SmsMsgAPI already defined in LocationRequest.obj smsmsgapi_i.obj : error LNK2005: _CLSID_SmsMessaging already defined in LocationRequest.obj C:DataCodeProjectsLocationRequestDebugLocationRequest.exe : fatal error LNK1169: one or more multiply defined symbols found Build log was saved at "file://c:DataCodeProjectsLocationRequestLocationRequestDebugBuildLog.htm" LocationRequest - 6 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • Anonymous
    May 17, 2007
    I haven't compiled in this quite some time and I think the last time I did it I used Visual Studio 2003.  I'll try and do this in 2005 early next week and comment on my results...

  • Anonymous
    May 22, 2007
    I got it to compile in VS2005 by taking out the include for the SmsMsgAPI.c. Apparently since it was in my project and in the code as an include, it was including the same code twice. Also had to get rid of the use of "+" for appending. Pain. Where I am right now is that I send my soap request to the MP and I get a response back. The response however is just: <ContentLocationReply SchemaVersion="1.00"><Sites/></ContentLocationReply> Return Code=0 I'm not sure what I should be seeing, but I would have thought there would have been a distribution point path or server name or something in there. Still working away on this one but figured I'd give you an update. I am going back to the API's to see if they provide any additional guidance.

  • Anonymous
    May 22, 2007
    The comment has been removed

  • Anonymous
    May 22, 2007
    The comment has been removed

  • Anonymous
    May 22, 2007
    OK, I got it fixed, the problem was that the SMS Administrators had done some funky things with the boundaries and there was no distribution point for the workstation. Everything is working now. Thanks!

  • Anonymous
    February 25, 2008
    Anyone know how you can get this API working if you don't have domain authentication?  For example, if your logged in as local administrator?  Is there a way to pass authentication? When I run it in Windows as a domain user everything works fine, but when I run it as local administrator I get return code 80072F78.