Initializes the COM library on the current thread and identifies the concurrency model as single-thread apartment (STA). Applications must initialize the COM library before they can call COM library functions other than CoGetMalloc and memory allocation functions.
New applications should call CoInitializeEx instead of CoInitialize.
HRESULT CoInitialize(
LPVOID pvReserved //Reserved; must be NULL
);
This function supports the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED, as well as the following:
CoInitializeEx provides the same functionality as CoInitialize and also provides a parameter to explicitly specify the thread's concurrency model. CoInitialize calls CoInitializeEx and specifies the concurrency model as single-thread apartment. Applications developed today should call CoInitializeEx rather than CoInitialize.
You need to initialize the COM library on a thread before you call any of the library functions except CoGetMalloc, to get a pointer to the standard allocator, and the memory allocation functions.
Once the concurrency model for a thread is set, it cannot be changed. A call to CoInitialize on an apartment that was previously initialized as multithreaded will fail and return RPC_E_CHANGED_MODE.
Typically, the COM library is initialized on a thread only once. Subsequent calls to CoInitialize or CoInitializeEx on the same thread will succeed, as long as they do not attempt to change the concurrency model, but will return S_FALSE. To close the COM library gracefully, each successful call to CoInitialize or CoInitializeEx, including those that return S_FALSE, must be balanced by a corresponding call to CoUninitialize. However, the first thread in the application that calls CoInitialize(0) or CoInitializeEx(COINIT_APARTMENTTHREADED) must be the last thread to call CoUninitialize(). If the call sequence is not in this order, then subsequent calls to CoInitialize on the STA will fail and the application will not work.
Because there is no way to control the order in which in-process servers are loaded or unloaded, it is not safe to call CoInitialize, CoInitializeEx, or CoUninitialize from the DllMain function.
Closes the COM library on the current thread, unloads all DLLs loaded by the thread, frees any other resources that the thread maintains, and forces all RPC connections on the thread to close.
void CoUninitialize( );
A thread must call CoUninitialize once for each successful call it has made to CoInitialize or CoInitializeEx. Only the CoUninitialize call corresponding to the CoInitialize or CoInitializeEx call that initialized the library can close it.
Calls to OleInitialize must be balanced by calls to OleUninitialize. The OleUninitialize function calls CoUninitialize internally, so applications that call OleUninitialize do not also need to call CoUninitialize.
CoUninitialize should be called on application shutdown, as the last call made to the COM library after the application hides its main windows and falls through its main message loop. If there are open conversations remaining, CoUninitialize starts a modal message loop and dispatches any pending messages from the containers or server for this COM application. By dispatching the messages, CoUninitialize ensures that the application does not quit before receiving all of its pending messages. Non-COM messages are discarded.
Because there is no way to control the order in which in-process servers are loaded or unloaded, it is not safe to call CoInitialize, CoInitializeEx, or CoUninitialize from the DllMain function.
Creates a single uninitialized object of the class associated with a specified CLSID. Call CoCreateInstance when you want to create only one object on the local system. To create a single object on a remote system, call CoCreateInstanceEx. To create multiple objects based on a single CLSID, refer to the CoGetClassObject function.
STDAPI CoCreateInstance(
REFCLSID rclsid, //Class identifier (CLSID) of the object
LPUNKNOWN pUnkOuter, //Pointer to controlling IUnknown
DWORD dwClsContext, //Context for running executable code
REFIID riid, //Reference to the identifier of the interface
LPVOID * ppv //Address of output variable that receives
// the interface pointer requested in riid
);
The CoCreateInstance helper function provides a convenient shortcut by connecting to the class object associated with the specified CLSID, creating an uninitialized instance, and releasing the class object. As such, it encapsulates the following functionality:
CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF);
hresult = pCF->CreateInstance(pUnkOuter, riid, ppvObj)
pCF->Release();
It is convenient to use CoCreateInstance when you need to create only a single instance of an object on the local machine. If you are creating an instance on remote machine, call CoCreateInstanceEx. When you are creating multiple instances, it is more efficient to obtain a pointer to the class object's IClassFactory interface and use its methods as needed. In the latter case, you should use the CoGetClassObject function.
In the CLSCTX enumeration, you can specify the type of server used to manage the object. The constants can be CLSCTX_INPROC_SERVER, CLSCTX_INPROC_HANDLER, CLSCTX_LOCAL_SERVER, or any combination of these values. The constant CLSCTX_ALL is defined as the combination of all three. For more information about the use of one or a combination of these constants, refer to CLSCTX.
The InitAudio method initializes the performance and optionally sets up a default audiopath. This method must be called before the performance can play using audiopaths.
This method should be used in most cases instead of IDirectMusicPerformance8::Init.
Syntax
HRESULT InitAudio(
IDirectMusic** ppDirectMusic,
IDirectSound** ppDirectSound,
HWND hWnd,
DWORD dwDefaultPathType,
DWORD dwPChannelCount,
DWORD dwFlags,
DMUS_AUDIOPARAMS *pParams
);
Parameters
ppDirectMusic
Address of a variable that specifies or receives an interface pointer to a DirectMusic object.
If the variable pointed to by ppDirectMusic contains a valid IDirectMusic or IDirectMusic8 interface pointer, the existing object is assigned to the performance. The reference count of the interface is incremented.
If the variable pointed to by ppDirectMusic contains NULL, a DirectMusic object is created and the IDirectMusic interface pointer is returned. Use QueryInterface to obtain IDirectMusic8.
If ppDirectMusic is NULL, a DirectMusic object is created and used internally by the performance.
See Remarks.
ppDirectSound
Address of a variable that specifies or receives an IDirectSound interface pointer for a DirectSound device object to use by default for waveform output. If this parameter contains a NULL pointer, DirectMusic creates a private DirectSound device object. If the variable pointed to contains NULL, DirectMusic creates a DirectSound device object and returns the interface pointer. See Remarks.
hWnd
Window handle to use for the creation of DirectSound. This parameter can be NULL, in which case the foreground window is used. See Remarks.
This parameter is ignored if an IDirectSound interface pointer is passed to the method in ppDirectSound. In that case the application is responsible for setting the window handle by using IDirectSound8::SetCooperativeLevel.
dwDefaultPathType
Value that specifies the default audiopath type. Can be zero if no default path type is wanted. For a list of defined values, see IDirectMusicPerformance8::CreateStandardAudioPath.
dwPChannelCount
Value that specifies the number of
dwFlags
Flags that specify requested features. If pParams is not NULL, this value is ignored and the requested features are specified in the dwFeatures member of the DMUS_AUDIOPARAMS structure. The values listed in the following table are defined for use in this parameter.
Value | Description |
DMUS_AUDIOF_3D | 3-D buffers. This flag is not implemented. Buffers in 3-D audiopaths always have 3-D capabilities. |
DMUS_AUDIOF_ALL | All features. |
DMUS_AUDIOF_BUFFERS | Multiple buffers. |
DMUS_AUDIOF_DMOS | Additional DMOs. This flag is not implemented. |
DMUS_AUDIOF_ENVIRON | Environmental modeling. This flag is not implemented. |
DMUS_AUDIOF_EAX | Support for Environmental Audio Extensions (EAX). This flag is not implemented. |
DMUS_AUDIOF_STREAMING | Support for streaming waveforms. |
pParams
Address of a DMUS_AUDIOPARAMS structure that specifies parameters for the synthesizer and receives information about what parameters were set. Can be NULL if the default parameters are wanted.
Return Values
If the method succeeds, the return value is S_OK.
If it fails, the method can return one of the error values shown in the following table.
Return code |
DMUS_E_ALREADY_INITED |
DSERR_BUFFERLOST |
DSERR_PRIOLEVELNEEDED |
DSERR_UNINITIALIZED |
E_NOINTERFACE |
E_OUTOFMEMORY |
E_POINTER |
Remarks
This method can be called only once. It cannot be used to retrieve an existing IDirectMusic8 interface.
A DirectMusic object can be associated with the performance in the following ways.
If you specify an interface pointer in ppDirectSound, it must be an interface to an object of class CLSID_DirectSound8. Objects of this class support both IDirectSound and IDirectSound8, but the IDirectSound interface must be passed. The DirectSound device object must be fully initialized before being passed to InitAudio. If the object was created by using CoCreateInstance, call IDirectSound8::Initialize. Set the cooperative level to DSSCL_PRIORITY by using IDirectSound8::SetCooperativeLevel.
You can pass NULL in the hWnd parameter to pass the current foreground window handle to DirectSound. However, do not assume that the application window will be in the foreground during initialization. It is best to pass the top-level application window handle.
The parameters set in dwFlags and pParams apply to the default audiopath and any audiopaths created subsequently.
The method fails with DSERR_BUFFERLOST if a value other than zero is passed in dwDefaultPathType and any application has initialized DirectSound with the write-primary cooperative level.
The performance must be terminated by using the IDirectMusicPerformance8::CloseDown method before being released.
The SetSearchDirectory method sets a search path for finding object files. The search path can be set for one object file type or for all files.
Syntax
HRESULT SetSearchDirectory(
REFGUID rguidClass,
WCHAR* pwszPath,
BOOL fClear
);
Parameters
rguidClass
Reference to (C++) or address of (C) the identifier of the class of objects that the call pertains to. GUID_DirectMusicAllTypes specifies all objects. For a list of standard loadable classes, see IDirectMusicLoader8.
pwszPath
File path for directory. Must be a valid directory and must be less than MAX_PATH in length. The path, if not fully qualified, is relative to the current directory when IDirectMusicLoader8::ScanDirectory is called.
fClear
If TRUE, clears all information about objects before setting the directory. This prevents the loader from accessing objects in a previous directory when those objects have the same name. However, objects are not removed from the cache.
Return Values
If the method succeeds, the return value is S_OK, or S_FALSE if the search directory is already set to pwszPath.
If it fails, the method can return one of the error values shown in the following table.
Return code |
DMUS_E_LOADER_BADPATH |
E_OUTOFMEMORY |
E_POINTER |
Remarks
After a search path is set, the loader does not need a full path every time it is given an object to load by file name. This enables objects that refer to other objects to find them by file name without knowing the full path.
When this method has been called, the loader expects the wszFileName member of the DMUS_OBJECTDESC structure to contain only a file name or a path relative to the search directory, unless the DMUS_OBJ_FULLPATH flag is set in the dwValidData member.
Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
size_t mbstowcs( |
The address of a sequence of wide characters.
The address of a sequence of null terminated multibyte characters.
The maximum number of multibyte characters to convert.
If mbstowcs successfully converts the source string, it returns the number of converted multibyte characters. If the wcstr argument is NULL, the function returns the required size (in wide characters) of the destination string. If mbstowcs encounters an invalid multibyte character, it returns –1. If the return value is count, the wide-character string is not null-terminated.
Security Note |
---|
Ensure that wcstr and mbstr do not overlap, and that count correctly reflects the number of multibyte characters to convert. |
The DMUS_OBJECTDESC structure is used to describe a loadable object. This structure is passed to the IDirectMusicLoader8::GetObject method to identify the object that the loader should retrieve from storage. Information about an object is retrieved in this structure by the IDirectMusicLoader8::EnumObject and IDirectMusicObject8::GetDescriptor methods.
Syntax
typedef struct _DMUS_OBJECTDESC {
DWORD dwSize;
DWORD dwValidData;
GUID guidObject;
GUID guidClass;
FILETIME ftDate;
DMUS_VERSION vVersion;
WCHAR wszName[DMUS_MAX_NAME];
WCHAR wszCategory[DMUS_MAX_CATEGORY];
WCHAR wszFileName[DMUS_MAX_FILENAME];
LONGLONG llMemLength;
LPBYTE pbMemData;
IStream* pStream
} DMUS_OBJECTDESC, *LPDMUS_OBJECTDESC;
Members
dwSize
Size of the structure, in bytes. This member must be initialized to sizeof(DMUS_OBJECTDESC) before the structure is passed to any method.
dwValidData
Flags describing which members are valid and giving further information about some members. Thefollowing values are defined:
Flag | Description |
DMUS_OBJ_CATEGORY | The wszCategory member is valid. |
DMUS_OBJ_CLASS | The guidClass member is valid. |
DMUS_OBJ_DATE | The ftDate member is valid. |
DMUS_OBJ_FILENAME | The wszFileName member is valid. The presence of this flag is assumed if DMUS_OBJ_FULLPATH is set. |
DMUS_OBJ_FULLPATH | The wszFileName member contains either the full path of a file or a path relative to the application directory. The directory set by IDirectMusicLoader8::SetSearchDirectory is not searched. If this flag is not set, wszFilename is always assumed to be relative to the application directory, or to the search directory if SetSearchDirectory has been called for this object type. |
DMUS_OBJ_LOADED | The object is currently loaded in memory. |
DMUS_OBJ_MEMORY | The object is in memory, and llMemLength and pbMemData are valid. |
DMUS_OBJ_NAME | The wszName member is valid. |
DMUS_OBJ_OBJECT | The guidObject member is valid. |
DMUS_OBJ_STREAM | The pStream member contains a pointer to the data stream. |
DMUS_OBJ_URL | The wszFileName member contains a URL. URLs are not currently supported by the DirectMusic loader. |
DMUS_OBJ_VERSION | The vVersion member is valid. |
guidObject
Unique identifier for this object.
guidClass
Unique identifier for the class of object. See DirectMusic Component GUIDs.
ftDate
Date that the object was last edited.
vVersion
DMUS_VERSION structure containing version information.
wszName
Name of the object.
wszCategory
Category for the object.
wszFileName
File path. If DMUS_OBJ_FULLPATH is set, this is the full path; otherwise, it is the file name. If the IDirectMusicLoader8::SetSearchDirectory method has been called, this member must contain only a file name.
llMemLength
Size of data in memory.
pbMemData
Pointer to data in memory. Do not use this value except when loading from a resource contained in the executable file.
pStream
Address of the IStream interface of a custom stream that can be used to load the object into memory. In most cases this value should be NULL.See Remarks.
Remarks
At least one of wszName, guidObject, and wszFileName must contain valid data to retrieve the object by using the IDirectMusicLoader8::GetObject method.
The name and category strings use 16-bit characters in the WCHAR format, not 8-bit ANSI characters. Be sure to convert as appropriate. You can use the C library mbstowcs function to convert from multibyte to Unicode and the wcstombs function to convert from Unicode back to multibyte.
Instead of passing on object descriptor to IDirectMusicLoader8::GetObject or IDirectMusicLoader8::SetObject with a filename or memory pointer, an application can pass a stream. This is done by setting the DMUS_OBJ_STREAM flag in dwValidData and a pointer to the stream in pStream. When the application calls GetObject, the loader saves the stream's current location, reads the object from the stream, and then restores the saved location. The application can continue reading from the stream without being affected by the call to GetObject.
When SetObject is called with a stream, the loader makes a clone of the stream object, and this clone is used if the object is later loaded. Thus an application can release a stream or continue to read from it after passing it to the loader by using SetObject. The actual data of the stream is not copied, so the application should not change or delete the data.
The GetObject method retrieves an object from a file or resource and returns the specified interface.
Syntax
HRESULT GetObject(
LPDMUS_OBJECTDESC pDesc,
REFIID riid,
LPVOID FAR * ppv
);
Parameters
pDesc
Address of a DMUS_OBJECTDESC structure describing the object.
riid
Unique identifier of the interface. See DirectMusic Interface GUIDs.
ppv
Address of a variable that receives a pointer to the desired interface of the object.
Return Values
If the method succeeds, the return value is S_OK or DMUS_S_PARTIALLOAD.
DMUS_S_PARTIALLOAD is returned if any referenced object cannot be found, such as a
If it fails, the method can return one of the error values shown in the following table.
Return code |
DMUS_E_LOADER_FAILEDCREATE |
DMUS_E_LOADER_FAILEDOPEN |
DMUS_E_LOADER_FORMATNOTSUPPORTED |
DMUS_E_LOADER_NOCLASSID |
E_FAIL |
E_INVALIDARG |
E_OUTOFMEMORY |
E_POINTER |
REGDB_E_CLASSNOTREG |
Remarks
For file objects, it is simpler to use the IDirectMusicLoader8::LoadObjectFromFile method.
DirectMusic does not support loading from URLs. If the dwValidData member of the DMUS_OBJECTDESC structure contains DMUS_OBJ_URL, the method returns DMUS_E_LOADER_FORMATNOTSUPPORTED.
The method does not require that all valid members of the DMUS_OBJECTDESC structure match before retrieving an object. The dwValidData flags are evaluated in the following order:
In other words, the highest priority goes to a unique GUID, followed by a stream pointer, followed by a resource, followed by the full file path name, followed by an internal name plus category, followed by an internal name, followed by a local file name.
Do not load data from untrusted sources. Loading DirectMusic data files causes objects to be constructed, with the possibility that excessive demand on resources will lead to degradation of performance or system failure.