If you are using the DataCollection API (either from native, by linking agains VSPerf.lib, or from managed, by importing Microsoft.VisualStudio.Profiler.dll), and wish to enable or disable collection at the global level, you must pass CurrentId for the elementId parameter:

  using Microsoft.VisualStudio.Profiler;

  [Not so interesting code here]  

  DataCollection.StartProfile(
      ProfileLevel.Global,
      DataCollection.CurrentId);

  [Interesting code here]

Similarly, for native code:

  #include "VSPerf.h"

  [Not so interesting code here]

  StartProfile(PROFILE_GLOBALLEVEL, PROFILE_CURRENTID);

  [Interesting code here]

Note that both the native and managed versions return zero on success, so it is possible to detect whether the call succeeded:

C# c++ Description
ProfileOperationResult.ErrorNotYetImplemented PROFILE_ERROR_NOT_YET_IMPLEMENTED API level, id combination not supported
ProfileOperationResult.ErrorModeNever PROFILE_ERROR_MODE_NEVER Global mode was "never" when called
ProfileOperationResult.ErrorLevelDoesNotExist PROFILE_ERROR_LEVEL_NOEXIST Profile level does not exist
ProfileOperationResult.ErrorIdDoesNotExist PROFILE_ERROR_ID_NOEXIST Element id doesn't exist