Click here to Skip to main content
15,912,897 members
Home / Discussions / C#
   

C#

 
QuestionWin service jumps out Pin
suunker1-Nov-11 10:01
suunker1-Nov-11 10:01 
AnswerRe: Win service jumps out Pin
DaveyM691-Nov-11 11:22
professionalDaveyM691-Nov-11 11:22 
AnswerRe: Win service jumps out Pin
PIEBALDconsult1-Nov-11 13:32
mvePIEBALDconsult1-Nov-11 13:32 
QuestionTheming Custom Controls At Design-Time Pin
Matt U.1-Nov-11 8:48
Matt U.1-Nov-11 8:48 
AnswerRe: Theming Custom Controls At Design-Time Pin
BobJanova2-Nov-11 0:00
BobJanova2-Nov-11 0:00 
Questioncreate DSN for oracle programatically Pin
KKSharma31-Oct-11 22:56
KKSharma31-Oct-11 22:56 
AnswerRe: create DSN for oracle programatically Pin
PIEBALDconsult1-Nov-11 2:50
mvePIEBALDconsult1-Nov-11 2:50 
QuestionHelp with GetProcesses Pin
CCodeNewbie31-Oct-11 13:03
CCodeNewbie31-Oct-11 13:03 
Greetings Gurus,

I have been searching the forums and tuts for an answer to a question regarding getting running processes off a XP SP3 box without luck so I am posting here hoping one of the Gurus could help me solve this problem.

Running a Windows service under the Local System account (yes I am aware of the dangers this poses but I have taken care of that).

If I run
C#
try
{
SqlConnection myconn = new SqlConnection("User id = a; password = b; server=c;" database=e; connection timeout=30");
SqlCommand insp = new SqlCommand("INSERT INTO dbo.tbconn(Tstamp, SysID, PID, PName)VALUES (@Tstamp, @SysID, @PID, @PName)", myconn);

insp.Parameters.Add("@Tstamp", SqlDbType.DateTime);
insp.Parameters.Add("@SysID", SqlDbType.Int);
insp.Parameters.Add("@PID", SqlDbType.Int);
insp.Parameters.Add("@PName", SqlDbType.NVarChar, 50);

DateTime Tstamp = DateTime.Now;
Process[] processlist = Process.GetProcesses();
foreach (Process p in processlist)
    {
        insp.Parameters["@Tstamp"].Value = Tstamp;
        insp.Parameters["@SysID"].Value = 100;
        insp.Parameters["@PID"].Value = p.Id;
        insp.Parameters["@PName"].Value = p.ProcessName; 
        
        ProcMon.Open();
        insp.ExecuteNonQuery();
        ProcMon.Close();
}
}
catch (Exception procinfo)
{

everything writes to the table as expected, however I get 3 errors if I run
C#
try
{
SqlConnection myconn = new SqlConnection("User id = a; password = b; server=c;" database=e; connection timeout=30");
SqlCommand insp = new SqlCommand("INSERT INTO dbo.tbconn(Tstamp, SysID, PID, PName, PExe, PCPU, PMem)VALUES (@Tstamp, @SysID, @PID, @PName, @PExe, @PCPU, @PMem)", myconn);

insp.Parameters.Add("@Tstamp", SqlDbType.DateTime);
insp.Parameters.Add("@SysID", SqlDbType.Int);
insp.Parameters.Add("@PID", SqlDbType.Int);
insp.Parameters.Add("@PName", SqlDbType.NVarChar, 50);
insp.Parameters.Add("@PExe", SqlDbType.NVarChar, -1);
insp.Parameters.Add("@PCPU", SqlDbType.Int);
insp.Parameters.Add("@PMem", SqlDbType.Int)

DateTime Tstamp = DateTime.Now;
Process[] processlist = Process.GetProcesses();
foreach (Process p in processlist)
    {
        insp.Parameters["@Tstamp"].Value = Tstamp;
        insp.Parameters["@SysID"].Value = 100;
        insp.Parameters["@PID"].Value = p.Id;
        insp.Parameters["@PName"].Value = p.ProcessName;
        insp.Parameters["@PExe"].Value = p.MainModule.FileName; //Error 1
        insp.Parameters["@PCPU"].Value = p.TotalProcessorTime;  //Error 2
        insp.Parameters["@PMem"].Value = p.VirtualMemorySize64; //Error 3

        
        ProcMon.Open();
        insp.ExecuteNonQuery();
        ProcMon.Close();
}
}
catch (Exception procinfo)
{

Error 1: System.ComponentModel.Win32Exception: Unable to enumerate the process modules - Still writes all fields but excludes "IDLE"

Error 2: System.InvalidCastException: Failed to convert parameter value from a TimeSpan to a Int32. System.InvalidCastException: Object must implement IConvertible - Writes all values except the TotalProcessorTime,"SYSTEM" and "IDLE" values.

Error 3: System.ComponentModel.Win32Exception: Unable to enumerate the process modules - Writes all values except TotalProcessorTime, "SYSTEM" and "IDLE".

I have tried running it piecemeal and as a Console App but I can't seem to shake these errors (even trying some Convert.ToX stuff).

Can someone help me move this forward please?

Many thanks.
AnswerRe: Help with GetProcesses Pin
Pete O'Hanlon31-Oct-11 20:46
mvePete O'Hanlon31-Oct-11 20:46 
GeneralRe: Help with GetProcesses Pin
CCodeNewbie31-Oct-11 23:29
CCodeNewbie31-Oct-11 23:29 
GeneralRe: Help with GetProcesses Pin
Pete O'Hanlon1-Nov-11 0:46
mvePete O'Hanlon1-Nov-11 0:46 
AnswerRe: Help with GetProcesses - Sort of Solved Pin
CCodeNewbie2-Nov-11 12:11
CCodeNewbie2-Nov-11 12:11 
QuestionAudio processing Pin
Guy00731-Oct-11 8:09
Guy00731-Oct-11 8:09 
AnswerRe: Audio processing Pin
phil.o31-Oct-11 8:44
professionalphil.o31-Oct-11 8:44 
QuestionUnique values in list Pin
nitin_ion31-Oct-11 0:09
nitin_ion31-Oct-11 0:09 
AnswerRe: Unique values in list Pin
Pete O'Hanlon31-Oct-11 0:57
mvePete O'Hanlon31-Oct-11 0:57 
GeneralRe: Unique values in list Pin
nitin_ion31-Oct-11 1:01
nitin_ion31-Oct-11 1:01 
GeneralRe: Unique values in list Pin
Not Active31-Oct-11 2:06
mentorNot Active31-Oct-11 2:06 
GeneralRe: Unique values in list Pin
Pete O'Hanlon31-Oct-11 7:33
mvePete O'Hanlon31-Oct-11 7:33 
AnswerRe: Unique values in list Pin
Reiss31-Oct-11 3:28
professionalReiss31-Oct-11 3:28 
QuestionWCF server don't detect lost or broken connection? Pin
teknolog12331-Oct-11 0:04
teknolog12331-Oct-11 0:04 
AnswerRe: WCF server don't detect lost or broken connection? Pin
PIEBALDconsult31-Oct-11 2:44
mvePIEBALDconsult31-Oct-11 2:44 
GeneralRe: WCF server don't detect lost or broken connection? Pin
teknolog12331-Oct-11 3:18
teknolog12331-Oct-11 3:18 
GeneralRe: WCF server don't detect lost or broken connection? Pin
jschell31-Oct-11 8:34
jschell31-Oct-11 8:34 
GeneralRe: WCF server don't detect lost or broken connection? Pin
PIEBALDconsult31-Oct-11 14:02
mvePIEBALDconsult31-Oct-11 14:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.