Get Object's Persistent Reference ID Example (C++ COM)
This example shows how to get the selected object's persistent reference
ID. This example also shows how to use a modified SafeArray template class
to expect BYTE SafeArrays to parse the VARIANT returned by IModelDocExtension::GetPersistReference3.
StorePersistReferenceArray.cpp
'-----------------------------------------
'
' Preconditions: Model document is open and an entity
is selected.
'
' Postconditions: None
'
'------------------------------------------
// Defines the entry point for the console application.
#include "stdafx.h"
#include "StorePersistReferenceArray.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
#include "smartvars.h"
#import "sldworks.tlb" raw_interfaces_only
#import "swconst.tlb" no_namespace
CWinApp theApp;
using namespace std;
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))
{
// TODO: change error code to suit your
needs
_tprintf(_T("Fatal Error: MFC initialization
failed\n"));
nRetCode = 1;
}
else
{
::CoInitialize(NULL);
{
CComPtr<SldWorks::ISldWorks> swApp;
swApp.CoCreateInstance(L"SldWorks.Application",
NULL, CLSCTX_LOCAL_SERVER);
CComPtr<SldWorks::IModelDoc2> swDoc;
swApp->get_IActiveDoc2(&swDoc);
CComPtr<SldWorks::IModelDocExtension>
swDocExt;
swDoc->get_Extension(&swDocExt);
CComPtr<SldWorks::ISelectionMgr> swSelMgr;
swDoc->get_ISelectionManager(&swSelMgr);
CComPtr<IDispatch> disp;
swSelMgr->GetSelectedObject6(1,
-1, &disp);
_variant_t varPersist;
swDocExt->GetPersistReference3(disp,
&varPersist);
CString strPersist;
SafeBYTEArray byteArr(varPersist);
BYTE* bytes = new BYTE[byteArr.getSize()];
for(int i = 0; i < byteArr.getSize();
i++)
{
bytes[i] = byteArr[i];
}
//Delete the array
delete [] bytes;
}
::CoUninitialize();
}
return nRetCode;
}
Back to top
stdafx.cpp
// stdafx.cpp : source file that includes just the standard
includes
// StorePersistReferenceArray.pch will be the pre-compiled
header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in
STDAFX.H
// and not in this file
Back to top
StorePersistReference.h
#pragma once
#include "resource.h"
Back to top
stdafx.h
// stdafx.h : include file for standard system include
files,
// or project specific include files that are used frequently,
but
// are changed infrequently
//
#pragma once
// Modify the following defines if you have to target
a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding
values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows
XP or later.
#define WINVER 0x0501 // Change this to the appropriate
value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT // Allow use of features specific
to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate
value to target other versions of Windows.
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific
to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate
value to target Windows Me or later.
#endif
#ifndef _WIN32_IE // Allow use of features specific to
IE 6.0 or later.
#define _WIN32_IE 0x0600 // Change this to the appropriate
value to target other versions of IE.
#endif
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff
from Windows headers
#include <stdio.h>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString
constructors will be explicit
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from
Windows headers
#endif
#include <afx.h>
#include <afxwin.h> //
MFC core and standard components
#include <afxext.h> //
MFC extensions
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdtctl.h> // MFC support for Internet
Explorer 4 Common Controls
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common
Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <iostream>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString
constructors will be explicit
#include <atlbase.h>
#include <atlstr.h>
// TODO: reference additional headers your program requires
here
Back to top
Resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by StorePersistReferenceArray.rc
//
#define IDS_APP_TITLE 103
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
Back to top