Hello all,
I have enjoyed using Sandboxie, and wished I had found it sooner. My usage is as an RDP replacement for running browser sessions. The US$50 it costs is simply amazing when you compare to the US$35k Microsoft wanted and US$20k Graphon GoGlobal costs to do much of our used functionality.

Anyway, here is source for a C# wrapper of the SBIE API, free to use, distribute, and modify as you see fit. I would prefer to know if you improve upon it (not hard to do), because I'll be posting this same code on CodeProject.com to sing the praises of Sandboxie soon.

ddwyer wrote:Hello all,I have enjoyed using Sandboxie, and wished I had found it sooner. My usage is as an RDP replacement for running browser sessions. The US$50 it costs is simply amazing when you compare to the US$35k Microsoft wanted and US$20k Graphon GoGlobal costs to do much of our used functionality.

Anyway, here is source for a C# wrapper of the SBIE API, free to use, distribute, and modify as you see fit. I would prefer to know if you improve upon it (not hard to do), because I'll be posting this same code on CodeProject.com to sing the praises of Sandboxie soon.

Hello Mr. ddwyer, much appreciate the code. A nice little starter pack for anyone (such as my-self) wishing to try their luck and Dev an app or two for use with Sandboxie.

Also, building on what you said "prefer to know if you improve upon it"; not sure of the chance of me improving on it as yet, but would definitely be interested in seeing any improvements or changes which you might be coming up with too. Thanks once again.

Hi ddwyer, many thanks for your hard work! Unfortunately, the updated version on CodeProject was deleted on 1 Nov 2010. But not to worry, I've used your example and written a wrapper that loads Sandboxie dynamically (no more hard coded DllImport calls!). Hope this helps, and thanks to Sandboxie for a really useful tool!

/// <summary> /// Enumerate Sandbox Names /// </summary> /// <param name="index">Specifies which sandbox to return. Initialize to -1. Sandboxes are enumerated in the order they appear in Sandboxie.ini.</param> /// <param name="box_name">Receives the sandbox name</param> /// <returns>Returns the next value to use for the index parameter. Returns -1 when there is nothing left to enumerate.</returns> private delegate Int32 SbieApi_EnumBoxes(Int32 index, byte[] box_name);

/// <summary> /// Query Sandbox Paths by Sandbox Name /// </summary> /// <param name="box_name">Specifies the name of the sandbox for which to return path information.</param> /// <param name="file_path">Receives the path to the root directory of the sandbox, as set by the FileRootPath setting. The buffer receives at most the number of bytes specified by the file_path_len parameter. Pass NULL to ignore this parameter.</param> /// <param name="key_path">Receives the path to the root key of the sandbox registry, as set by the KeyRootPath setting. The buffer receives at most the number of bytes specified by the key_path_len parameter. Pass NULL to ignore this parameter.</param> /// <param name="ipc_path">Receives the path to the root object directory of the sandbox, as set by the IpcRootPath setting. The buffer receives at most the number of bytes specified by the ipc_path_len parameter. Pass NULL to ignore this parameter.</param> /// <param name="file_path_len">Specifies the length in bytes of the file_path buffer. On return, receives the length in bytes needed to receive a complete buffer.</param> /// <param name="key_path_len">Specifies the length in bytes of the key_path buffer. On return, receives the length in bytes needed to receive a complete buffer.</param> /// <param name="ipc_path_len">Specifies the length in bytes of the ipc_path buffer. On return, receives the length in bytes needed to receive a complete buffer.</param> /// <returns>Returns zero on success, a non-zero value on error.</returns> private delegate Int32 SbieApi_QueryBoxPath(byte[] box_name, byte[] file_path, byte[] key_path, byte[] ipc_path, ref ulong file_path_len, ref ulong key_path_len, ref ulong ipc_path_len);

/// <summary> /// Query Sandbox Paths by Process ID /// </summary> /// <param name="process_id">Specifies the ID of the sandboxed process to query.</param> /// <param name="file_path">Receives the path to the root directory of the sandbox, as set by the FileRootPath setting. The buffer receives at most the number of bytes specified by the file_path_len parameter. Pass NULL to ignore this parameter.</param> /// <param name="key_path">Receives the path to the root key of the sandbox registry, as set by the KeyRootPath setting. The buffer receives at most the number of bytes specified by the key_path_len parameter. Pass NULL to ignore this parameter.</param> /// <param name="ipc_path">Receives the path to the root object directory of the sandbox, as set by the IpcRootPath setting. The buffer receives at most the number of bytes specified by the ipc_path_len parameter. Pass NULL to ignore this parameter.</param> /// <param name="file_path_len">Specifies the length in bytes of the file_path buffer. On return, receives the length in bytes needed to receive a complete buffer.</param> /// <param name="key_path_len">Specifies the length in bytes of the key_path buffer. On return, receives the length in bytes needed to receive a complete buffer.</param> /// <param name="ipc_path_len">Specifies the length in bytes of the ipc_path buffer. On return, receives the length in bytes needed to receive a complete buffer.</param> /// <returns>Returns zero on success, a non-zero value on error.</returns> private delegate Int32 SbieApi_QueryProcessPath(uint process_id, byte[] file_path, byte[] key_path, byte[] ipc_path, ref ulong file_path_len, ref ulong key_path_len, ref ulong ipc_path_len);

/// <summary> /// Enumerate Running Processes /// </summary> /// <param name="box_name">Specifies the name of the sandbox in which processes will be enumerated.</param> /// <param name="all_sessions">Specifies TRUE to enumerate processes in all logon sessions or only in a particular logon session</param> /// <param name="which_session">Specifies the logon session number in which processes will be enumerated. Ignored if all_sessions if TRUE. Pass the value -1 to specify the current logon session.</param> /// <param name="boxed_pids">Receives the process ID (PID) numbers. The first ULONG receives the number of processes enumerated. The second ULONG receives the first PID, the third ULONG receives the second PID, and so on.</param> /// <returns>Returns zero on success, a non-zero value on error.</returns> private delegate Int32 SbieApi_EnumProcessEx(byte[] box_name, bool all_sessions, Int32 which_session, [MarshalAs(UnmanagedType.LPArray)] UInt32[] boxed_pids);

/// <summary> /// Query Process Information /// </summary> /// <param name="process_id">Specifies the ID of the sandboxed process to query.</param> /// <param name="box_name">Receives the name of the sandbox in which the process is running. Pass NULL to ignore this parameter.</param> /// <param name="image_name">Receives the process name. Pass NULL to ignore this parameter.</param> /// <param name="sid_string">Receives the SID string for the process. Pass NULL to ignore this parameter.</param> /// <param name="session_id">Receives the logon session number in which the process is running. Pass NULL to ignore this parameter.</param> /// <returns>Returns zero on success, a non-zero value on error.</returns> private delegate Int32 SbieApi_QueryProcess(uint process_id, byte[] box_name, byte[] image_name, byte[] sid_string, UIntPtr session_id);

/// <summary> /// Terminate a Single Sandboxed Process /// </summary> /// <param name="process_id">Specifies the process ID for the sandboxed process that should be terminated.</param> /// <returns>Returns TRUE on success, FALSE on failure. The target process is terminated by the Sandboxie service (SbieSvc) with exit code 1 through a call to the Windows API TerminateProcess (ProcessId, 1).</returns> private delegate bool SbieDll_KillOne(uint process_id);

/// <summary> /// Terminate All Sandboxed Processes /// </summary> /// <param name="session_id">Specifies the logon session number in which sandboxed programs should be terminated. Specify -1 to indicate the current logon session.</param> /// <param name="box_name">Specifies the sandbox name in which sandboxed programs should be terminated.</param> /// <returns>Returns TRUE on success, FALSE on failure. The target processes are terminated in the fashion described above; see SbieDll_KillOne.</returns> private delegate bool SbieDll_KillAll(Int32 session_id, byte[] box_name);

/// <summary> /// Query Configuration from Sandboxie.ini /// </summary> /// <param name="section_name">Specifies the section name that contains the setting to query.</param> /// <param name="setting_name">Specifies the setting name to query.</param> /// <param name="setting_index">Specifies the zero-based index number for a setting that may appear multiple times. The index number can be logically OR'ed with these special values: 0x40000000 - do not scan the [GlobalSettings] section if the specified setting name does appear in the specified section. 0x20000000 - do not expand any variables in the result.</param> /// <param name="value">Receives the value of the specified setting.</param> /// <param name="value_len">Specifies the maximum length in bytes of the buffer pointed to by the value parameter.</param> /// <returns>Returns zero on success. Returns 0xC000008B if the setting was not found. Any other return value indicates some other error.</returns> private delegate Int32 SbieApi_QueryConf(byte[] section_name, byte[] setting_name, UInt32 setting_index, byte[] value, UInt32 value_len);

/// <summary> /// Enumerate Running Processes /// </summary> /// <param name="box_name">Specifies the name of the sandbox in which processes will be enumerated.</param> /// <param name="all_sessions">Specifies TRUE to enumerate processes in all logon sessions or only in a particular logon session</param> /// <param name="which_session">Specifies the logon session number in which processes will be enumerated. Ignored if all_sessions if TRUE. Pass the value -1 to specify the current logon session.</param> /// <returns>An array of process ID's for the supplied <paramref name="box_name"/></returns> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="box_name"/> is null or empty</exception> public UInt32[] EnumProcess(string box_name, bool all_sessions = true, Int32 which_session = -1) { if (string.IsNullOrWhiteSpace(box_name)) throw new ArgumentNullException("box_name");

/// <summary> /// Terminate a Single Sandboxed Process /// </summary> /// <remarks> /// When run against version 3.58, fails with "SBIE2203 Failed to communicate with Sandboxie Service: connect C0000041" /// See http://www.sandboxie.com/index.php?SBIE2203 /// </remarks> /// <param name="process_id">Specifies the process ID for the sandboxed process that should be terminated.</param> /// <returns>Returns TRUE on success, FALSE on failure. The target process is terminated by the Sandboxie service (SbieSvc) with exit code 1 through a call to the Windows API TerminateProcess (<paramref name="process_id"/>, 1).</returns> public bool KillOne(uint process_id) { SbieDll_KillOne extMethod = (SbieDll_KillOne)this.GetExternalMethodDelegate<SbieDll_KillOne>(); return extMethod(process_id); }

/// <summary> /// Terminate All Sandboxed Processes /// </summary> /// <param name="box_name">Specifies the sandbox name in which sandboxed programs should be terminated.</param> /// <returns>Returns TRUE on success, FALSE on failure. The target processes are terminated in the fashion described above; see KillOne.</returns> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="box_name"/> is null or empty</exception> public bool KillAll(string box_name) { return this.KillAll(-1, box_name); }

/// <summary> /// Terminate All Sandboxed Processes /// </summary> /// <param name="session_id">Specifies the logon session number in which sandboxed programs should be terminated. Specify -1 to indicate the current logon session.</param> /// <param name="box_name">Specifies the sandbox name in which sandboxed programs should be terminated.</param> /// <returns>Returns TRUE on success, FALSE on failure. The target processes are terminated in the fashion described above; see KillOne.</returns> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="box_name"/> is null or empty</exception> public bool KillAll(Int32 session_id, string box_name) { if (string.IsNullOrWhiteSpace(box_name)) throw new ArgumentNullException("box_name");

/// <summary> /// Query Configuration from Sandboxie.ini /// </summary> /// <param name="section_name">Specifies the section name that contains the setting to query.</param> /// <param name="setting_name">Specifies the setting name to query.</param> /// <param name="setting_index">Specifies the zero-based index number for a setting that may appear multiple times. The index number can be logically OR'ed with these special values: 0x40000000 - do not scan the [GlobalSettings] section if the specified setting name does appear in the specified section. 0x20000000 - do not expand any variables in the result.</param> /// <returns>Returns zero on success. Returns 0xC000008B if the setting was not found. Any other return value indicates some other error.</returns> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="section_name"/> is null or empty</exception> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="setting_name"/> is null or empty</exception> /// <exception cref="System.ApplicationException">Thrown when <see cref="SbieApi_QueryConf"/> returns non-zero</exception> public string QueryConf(string section_name, string setting_name, UInt32 setting_index = 0) { if (string.IsNullOrWhiteSpace(section_name)) throw new ArgumentNullException("section_name"); if (string.IsNullOrWhiteSpace(setting_name)) throw new ArgumentNullException("setting_name");

/// <summary> /// Enumerate Running Query Process Information /// </summary> /// <param name="box_name">Specifies the name of the sandbox in which processes will be enumerated.</param> /// <param name="all_sessions">Specifies TRUE to enumerate processes in all logon sessions or only in a particular logon session</param> /// <param name="which_session">Specifies the logon session number in which processes will be enumerated. Ignored if all_sessions if TRUE. Pass the value -1 to specify the current logon session.</param> /// <returns>An array of <see cref="ProcessInformation"/> for the supplied <paramref name="box_name"/></returns> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="box_name"/> is null or empty</exception> public ProcessInformation[] QueryEnumProcess(string box_name, bool all_sessions = true, Int32 which_session = -1) { if (string.IsNullOrWhiteSpace(box_name)) throw new ArgumentNullException("box_name");

/// <summary> /// Returns true if the sandbox is idle (has no running processes) /// </summary> /// <param name="box_name">Specifies the name of the sandbox in which to check for running processes.</param> /// <returns>Returns true if the sandbox specified in <paramref name="box_name"/> has no running processes.</returns> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="box_name"/> is null or empty</exception> public bool BoxIsIdle(string box_name) { if (string.IsNullOrWhiteSpace(box_name)) throw new ArgumentNullException("box_name"); return this.EnumProcess(box_name).Length == 0; }

/// <summary> /// Returns true if the sandbox is idle (has running processes) /// </summary> /// <param name="box_name">Specifies the name of the sandbox in which to check for running processes.</param> /// <returns>Returns true if the sandbox specified in <paramref name="box_name"/> has running processes.</returns> public bool BoxIsBusy(string box_name) { return !this.BoxIsIdle(box_name); }