|
Well...
<br />
static public Data data[] = {<br />
new Data(0, "Zero"),<br />
new Data(1, "One" ),<br />
new Data(-1, NULL )};<br />
In a sense, it's the only logical thing to do (and allow)..
After all, in C# arrays are actually a type of their own
(unlike the older C style case where an array is just a
pointer).
So, while the first line begins the instanciation of a new
Array of Data, you still have to instanciate and initialize
each data (hence the other lines look like they do).
The longer version of the same code is (forgetting about static and public):
<br />
Data[] data = new Data[3];<br />
data[0] = new Data(0, "Zero");<br />
data[1] = new Data(1, "One");<br />
data[2] = new Data(-1, NULL);<br />
Until the second line, data[0] has not been instanciated to be
a Data object (yet, data[] has been instanciated to be an array of
Data objects that can accomodate 3 Data instances).
Might seem inefficient, but, thinking of the Data instances as objects
of their own right, it makes sense (sort-of)...
..sure, in your case they are structs, but I think that at this point they
just figured it was better to avoid a different syntax for structs vs classes...
I saw this situation in Java the first time,
and it took a bit to get used to it (yeah, I kept on thinking that
after Data[] data = new Data[3] all three data objects where instanciated,
pointing to NULL... again, they are not pointers!).
F.O.R.
|
|
|
|
|
I'm actually not sure what happens in this case. Your data type is a struct so it's a value type. Typically value types are allocated on the stack instead of the heap. However, you're array is most likely (please correct me if I'm wrong here) is a reference type and will be allocated on the heap. So I'm not sure exactly where the value types memory will be allocated.
Jared
jparsons@jparsons.org
www.prism.gatech.edu/~gte477n
|
|
|
|
|
Arrays are reference types, therefore they're always allocated on the heap.
An array of structs is no different -- it'll be on the heap -- however, in this case the memory block on the heap will be a contiguous series of Data structs, one after the other. All that the "new Data()" calls are doing is initializing the various elements of the array.
Cheers, Julian
Program Manager, C#
This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
|
|
That's music to my ears.
Joe Woodbury
When all else fails, there's always delusion.
- Conan O'Brien
|
|
|
|
|
I'm putting together initial specs on a new version of a business application.
One of the items on the potential features list is to give the end user the ability to modify one of the main forms in the program. It's database driven and the idea is that they can move around fields (combo boxes, text input boxes etc) to their liking, removing some if not required etc until they have it the way they like it, then save it as a template for others to use.
One wrinkle in this is that we will also have an asp.net presentation layer into the same database that will replicate as closely as possible the winforms appearance. In an ideal world they templates would work with both asp.net and windows forms.
Anyone ever tried this with .net and c# or have any words of inspiration?
Strangers passing in the street
By chance two separate glances meet
And I am you and what I see is me...
|
|
|
|
|
|
Thank you, there is some useful info there and I had not seen that before.
Cheers!
Everybody knows that the bird is the word.
|
|
|
|
|
i have the following problem with a remoting application.
when a client try to register for events of the remote object, the host for these remote objects throws a file not found exception
saying that it can't find client aplication excecutable file in directory where the excecutable for the host application is located.
when i copy the client excecutable file to that derectory it works fine.
i dont know why it needs the client excecutable for that, if object address and method address are contained with the event delegate.
please help me.
|
|
|
|
|
i have dll written in VB 6.0 and it contains some controls.Let's call it MyControls.dll. I imported this dll to my C# project and put controls from it on toolbox.Visual studio created for me AxInterop.Mycontrols.dll,named AxMyControls in solution explorer.So,when i drop a control from toolbox to the form it is AxMyControls.OneControl,not a MyControls.OneControl.
So,when i call function which need a MyControl.OneControl as a parameter,
i have an error passing AxMyControls.OneControl into it.
What should i do? I was trying to create GUI by hand but
MyControls.OneControl ControlObject=new MyControls.OneControl();
does not work cause of
C:\WORK\Karn\ExcellentConverter0.01\ExcellentConverter0.01\FrmImport.cs(59): 'Mycontrols.OneControl.OneControl()' is inaccessible due to its protection level
can i change protection level (private/public) in dll and recompile it?
what is a constructor in VB 6?
thanks for answers
sorri for my english
|
|
|
|
|
Hello,
here is my problem; using the GDI+ i draw a string using DrawString() and i draw a rectangle around this string.
But when i scale the view (to simulate a zoom in or zoom out) the string can becomes larger than the rectangle !
It seems that the true type fonts are not truly scalable.
However i read that the GDI+ subtly manipulates the character and word spacing...
Can you help me ?
Thanks a lot.
|
|
|
|
|
I guess MeasureString() is what you need.
Mazy
No sig. available now.
|
|
|
|
|
Sorry but in my case the rectangle is drawn by the user near the text...
|
|
|
|
|
OlivierMDVY wrote:
It seems that the true type fonts are not truly scalable.
Yes, I've faced this problem as well before.
first of all, how do you perform the zoom in out effect?
if you are usign Graphics scalling technique, you can define the Font size to the point scale,
eg.
Font f = new Font("Courior", 25.4f*50.0f/72f, GraphicsUnit.World);
// the 50f is the default size I used.
then draw it by DrawString(), you can see the scale changed if graphics scale change.
Hope this help~~
|
|
|
|
|
I want to be downloading 5 files simultaneously. I have an array of threads created, each downloading a file. These files though, are actually pages of a book. So I want the thread to download a page only once (of course!). What I wanted to do was to maintain a shared global page number in the main thread, so that when each of the worker thread finishes it's current page, it reads this number, decrements it, and downloads it. Only, two things:
1- I want this number to somehow be stored on disk, so that if a power cut happens, or windows restarts or any of these issues, I'd be able to resume.
2- Of course, I don't want the threads to access the global number at the same time, so that they wouldn't corrupt it. How can I enable this lock?
Thank you very much.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
Don't use an array of threads. Use async IO or ThreadPool.QueueUserWorkItem() instead.
Use the C# lock statement or possibly Interlocked.Decrement() to protect your shared state.
Think about the fact that what your just described will not correction resume because you describe decrementing a counter when the download is started. You need to maintain state about which ones are started and which ones are completed. You can't rely on them completing in the order the downloads were started.
Why do you think you need to download five pages simultaneously anyway? That sounds suspect to start with.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Blake Coverett wrote:
You can't rely on them completing in the order the downloads were started.
Actually, what I was planning to do is, if I detected that the download was interrupted, I would increment this counter by 5, and re-start. This way, all pages that were downloading at that time, will be resumed.
Blake Coverett wrote:
Why do you think you need to download five pages simultaneously anyway? That sounds suspect to start with.
I'm writing a program for someone to parse his daily online reports. It's much faster if I download the html pages simultaneously than to get them one by one. Also, I can parse one page while the other is downloading.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
profoundwhispers wrote:
Actually, what I was planning to do is, if I detected that the download was interrupted, I would increment this counter by 5, and re-start. This way, all pages that were downloading at that time, will be resumed.
You can't get away with just a single counter like that. That's the class problem I was describing when I said you couldn't count on them completing in order. Consider this sort of pattern:
counter = 10
start 1, counter = 9
start 2, counter = 8
start 3, counter = 7
start 4, counter = 6
start 5, counter = 5
end 2
start 6, counter = 4
If it aborts here, incrementing the counter by 5 puts it back at 9, meaning page 2 will be downloaded twice and page 1 lost.
You need to track completions separately.
profoundwhispers wrote:
I'm writing a program for someone to parse his daily online reports. It's much faster if I download the html pages simultaneously than to get them one by one. Also, I can parse one page while the other is downloading.
Right, but why particularly 5 at a time? What you probably want to do is just fire off all your WebRequest.BeginGetResponse and let the normal infrastructure dispatch them a few at a time. It already knows how to balance such things.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Hi!
I'm working on a P2P app, with security built in, it's going to be released as open source as soon as I have a working prototype...
I've gotten as far as exchanging messages, encrypting username password with RSA and exchanging a common symmetric key. But when I try to decrypt messages encrypted with the sym-key I get an error:
Binary stream does not contain a valid BinaryHeader, 95 possible causes...
I have the following code:
<br />
public byte[] SecureSerialize(byte[] key, byte[] iv)<br />
{<br />
if (encrypted == Encryption.NotEncrypted) throw new Exception("Message type should be serialized with Serialize()");<br />
<br />
byte[] streamBuffer = new byte[BufferSize];<br />
<br />
streamBuffer[0] = (byte)msgType;<br />
streamBuffer[1] = (byte)encrypted;<br />
<br />
MemoryStream ms = new MemoryStream(streamBuffer, sizeOffset, BufferSize - sizeOffset);<br />
<br />
RijndaelManaged rij = new RijndaelManaged();<br />
CryptoStream cs = new CryptoStream(ms, rij.CreateEncryptor(key, iv), CryptoStreamMode.Write);<br />
<br />
BinaryFormatter bf = new BinaryFormatter();<br />
bf.Serialize(cs, this);<br />
<br />
cs.Flush();<br />
ms.Flush();<br />
<br />
SHA1Managed hashAlg = new SHA1Managed();<br />
byte[] hash = hashAlg.ComputeHash(streamBuffer, 0, (int)ms.Position + sizeOffset);<br />
bf.Serialize(ms, hash);<br />
<br />
byte[] returnBuffer = new byte[(int)ms.Position + sizeOffset];<br />
Array.Copy(streamBuffer, 0, returnBuffer, 0, (int)ms.Position + sizeOffset);<br />
<br />
return returnBuffer;<br />
}<br />
<br />
public static UdpMessage SecureDeserialize(byte[] msg, byte[] key, byte[] iv)<br />
{<br />
if ((Encryption)msg[1] == Encryption.NotEncrypted) throw new Exception("Message type should be deserialized with Deserialize()");<br />
<br />
byte[] decryptedBuffer = msg;<br />
<br />
MemoryStream ms = new MemoryStream(decryptedBuffer, sizeOffset, decryptedBuffer.Length - sizeOffset);<br />
<br />
RijndaelManaged rij = new RijndaelManaged();<br />
CryptoStream cs = new CryptoStream(ms, rij.CreateDecryptor(key, iv), CryptoStreamMode.Read);<br />
<br />
BinaryFormatter bf = new BinaryFormatter();<br />
UdpMessage message = (UdpMessage)bf.Deserialize(cs);<br />
<br />
cs.Flush();<br />
ms.Flush();<br />
<br />
message.msgType = (MessageType)msg[0];<br />
message.encrypted = (Encryption)msg[1];<br />
<br />
SHA1Managed hashAlg = new SHA1Managed();<br />
byte[] calculatedHash = hashAlg.ComputeHash(decryptedBuffer, 0, (int)ms.Position + sizeOffset);<br />
byte[] msgHash = (byte[])bf.Deserialize(ms);<br />
for (int i = 0 ; i < calculatedHash.Length ; i++)<br />
if (calculatedHash[i] != msgHash[i])<br />
throw new CryptographicException("Hash missmatch");<br />
<br />
return message;<br />
}<br />
The same code without the encryption stuff works fine.
Anybody has a clue what's wrong?
And while I'm at it... Is there any good way to protect against man in the middle attacks when exchanging keys? Is the application very vunerable with out that protection?
/Regards 3ddA
|
|
|
|
|
Just a quick reply to your final question (I'm afraid I don't have time at the moment to figure out your code): SSL was designed to protect against man-in-the-middle attacks. Check out the specification of SSL to understand how it works (it involves the exchange of certificates for example).
Cheers, Julian
Program Manager, C#
This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
|
|
What I've gathered so far is that it all comes down to having some trusted site to sign a certificate for you. I guess using that service will cost money and will not be that interesting for this type of friend2friend communication application.
I think I'll settle for a hash of the public key encoded in base64. This could be calculated on the client side too and then compared by using the telephone or something if MITM-attack is suspected. I guess MITM-attack of voice communication still is very hard to do
I guess problems like this will have to be solved by people that really knows security when the app is available to the public.
Thanx for the help. I now realize how much more complex this is than exchanging public keys.
|
|
|
|
|
I've written code a little easier to understand, and hope it will give some response...
Key and iv are double checked and correct data is received as it was sent.
<br />
public byte[] SecureSerialize(byte[] key, byte[] iv)<br />
{<br />
byte[] streamBuffer = new byte[BufferSize];<br />
<br />
MemoryStream ms = new MemoryStream(streamBuffer);<br />
ms.WriteByte((byte)encrypted);<br />
<br />
RijndaelManaged rij = new RijndaelManaged();<br />
CryptoStream cs = new CryptoStream(ms, rij.CreateEncryptor(key, iv), CryptoStreamMode.Write);<br />
<br />
BinaryFormatter bf = new BinaryFormatter();<br />
bf.Serialize(cs, this);<br />
<br />
SHA1Managed hashAlg = new SHA1Managed();<br />
byte[] hash = hashAlg.ComputeHash(streamBuffer, 0, (int)ms.Position);<br />
bf.Serialize(ms, hash);<br />
<br />
byte[] returnBuffer = new byte[(int)ms.Position];<br />
Array.Copy(streamBuffer, 0, returnBuffer, 0, (int)ms.Position);<br />
<br />
return returnBuffer;<br />
}<br />
<br />
public static UdpMessage SecureDeserialize(byte[] msg, byte[] key, byte[] iv)<br />
{<br />
MemoryStream ms = new MemoryStream(msg);<br />
byte enc = (byte)ms.ReadByte();<br />
<br />
RijndaelManaged rij = new RijndaelManaged();<br />
CryptoStream cs = new CryptoStream(ms, rij.CreateDecryptor(key, iv), CryptoStreamMode.Read);<br />
<br />
BinaryFormatter bf = new BinaryFormatter();<br />
UdpMessage message = (UdpMessage)bf.Deserialize(cs);
<br />
message.encrypted = (Encryption)enc;<br />
<br />
SHA1Managed hashAlg = new SHA1Managed();<br />
byte[] calculatedHash = hashAlg.ComputeHash(msg, 0, (int)ms.Position);<br />
byte[] msgHash = (byte[])bf.Deserialize(ms);<br />
for (int i = 0 ; i < calculatedHash.Length ; i++)<br />
if (calculatedHash[i] != msgHash[i])<br />
throw new CryptographicException("Hash missmatch");<br />
<br />
return message;<br />
}<br />
Regards 3ddA
|
|
|
|
|
Hey guys...
I am writing an SMTP server and am interested in adding support for open relay blacklists.
However, beyond the implementation, what I need to achieve is retrieving the remote IP from the TcpClient object.
I know that this can be retrieved from a raw socket object (which can be generated through TcpListenet.AcceptSocket() as opposed to TcpListener.AcceptTcpClient() ), although this doesn't support streaming.
Any ideas?
Tatham Oddie (VB.NET/C#/ASP.NET/VB6/ASP/JavaScript)
tatham@e-oddie.com
+61 414 275 989
|
|
|
|
|
You don't want to use TcpListener at all for another even remotely performant. Stick to Socket.Accept().
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Why do you say this? I would have though that something relating to TCP would be best performed by a TcpListener?
Is it performance issues, functionality (like what i'm trying to do) or what?
Tatham Oddie (VB.NET/C#/ASP.NET/VB6/ASP/JavaScript)
tatham@e-oddie.com
+61 414 275 989
|
|
|
|
|
TcpListener is just a wrapper around Socket. It doesn't give enough flexibility for anything but the most simple applications. Certainly not enough to implement an SMTP server.
--
-Blake (com/bcdev/blake)
|
|
|
|
|