Created
June 25, 2012 16:54
-
-
Save iguigova/2989846 to your computer and use it in GitHub Desktop.
Process query example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if (IsAsyncUploadAllowed(oAct, encAttachsToUpload) && BroadcastAsyncUploadAttachments(encAttachsToUpload)) | |
// { | |
// COlUIHandler.OnAllAttachUploadedComplete(null, new AttachUploadedEventArgs(false, null)); | |
// return; | |
// } | |
private static bool IsAsyncUploadAllowed(CActivation activation, CEncoding attachments) | |
{ | |
if (activation.pen.penProfile.useAsyncUpload) | |
{ | |
var uploadSize = 0; | |
for (var i = 1; i <= attachments.length; i++) | |
{ | |
var attachment = attachments.GetValueAsChild(string.Format("enc-attach{0}", i)); | |
if (attachment != null) | |
{ | |
uploadSize += attachment.GetValueAsInt("int-fileSize"); | |
} | |
} | |
return (uploadSize > activation.pen.penProfile.useAsyncUploadThreshold); | |
} | |
return false; | |
} | |
private static bool BroadcastAsyncUploadAttachments(CEncoding attachments) | |
{ | |
//TODO: Parameterize and unify all string identifiers across all components! | |
try | |
{ | |
var sAgentName = "Secure Desktop Agent"; | |
var sProcessName = "SecureDesktopAgent"; | |
var asyncUploadAgentPath = GetAsyncUploadAgentPath(sAgentName, STR_RunKeyPath); | |
if (string.IsNullOrEmpty(asyncUploadAgentPath)) | |
{ | |
asyncUploadAgentPath = GetAsyncUploadAgentPath(sAgentName, CRegistry.registryHome); | |
} | |
if (!string.IsNullOrEmpty(asyncUploadAgentPath)) | |
{ | |
if (ConfirmProcess(sProcessName, asyncUploadAgentPath)) | |
{ | |
IXDBroadcast broadcast = XDBroadcast.CreateBroadcast(XDTransportMode.IOStream); | |
broadcast.SendToChannel("email2Desktop", attachments.Encode()); | |
return true; | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
CLog.Log(string.Format("Exception attempting to broadcast async upload request: {0}", e.Message)); | |
} | |
return false; | |
} | |
private const string STR_RunKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Run"; | |
private static string GetAsyncUploadAgentPath(string name, string path) | |
{ | |
var runKey = Registry.CurrentUser.OpenSubKey(path); | |
if (runKey != null) | |
{ | |
foreach (string valueName in runKey.GetValueNames()) | |
{ | |
if (valueName.Contains(name) || valueName.Contains(name.Replace(" ", ""))) | |
{ | |
return runKey.GetValue(valueName).ToString(); | |
} | |
} | |
} | |
return string.Empty; | |
} | |
private static bool ConfirmProcess(string name, string cmd) | |
{ | |
if (!IsProcessRunning(name)) | |
{ | |
CLog.Log(string.Format("Attempting to start process '{0}'", cmd)); | |
if (!File.Exists(cmd)) | |
{ | |
CLog.Log(string.Format("Process not found '{0}'", cmd)); | |
return false; | |
} | |
// http://msdn.microsoft.com/en-us/library/e8zac0ca.aspx | |
var process = new Process(); | |
try | |
{ | |
process.StartInfo.FileName = cmd; | |
process.StartInfo.CreateNoWindow = true; | |
process.Start(); | |
return IsProcessRunning(name, 10); | |
} | |
catch (Exception ex) | |
{ | |
CLog.Log(ex, "ConfirmProcess"); | |
return false; | |
} | |
} | |
return true; | |
} | |
private static bool IsProcessRunning(string name, int maxNumOfAttempts) | |
{ | |
for (var i = 0; i < maxNumOfAttempts; i++) | |
{ | |
if (IsProcessRunning(name)) | |
{ | |
System.Threading.Thread.Sleep(5000); | |
return true; | |
} | |
System.Threading.Thread.Sleep(1000); | |
} | |
return false; | |
} | |
private static bool IsProcessRunning(string name) | |
{ | |
return (GetProcess(name) != null); | |
} | |
private static Process GetProcess(string name) | |
{ | |
foreach (var process in Process.GetProcesses()) | |
{ | |
if (process.ProcessName.Contains(name) && !process.ProcessName.Contains("vshost")) | |
{ | |
return process; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment