|
aaah... ok I see... That's it. That's what I was searching for.
Thanks a lot!
so, for testing an System.Array object, I have to use the "is" keyword or the typeof() function? I saw that REG_MULTI_SZ are considered as System.Array in .NET... I guess this is an array of "String"...
|
|
|
|
|
You're right - I wasn't thinking. A REG_MULTI_SZ would be an array. In that case, first do a simple check for the System.Array type, then you could get the first element and check it's type. It'll either be a String or Byte so use typeof since Byte is a value type.
Using is is slightly faster (1 or 2 instructions as opposed to 2 or 4 - you make the call how important that is) than using typeof , but can only be used when the second operand is a reference type. Below is an IL comparison of using each(str is a variable referencing a String ):
ldloc.0
isinst [mscorlib]System.String
ldloc.0
callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
ldtoken [mscorlib]System.String
call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(
valuetype [mscorlib]System.RuntimeHandle)
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You know this inverted shortcut icon on top of each page in VS.NET help; you're supposed to click on it, and it drops down a yellow menu that makes you filter the page by C#, VB or JScript. Well, I have to do this for each page. I want it to stick. Can I do that?
Thanks.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
It is supposed to persist for the class reference, but often does not persist for technical articles where such a feature is enabled. This just has to do with the information stored in the MSHelp metadata in each page and any cookies that are created (or aren't created, which is the problem with not being persisted).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi,
i want to redirect a certain window output into my application???????
thanx
|
|
|
|
|
First off, is this a WinForm or WebForm environment? If it's web, are you using frames within the same "window"? Or, are you wanting 2+ separate windows to talk to each other?
This is a start. Let me know.
Michael Flanakin
Web Log
|
|
|
|
|
He wants to redirect the output of another app that he didn't write, like Word or Excel, to a window in his app. Something like PCAnywhere or Remote Desktop Connection.
RageInTheMachine9532
|
|
|
|
|
yes i want to redirect window like excel or word into my app like PcAnyWhere
|
|
|
|
|
Well, at least your question wasn't as simple as I was thinking. Then again, maybe that would've been better. Unfortunately, I don't have a good answer for you. I could make a few speculations, but without playing around with it, your guess is as good as mine. Sorry.
Michael Flanakin
Web Log
|
|
|
|
|
This has been bugging me for awhile now. Can someone give me an example where you would use Application.Run without parameters?
|
|
|
|
|
All Application.Run does is start the message pump. If you give it a window, it will dispatch messages to the window and will automatically terminate the pump (post messages like WM_QUIT ) when that window is closed. You can use Application.Run() to simply start a message pump without displaying a form. You can later call Application.Exit to terminate the pump (also known as the application message loop).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for your response Heath. I'm still a little confused though. I'm pretty familiar (although not an expert) with the message pump and the role Application.Run plays in creating it. I guess what I'm confused about is if I call Application.Run without specifying a form then how do I go about getting the window handle for use in the SendMessage or PostMessage api's? In other words, how can I actually use the message pump that Application.Run creates? Does the NativeWindow class somehow factor into all of this?
|
|
|
|
|
Consider the following example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Test : Form
{
static void Main()
{
Test t = new Test();
t.ShowDialog();
Application.Run();
}
TextBox tb;
public Test()
{
tb = new TextBox();
tb.Location = new Point(8, 8);
Controls.Add(tb);
Button b = new Button();
b.Location = new Point(tb.Right + 8, tb.Top);
b.Text = "Click";
b.Click += new EventHandler(b_Click);
Controls.Add(b);
}
private void b_Click(object sender, EventArgs e)
{
MessageBox.Show(tb.Text);
Application.Exit();
}
} Now, I'm not sure why you'd want to do this, but you could. It's most likely that this would be handy when you want to have a dialog that doesn't act as the main window handle for an application that receives general application messages posted to the pump. You'll notice (if you compile and run this) that closing the dialog doesn't quit the application. Perhaps someone might have a good reason for using such an approach.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Interesting. I never would have thought that the button click message would have been dispatched correctly in this code. Thanks for the example. The reason I was asking is because I have to be able to use DDE in a service or console application so I was experimenting with different ways of creating the message pump without displaying a form. I have successfully used Application.Run with the form parameter, but it seems like a hack since the form is always hidden. I wonder if I could do something similar with the NativeWindow class? (Just thinking out loud!)
By the way, DDE sucks.
|
|
|
|
|
Nice code. Exactly what i needed for a game .
|
|
|
|
|
Hi,in C# i can list all the users in the domain using active directory services.But i just want to list only active users,i.e.users that have been already login.thanks anyway.
|
|
|
|
|
You could query the Last-Logoff and Last-Logon AD attributes (see DirectoryEntry.Properties ) and see if the latter is later than the former (i.e., they logged on after they logged off).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone,
In C# I can manipulate known objects in the registry using the Registry and RegistryKey classes. But I would like to know what is the equivalent of the RegQueryValueEx Win32 API in C#? Can anyone tell me please? I would like to know (and therefore handle) the type (REG_SZ, REG_MULTI_SZ...) of a given value in the registry when I browse subkeys and hives.
Thanks for the answers.
|
|
|
|
|
i got two buttons in the form. One is for encrypting the message read from a xml file and then encrypt the xml and save back to another file. The other button is for decryption. Again, this button read the encrypted file back, and then perform decryption.
But i don't know why an exception always thrown when processing this line of code "int actualbytesread = encStream.Read(buf, 0, 100);" ... Can anyone give me some instructions what is happening ?
private void Encrypt_Click(object sender, System.EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(Application.StartupPath + "\\Data\\userInfo.xml");
Byte[] data = new UnicodeEncoding().GetBytes(xml.OuterXml);
MemoryStream ms = new MemoryStream(data.Length* 2);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(),
CryptoStreamMode.Write);
encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();
//calculate the length of the encrypted data
byte[] bResult = new byte[ms.Position];
ms.Position = 0;
ms.Read(bResult, 0, bResult.Length) ;
rich.Text += new UnicodeEncoding().GetString(bResult) + "\n\n"; encStream.Close();
FileStream fs = new FileStream(Application.StartupPath + "\\Data\\file.try", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
fs.Write(bResult, 0, bResult.Length);
fs.Close();
}
private void Decrypt_Click(object sender, System.EventArgs e)
{
FileStream fs= new FileStream(Application.StartupPath + "\\Data\\file.try", FileMode.Open, FileAccess.Read, FileShare.None);
byte[] ByteArray=new byte[fs.Length];
int nBytesRead = fs.Read(ByteArray, 0, ByteArray.Length);
fs.Close();
MemoryStream ms = new MemoryStream(ByteArray);
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
MemoryStream plaintextmem = new MemoryStream();
ms.Position = 0;
do
{
//Create a byte array into which we will read the plaintext
//from CryptoStream
Byte[] buf = new Byte[100];
try
{
//read the plaintext from CryptoStream
int actualbytesread = encStream.Read(buf, 0, 100);
//if we have reached the end of stream quit the loop
if (0 == actualbytesread)
break;
//copy the plaintext byte array to MemoryStream
plaintextmem.Write(buf,0,actualbytesread);
}
catch(Exception ab)
{
MessageBox.Show(ab.Message, "Asdf");
}
}while(true);
//don't forget to close the streams
encStream.Close();
ms.Close();
//Extract the plaintext byte stream and close the MemoryStream
Byte[] plaintextbyte = plaintextmem.ToArray();
plaintextmem.Close();
//Encode the plaintext byte into Unicode string
string plaintext = new UnicodeEncoding().GetString(plaintextbyte);
encStream.Close();
}
I would appreciate any response and help from yours ~
|
|
|
|
|
Anyone here can give me some guides ? ....
|
|
|
|
|
Be patient. This isn't 24x7 support, it's a community forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
It helps to know what exception you're getting and what it states. And don't just give me the text from MessageBox.Show (which is not really a debugging tool - try a breakpoint or the Debug or Trace facilities at the very least), the actual exception type is good to know. Besides, in production code you'll give your user 1) a user-unfriendly message, and 2) a malicious user too much information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
In the try-catch block, this kind of error is showed when i list it out
"Data Error"
Mscorlib (in Mscorlib.dll)
at System.Security.Cryptography.CryptoAPITransform._DecryptData(IntPtr hKey, Byte[] rgb, Int32 ib, Int32 cb, Boolean fDone)
at System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at WindowsApplication3.Form1.button10_Click(Object sender, EventArgs e)
|
|
|
|
|
You're still not telling me the actual exception Type, only the message.
In any case, try using a buffer that is a power of 2 (2, 4, 8, 16, 32, 64, ...) when decrypting. This is a pretty common requirement when decrypting so that the decryption algorithm has all that it needs to decrypt a block. This usually isn't the case when you don't have a block of the right size, which is almost always a power of 2.
Also, don't new-up a UnicodeEncoding object every time you use it. Use Encoding.Unicode which create a single instance to be used any time you use it, which saves time and memory:
byte[] buffer = Encoding.Unicode.GetBytes(someString);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Actually, there was no exception being thrown.
If you have seen the code ... the try-catch is something that i insert to see what thing is happening ...
This is because ... the program was hanged in the do loop ...
I don't know how to express ... if you have tried the code, you will know what is happening...
Things made me mad ...
Anyway, u teach me a lot and thanks for your reply ...
i still look forward you can help me to solve this problem ....
|
|
|
|
|