|
How do I get the current logged in user name of a remote nt machine?
I know there is a way in delphi, but how to master it in C#, may be with an API call?!
Looking forward to some answers.
|
|
|
|
|
Do you mean the user that's logged into a remote machine, or a remote user logged into your machine? How did you do it in Delphi? If you used a native API then you can do the same thing in C#. Read about Consuming Unmanaged DLL Functions[^] in the .NET Framework SDK, and see http://pinvoke.net[^] for many common method signatures.
You can also use WMI, for which classes are defined in the System.Management namespace. Read Accessing Management Information with System.Management[^] also in the .NET Framework SDK.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi all!
I'm supposed to create an MMC snap-in in C# for XP/Win2000.
As far as I've seen, there is no support for this in C#. And most of the time people recommend the framewok of IronRingSoftware as a great starting point.
Now I wonder, if I'm adding a reference to Microsoft Management Console 2.0 in the Visual Studio IDE (Add Reference -> COM tab -> Microsoft Management Console 2.0), what am I referencing? By doing this I can write MMC20. in the code window, and a list of interfaces and methods appear.
Is this reference useful for creating snap-ins?
If this is a way of building snap-ins, what is the difference between the MMC reference and the framework from IronRingSoftware?
Thanx in advance
|
|
|
|
|
esjq wrote:
Is this reference useful for creating snap-ins?
It's necessary. MMC is a COM client, and snap-ins are COM servers. .NET can communicate with COM using Runtime Callable Wrappers (RCWs) to consume COM components, and with COM-Callable Wrappers (CCWs) to expose managed component to COM. You're actually doing both. You must implement those interfaces defined in that interop assembly you created, and register your assembly with regasm.exe.
There are rules to exposing your managed components to COM. NEver use auto-generated interfaces, and in this case you wouldn't have to because it's the interfaces defined in that interop assembly that you're implementing. This means, on your classes, you set ClassInterfaceAttribute to ClassInterfaceType.None . Always attribute your classes and interfaces - if you define any - with a unique GuidAttribute that you do not change. Never change published interfaces. These are all rules for writing COM components and must be followed when exposing managed components to COM, otherwise your COM clients (like MMC) will never find your components (because the CLSID, or class ID - a GUID for your classes keeps changing) or because the interfaces IDs (IIDs) keep changing. Read Design Considerations for Interoperability[^] in the .NET Framework SDK, as well as Exposing .NET Components to COM[^] for more information about this topic.
Code Project has some good articles about developing MMC snap-ins, including how to do it in managed code. Creating MMC Snapin using C# (Part I)[^] is one such example. You can search for others.
You should also read the MMC Programmer's Guide[^], which takes you through developing a snap-in (using native COM, but the information is still very much applicable since you'll be implementing the native interfaces in your managed code using the interop assembly).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thank you! Really appreciate the information and the links.
|
|
|
|
|
this is my code...why it doesn't update changes to my datasource???
void MenuItem3Click(object sender, System.EventArgs e)
{
MyConn = new OdbcConnection(myConnectionString);
string SQL = "SELECT * FROM novice ORDER BY id_novice DESC";
OdbcDataAdapter myCommand = new OdbcDataAdapter(SQL,MyConn);
myCommand.SelectCommand = new OdbcCommand(SQL, MyConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "novice");
OdbcCommandBuilder custCB = new OdbcCommandBuilder(myCommand);
BindingManagerBase cm = BindingContext[dataGrid1.DataSource] ;
DataRowView drv = (DataRowView )cm.Current ;
((DataRow )drv.Row).Delete () ;
myCommand.Update(ds, "novice");
ds.AcceptChanges();
}
________________________
live and die by the code
|
|
|
|
|
IMO, to call an OdbcDataAdapter instance myCommand is confusing. You should - following suggested naming guidelines in the .NET Framework sDK - use myAdapter at the very least. Something even more descriptive than that, using "adapter" somewhere, would even be better.
Also, while not the problem, calling ds.AcceptChanges() is not necessary. The Update method does this for you upon successful completion of updating the data store (i.e., no exceptions are thrown).
You should try debugging the code to make sure that the OdbcCommandBuilder constructed suitable SQL statements for each of OdbcDataAdapter 's properties besides the SelectCommand which you've already defined (and is required for the command builder).
Are you sure that the DataSet is full and that all the data is in the "novice" DataTable ? At times you have to set up OdbcDataAdapter.TableMappings if the table from which you're selecting doesn't result in the table name you expect. This is a problem with SQL Server, which names it's tables (result sets) "Table", "Table1", "Table2", and so on.
I also assume that the BindingContext code is elsewhere, perhaps as part of a Button.Click handler? Make sure that however you bind your data, you use the same exact approach to get the BindingContext . To eliminate any problems, it's recommended that you use the following:
CurrencyManager cm = (CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember</code>]; This ensures that if you set only DataSource to a DataTable , or set the DataSource to the DataSet and the DataMember to the table name (which is recommended for data navigation or to support multiple table views if binding multiple tables throughout the lifetime of that particular DataGrid ), that either works. The binding context must be exactly the same as how you bound your data, or it will not work.
Finally, debug your application or add some debugging code to make sure that the DataSet actually includes changes (which could be a result of not binding correctly, as I stated above). You can do this easy by using DataSet changes = ds.GetChanges();</pre>That will filter out all the unchanged data so you don't have to sift through so much, since examining <code>DataSet s in the variables windows is difficult (Whidbey, i.e. Visual Studio 2005, will improve this with debugging viewers for various types of objects, which is also extensible for your own objects).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello!
I have a problem, and nobody seems to know the remedy...
There are a lot of programmes capable of editing (doing various things) custom file formats, for example game editors.
For instance, there is a fifaFS console application which gives the oppurtunity of editing .big graphic archives of FIFA 2005 from EA Sports. You can pack, unpack files, rebuild etc.
So how did the creator manage to write such software? I mean, there is nothing in Delphi capable of editing .big files (it's written in Deplhi, but I am a C# programmer so I want to know how to do it in C#)... So how?!
I heard that you need to know the structure of the file... Ok, so for example I have the file specification of BMP file format.
But how do I do stuff with it? Please explain! And please give some piece of code... I really need your help!
Please don't tell me that .big is a commercial file format owned by EA, so there's no chance of editing it. I know at least 20 programmes capable of editing .big file.
Thanks!
Regards,
Lars
|
|
|
|
|
I dont htink there is anything magical with this file format. I think its just something where several files are packed into a single file. At the beginning of those files one can clearly the the contents.
You could just dig through one sample file and try to findout yourself how exactly it works. You could also contact someone from one of those 20 programs and ask him if he has any infos conecrning the exact format.
So it doesnt have to do anything with the programming language uyou use, but with the knwoledge you have of the format. If you know it a simple stream is enough to unpack the contents.
|
|
|
|
|
Yeah, well, thanks, but I am kind of a begginer...
So could anyone please give an example code for the available BMP file format, just to demonstrate the technique? Please!
The BMP specification is here: http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/BMP.txt
Or just type 'File formats' in Google, then check out the first site. Choose whichever file you like, and please help me!
Thanks!
|
|
|
|
|
It won't help much if someone shows you how to read file format A (for example, BMP) if you want to modify file format B.
When you get down to it, each file is just a bunch of bytes you can read or write (check BinaryReader or BinaryWriter in the FCL, for example).
But unless you get the specification _what_ the bytes at a certain offset mean, you're out of luck.
You might be able to scan for certain byte patterns, but usually when you try to modify a file without knowing the exact structure you'll end up with a corrupt file.
Once you have the structure you can write classes that read or write the correct files, but not any sooner.
mav
|
|
|
|
|
Ok, thanks! But you didn't get it: I don't want to edit those .big files, I just stated them as an example. I just want to understand how does one edit some file format. And I wanted some simple code about working with those bytes...
|
|
|
|
|
Editing the file format meaning understanding exactly what each byte in the format means. There is no one method for editing every file format. You read the file stream in, parse it up based on the rules for the file format, edit it based on the rules of the file format, and then write it back out.
Are you asking how to read a file? How to parse up the data? or what?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I would manage all the inputText of Internet Explorer and InputBox of other running software by my Program, so iif the user make right click on the input it shows the normal windows contextmenu cut/copy/delete but with a new vox relative to my program, infact if cliccked it call my program and it can return a text to put autmatically inside...
Yes it is a bit complicated but can you help me to understand how to do?
Thanks.
|
|
|
|
|
With a new "vox"? (Voice activation?) Could you elaborate?
You can interact with both the WebBrowser control (which is actually Internet Explorer) or a running instance (or create a new instance if none already exist) of the InternetExplorer class by creating an RCW (Runtime Callable Wrapper) from shdocvw.dll, which you can do using either tlbimp.exe from the .NET Framework SDK, or by importing a COM reference to "Microsoft Internet Controls" using the COM tab when you click "Add Reference..." for your project.
You should search this site for "WebBrowser" or "InternetExplorer" (no space, since it's a class name). There are lots of articles about hosting the WebBrowser control in Windows Forms, as well as advanced hosting interfaces. Either way allows you to handle when documents are loaded, then walk the document object model (DOM) using the WebBrowser.Document property and casting it to the mshtml.IHTMLDocument2 , which is defined in the Microsoft.mshtml.dll assembly that should've installed with VS.NET (and a number of other applications, or you can create your own from mshtml.dll as well). Walking the DOM is just like scripting a DHTML document, only with a little bit of extra work (and believe me, a lot less than with native C++ code) to cast objects to interfaces that implement the property or method you seek, which script does automatically).
You can also do this using the InternetExplorer class to automate a running instance of IE.
You could also implement a toolbar using managed code (though this isn't recommended as it will significantly increase the working set - amount of memory allocated to the process - for whatever process is hosting the WebBrowser control, be it iexplore.exe, explorer.exe (Windows Shell), or some other process). There are articles about that here on Code Project as well, if you'd only search. Try searching for "IE toolbar".
To search this site, use the text box at the top of every page underneath the logo.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello,
I'm new to the C# language but I'm trying to build a little app that will let me monitor to see if my computer is making HTTP/FTP/UNC requests to the outside world. I was just wondering how I can start on this? Is there a System namespace or class that I can use??
Thank you very much,
Troy
|
|
|
|
|
does anyone know how to start this one?
thanks
|
|
|
|
|
I'm wondering what the best threadsafe way to create and use general purpose routines like special date functions, special user calculations, etc. Up until now, what I have been doing is to create a file call myutils.cs that looks something like the code below. I can call it without instantiating it because it is static which is very convienient. I am calling these routine from multiple threads and my app is crashing so I'm concerned I may have made a fatal mistake.
Could someone enlighten me please if what I am doing is major bad? Or, possibly suggest a better way to do the common things.
Thanks
Peter
public class PolarUtils
{
public static int[] HillsForUser(string currentUserId)
{
SqlConnection sqlConnection = PolarUtils.GetNewSqlConnection();
sqlConnection.Open();
string sqlSelectString = "SELECT id_hill FROM HillFile ";
sqlSelectString += " WHERE UserId = '" + currentUserId + "'";
SqlCommand sqlCom
...
public static SqlConnection GetNewSqlConnection()
{
SqlConnection sqlConnection1 = new SqlConnection();
string str = null;
|
|
|
|
|
Within the same AppDomain, access to static members is serialized so they are automatically thread-safe.
What is the exception that is thrown. Saying "my application crashes" is hardly specific enough to provide you with any decent amount of help.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
This may be obvious, but does that mean that static members act like critical sections in that access from another thread will be automatically blocked until the current thread exits that static method? If so, that's great to know. If possible, could you point me to a link where I can read more about it?
Thanks again for both answers,
-Peter
|
|
|
|
|
Actually, upon looking for your documentation I found this to not be true. I was told this once a long time ago in the managed newsgroups. Guess I shouldn't take things for granted, huh?
In that case, use the C# lock keyword (which uses a Monitor when you compile) or a Mutex to synchronize access.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Oh, and using string concatenation to create SQL expressions is a terribly BAD idea. All I have to do is pass 0 OR 1=1; DROP TABLE HillFile -- and bye bye HillFile data. I could do a lot worse, too, depending on the content which I could easily discover since most RDBMS expose schema information that can be queried, like SQL Server and Access.
Use parameterized queries. See the SqlCommand.Parameters property documentation[^], as well as the SqlParameter class documentation[^] for details and examples. This also could be the reason you're getting an exception, if you're passing in string data that includes quotes anywhere. Using parameterized commands aleviates this problem, as well as avoiding SQL injection attacks. If you're getting a SqlException , which is most like the problem.
And, BTW, if your applications "crashes" because of an exception thrown, you really need to handle exceptions better. There is no reason - short of a catastrphic failure and in order to protect data - that an application should crash. Always validate your data and handle problem areas with try-catch blocks.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
thanks for the tips. In my new stuff I always use the .Parameter rather than sql because of the issues you mention. Also, I do need to better handle my errors so I know what the conditon was on the thread crashes.
|
|
|
|
|
Can someone suggest the different ways to persist/share temporary data across classes in a multi-tier WinForm Application?
For example, assume that I have a Data Structure/Class that holds [connection related data] and another one that holds [user preferences data]. Where can I store –temporarly- this data so I can readily access it from any object in the application with ease.
The following are the methods that I am aware of:
1. Share data through static methods... Bad idea because it is Public.
2. Share using AppDommain.CurrentDomain’s SetData and GetData methods.
Thanks in advance!
|
|
|
|
|
DaMagicMaster wrote:
The following are the methods that I am aware of:
1. Share data through static methods... Bad idea because it is Public.
2. Share using AppDommain.CurrentDomain’s SetData and GetData methods.
Both ways are public. Statics are only the same within the same AppDomain . Any other application or code in another AppDomain won't see the same thing.
Using approach #1, however, they don't have to be data. If your application is in one assembly, you can declare the members as internal , but this defeats the purpose since for a single assembly internal is the same as public . In .NET 2.0 you can also use the InternalsVisibleToAttribute in your assembly to name which other assemblies can see internals. Currently you can use code access security to allow only certain assemblies to access those members, for instance by their public key token (since all assemblies should be signed, and you typically sign all your organization's assemblies - or at least assemblies for a particular product - with the same key pair). See the StrongNameIdentityPermissionAttribute in the .NET Framework SDK for details.
In a N-tier application, that data must also be serializable, so don't forget that. An assembly on one machine couldn't access the data on another without that data being marshaled, which requires the data to be serializable in .NET. If you're using the .NET Framework features - such as .NET Remoting[^], this means attributing your classes and structures with the SerializableAttribute and optionally implementing ISerializable to control the serialization of an object. If you create your own mechanism - which isn't recommended between two managed applications, but may be necessary if supporting legacy code either on the client or server - then you must make sure you marshal your data in other ways. Windows drag-n-drop and the clipboard have a few different ways of marshaling data, from global memory to streams and more.
Between managed code, however, I highly recommend .NET Remoting[^]. It's like Web Services on steroids, and allows you to serialize to binary formats and use different wire formats (like TCP instead of HTTP, which is - by nature - unidirectional). You can even define your own formatters and channels, as well as other sinks which act on the data in a non-specific way (a great example of Aspect-Oriented Programming, or AOP).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|