|
In C#, using WMI has been great for access SI data. For reasons still unbeknownst to me, I cannot get a ManagementBaseObject instance (lets's call it mob) to properly invoke any of these function using InvokeMethod.
For example:
mof = "Rename";
name = "NEWNAME";
password = "adminpw";
user = "administrator";
object[] args = new args[3];
args[0] = name;
args[1] = password;
args[1] = user;
ManagementBaseObject mob = new ManagementBaseObject();
long retval = (long) mob.InvokeMethod(mof, name, password, user);
if (retval == 0)
Console.Writeline("Reboot PC for RENAME to take effect");
else
Console.Writeline("RENAME error {0}");
Everytime I get Invalid Method Parameter(s) message.
If anyone knows how to succcessfully call Rename, JoinDomainOrWorkgroup, etc from C#, please share the technique.
Thanks
|
|
|
|
|
There is a sample for InvokeMethod() here[^] and also here.[^] Frankly, I don't know how you managed to get to Invalid Method Parameter(s) with your code.
In January you said "Money in April" -
That was two years ago!
B. Python
|
|
|
|
|
The examples on MSDN are for Win32_Process and they work when I test. For some reason, Win32_ComputerSystem is behaving differently. I think there must a different way of calling the Win32_ComputerSystem functions. Here is the exact code that yields this result every time!
Exception caught!!!
System.Management: Invalid method Parameter(s)
public bool RenameComputer(string name, string password, string user)
{
bool ok = true;
string mofName = "Rename";
try
{
ManagementClass processClass = new ManagementClass("Win32_ComputerSystem");
object[] methodArgs = { name, password, user};
for (int i = 0; i < methodArgs.Length; ++i)
Console.WriteLine("methodArgs[{0}] = {1}", i, methodArgs[i]);
object retval = processClass.InvokeMethod(mofName, methodArgs);
Console.WriteLine("InvokeMethod Status {0}, Process ID {1}",
retval, methodArgs[3]);
}
catch (Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("{0}: {1}", e.Source, e.Message);
ok = false;
}
return (ok);
}
Has anyone been sucessful in calling Win32_ComputerSystem InvokeMethod() for Rename, or JoinDomainOrWorkgroup or UnjoinDomainOrWorkgroup. Help is greatly appreciated by me and anyone else dealing with this challenge.
|
|
|
|
|
Win32_ComputerSystem.Rename is an instance method so you first need to get the Win32_ComputerSystem instance to invoke it on:
ManagementClass computerSystemClass =
new ManagementClass("Win32_ComputerSystem");
object[] methodArgs = { name, password, user };
foreach (ManagementObject computerSystem in computerSystemClass.GetInstances())
{
object retval = computerSystem.InvokeMethod(mofName, methodArgs);
}
computerSystemClass.GetInstances() will return only one instance but you still need to use foreach. To avoid it, you could use the ManagementObject constructor that accepts the object path to get the instance:
ManagementObject computerSystem =
new ManagementObject("Win32_ComputerSystem.Name='SomeName'");
but you would have to get the computer name that you want to change first.
In January you said "Money in April" -
That was two years ago!
B. Python
|
|
|
|
|
|
Hi there!
I build C# application and then I add a setup project for the application within the solution. When I am installing the application, everything is going fine, I am starting the application, everything is going fine. When I want to insert some data in the database which is situated within the installation folder, I am getting this error:
a network-related or instance-specific error occured while establishing a connection to SQL server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL server is configured to allow remote connections. [provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified
Can someone explain me why I am getting this?? I will really appreciate. Thanks in advance, Laziale
|
|
|
|
|
what is the type of database you have in your installation folder?
The error you are getting is because you are trying to connect to an SQL Server instance. In which you need to setup SQL server and have the database part of an instance in that.
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
the type of the database is Sql Server Express database. How I can solve that problem, can you give me some hint, I don't have any idea. Thanks in advance, Laziale
|
|
|
|
|
Im not hugely experienced with using it but as far as i known you will need to install SQL Server Express on the target machine, or on a server if its a network application. I think you can download the redistributable for SQL server express. you will then need to include that in your setup application. Then once that is set up you can create your database on it and use it as, i assume, you are using it for testing
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hi!
The plan is this database to be used at the client environment. It is a management system, so they will need to pull and insert data in their local database. That's why I have doubts how to build this setup, I have SQL Server installed on my machine, I am installing the application, trying to insert some data and the error is occuring. That's what I want to know, why that error is occurring, since I have installed SQL Server on my machine. Thanks for your help, Laziale
|
|
|
|
|
does you application work when you test it before adding the setup?
Can you also post some code in relation to the database connection
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
yes, when I am running and debugging within VS, it works fine, I am inserting, selecting, deleting data from database. When I am running like standalone application, it gives me the error what I said in the first post.
Here is some code where I am selecting some data:
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Environment.CurrentDirectory + @"\App_Data\CMS.mdf;Integrated Security=True;User Instance=True;");
conn.Open();
string selectFamilyID = "SELECT Family_Id, Family_Name FROM Families WHERE Family_HouseHold = '" + fullName + "';";
SqlCommand selectCommand = conn.CreateCommand();
selectCommand.CommandText = selectFamilyID;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = selectCommand;
DataSet myDataSet = new DataSet();
string dataTableName = "Families";
adapter.Fill(myDataSet, dataTableName);
int familyID = 0;
DataTable table = myDataSet.Tables["Families"];
foreach (DataRow row in table.Rows)
{
familyID = Convert.ToInt32(row["Family_Id"]);
secondName = Convert.ToString(row["Family_Name"]);
}
|
|
|
|
|
hmmm. well i can only suggest that the application is not locating the file as expected.
You might want to try modifying the code so you get a OpenFileDialog prior to the connection. select you database location then use that information in the connection string. once you have done that test i VS, if that works then test with setup. If that also works then it must be something to do with the file path
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
The DB-filename isn't quoted. It might not find the file if there are spaces in the path to the database.
I are troll
|
|
|
|
|
how then is finding the file when I am running from visual studio?
|
|
|
|
|
I dunno, but by your reasoning a space can't be the problem
I are troll
|
|
|
|
|
But are you copying the database file to the installation directory. . .??
That means if you are installin it to some C:\Program Files\MyApplication\
then you need to copy the database file to that location. . .
According to me the main problem is in connection string . ..
What you can do is firstly you just give a message box showing the connection string by cn.ConnectionString.
So You can verify that the connection string is proper or not. . .
|
|
|
|
|
Greetings all!
My question:
From the main thread, how can I create two additional threads, each containing an object that will sit around and wait to be invoked? I want to do this because I don’t want the objects to be created and destroyed between invokes.
This is what I would like to do:
Have a main interface and two objects. The main form has 10 things it wants the objects to do, so upon starting it sends an item to each object via BeginInvoke (the objects are controls) to work and as soon as one of the objects is finished with an item, the main thread sends that object the next item to work, until all 10 are done.
Any help you can provide is much appreciated.
Thank you!
|
|
|
|
|
If by "the objects are controls" you mean "the objects are System.Windows.Forms.Control s" and you have your controls doing something business-y, then I think that you're wrong in your design and should look at something like the the ThreadPool class[^].
Furthermore, if those controls are Control s, then any updating that you want to do will have to be marshaled across thread boundaries back to the GUI thread.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Thank you for the response!
The objects are System.Windows.Forms.WebBrowser that have been made a child on their own form. What I would like to do is make two of these forms in their own threads, then BeginInvoke the WebBrowser to do the work I need.
|
|
|
|
|
Well, that sounds great. I think that you should do that.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
D'oh! Threads, not processes!
That won't work. Windows requires all painting to occur on the UI thread. If it's somewhere else, then you'll get lots of .NET exceptions.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Anticast wrote: What I would like to do is make two of these forms in their own threads,
Nope. Won't work. The forms will not paint properly on anything other than the UI thread. The browser components will also have nasty issues work on a background thread, if they work for you at all.
|
|
|
|
|
As others have said you have to do all UI work in the UI thread. What you can do is to create worker threads for each form and have them do all the data crunching and then give the results back to the UI thread in order to display them. The only way you could get the forms into separate threads is to make each form a separate process entirely and use IPC to communicate between them and your main app.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
Hello people, I have a project where i need to pass a file approx 2.68Gb or larger from one application to another. Now the problem is that in my project i have to use web services to do this operation.
I currently have the following setup in my code:
BinaryReader binReader = new BinaryReader(File.Open(parser.TemporaryFilesPath + "\\database.zip", FileMode.Open, FileAccess.Read));
binReader.BaseStream.Position = 0;
byte[] binaryfile = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
binReader.Close();
sc.Helllo(binaryfile);
The above code is in my first application. Than in my web service appliciton i have the following code that writes the binary file to the temp location where it is stored.
[WebMethod]
public void Helllo(byte[] fileinbytes)
{
XMLParser parser = new XMLParser();
parser.CreateTemporaryDirectory();
BinaryWriter binWriter = new BinaryWriter(File.Open(parser.TemporaryFilesPath + "\\database.zip", FileMode.Create,FileAccess.ReadWrite));
binWriter.Write(fileinbytes);
binWriter.Close();
parser.Unzip();
parser.DecryptFiles();
parser.MergeXMLDataToDatabae("Centre to Main Database");
}
Now the problem is here in the first code block when the database.zip file increases in size its giving me the error "Value was either too large or too small for an Int32".Do you think there is a way which i can work around this or another way in which i cna program this. Thank you for your help and time guys apprciate it.
|
|
|
|