|
By comparing the IList<T> interface with the List<T> class I noticed that one difference is that List<T> implements IList as well as IList<T>. Implementing IList in addition to IList<T> in my class seemed to fix the problem, although I have no idea why?
|
|
|
|
|
I am currently developing a Form that will scan a plugins directory and load any dll files that have a baseType of "MadPages.PageBase"
For proof of concept, I created a Control that extended the PageBase class. The form application found the dll file and loaded it into the application as planned.
My new problem is this. I have a new project in the solution named ProjectA. ProjectA is another control library that extends the PageBase class. ProjectA has a reference to another project which I called ProjectB. When my form application finds the DLL file for ProjectA, I do a foreach loop on the dll file's GetTypes().
This of course now comes up with 2 types...PageBase and ProjectB. When I try to dynamically load the Type PageBase into a local PageBase variable, I get an exception stating that it cannot find the dll for ProjectB. If I add a reference to my main form's project (pointing to to ProjectB), the application can load ProjectA. I know I must be doing something wrong.
The point of having these plugins was so I don't have to touch the main form's project when I want to extend it. Please let me know what I am doing wrong. Here is the code on how i'm loading my dll files from the plugin-dir:
Assembly asm= Assembly.LoadFile(path);
foreach (Type t in asm.GetTypes())
{
if (t.BaseType.ToString() == "MadPages.PageBase")
{
return (PageBase)t.GetConstructors()[0].Invoke(new object[] { });
}
}
There is only one constructor for the PageBase class. Thanks!
|
|
|
|
|
One more little thing I found out while de-bugging. If I step through the constructor for ProjectA, as soon as it tries to make a call to ProjectB it bombs meaning that it cannot find the file.
When I compile ProjectA and run it, everything is fine. The ProjectB's dll file is in the same folder as ProjectA's. I am copying these dll files over to a plugins directory for the main application. Both dll files are once again in the same folder but it looks like once I copy both of them to another directory, ProjectA doesn't find the dll for ProjectB. Someone wanna school me on this awful Friday?
|
|
|
|
|
|
Worked! I owe you a beer good sir. Thanks a lot!
|
|
|
|
|
Make it a Guiness
only two letters away from being an asset
|
|
|
|
|
Hi all,
going nuts... i placed a windows media player on my form (tried the 6.4 version and version 10) and like to play a mkv file (720p x264).
I can play the mkv in the old media player that comes with xp (C:\Program Files\Windows Media Player\mplayer2.exe) without any problems.
The embedded player tries to connect to the internet, probably to find a codec.
It only plays the sound.
Does anyone know how i can make the mkv play in my form ?
thanx a lot.
|
|
|
|
|
Matroska (Mkv) is just a container (like avi), so the file format says nothing about the codec used. Try installing CCCP, and it will probably work.
Standards are great! Everybody should have one!
|
|
|
|
|
i don't want to install other codecs, since it works in the standard windows mediaplayer, it should work in my embedded player too.
|
|
|
|
|
Hai Everybody,
I want to encrypt the user name and password given by the user separately and i have to store the encrypted content in sql server. yesterday our forum members suggested me to use the SHA256 algorithm so that i referred one article in our forum but it encrypts and decrypts some file contents.
I felt very difficult to understand also.
So can anyone suggest me some other simple and strong algorithm.
Best Regards,
M. J. Jaya Chitra
|
|
|
|
|
The System.Security.Cryptographic namespace within the Microsoft .NET Framework provides a variety of tools to aid in encryption and decryption. The CryptoStream class is used here to demonstrate the encryption and decryption with System.Security.Cryptographic.SymmetricAlgorithm.
For Details[^]
Regards,
Satips.
|
|
|
|
|
I cant help you with SHA256, but maybe another solution is acceptable for you.
I'am using the MD5, but this is only a unidirectional encryption.
I show a dialog to enter a password and encrypt the password via MD5. The encrypted result will then be stored in a database.
When the user logs on again, he must enter the password, which will be encrypted again with MD5.
To verify if the password is correct i compare the encryption stored in the database, with the new encryption.
To get the MD5 encryption do the following:
public static string StringToMd5(string text)
{
byte[] data = System.Text.Encoding.Unicode.GetBytes(text);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in result)
{
s.Append(b.ToString("x2").ToLower());
}
return s.ToString();
}
may be it suits you...
greets Snow
|
|
|
|
|
Just as a note MD5 is considered insecure now and it's recommended to change to the SHA-1 hash instead.
|
|
|
|
|
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Security.Cryptography;<br />
<br />
namespace Helper.Crypto<br />
{<br />
public class HashMethods<br />
{<br />
public static string GenerateSalt()<br />
{<br />
byte[] Salt = new byte[32];<br />
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();<br />
rng.GetNonZeroBytes(Salt);<br />
return Encoding.UTF8.GetString(Salt);<br />
}<br />
public static string Hash(string text,string saltString)<br />
{<br />
string SaltedString = text + saltString;<br />
byte[] SaltedText = Encoding.UTF8.GetBytes(SaltedString);<br />
HashAlgorithm hash = new SHA512Managed();<br />
byte[] hashBytes = hash.ComputeHash(SaltedText);<br />
return Convert.ToBase64String(hashBytes);<br />
}<br />
}<br />
}
I store both the salt and the hashed PWD+Salt in a DB.
When a client tries to log in the server gives it the salt and the client adds the salt to the username and hashes. pass the hashed password up to the server and compare with the value stored in the DB.
This way you never know the user's password.
HTH
Russ
|
|
|
|
|
Does anybody know how I can get the SQL Connection DialogBox to show up in C#?
You know, the one that comes up when you open SQL query analyzer. I think that I need to use Microsoft.Data.ConnectionUI which I need to install another SDK to use. Does anybody know which SDK this is?
Any help would be appreciated
Thanks
There are 10 types of people in the world, those who understand binary and those who dont.
|
|
|
|
|
I'm looking for a way to represent an instantiated class as a byt array (and populate an instantiated class frm a byte array. I was hoping that [Serializable] would be the magic bullet but it seems I was wrong - or do I just not understand how to use [Serializable] properly?
Let's say I have:
public class myClass
{
public byte myByte1;
public byte myByte2;
public ushort myUshort1;
public ushort myUshort2;
public myClass()
{
this.myByte1 = 0x01;
this.myByte2 = 0x02;
this.myUshort1 = 0x0304;
this.myUshort2 = 0x0506;
}
}
Now, if I instantiate this class with myClass myInstance = new myClass() the member variales will hold the values as defined by the constructor. I'd like to be able to view the contents of the class as an array of bytes (ie. {0x01, 0x02, 0x03, 0x04, 0x05, 0x06} ).
So I was thinking that if the class is serializable and I write the seraialized stream to a byte array (or write a byte array to the associated stream) I'd be able to pull it off, something like this:
MemoryStream memStream = new MemoryStream();
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(memStream, myInstance);
byte[] myBytes = memStream.ToArray();
But myBytes is 183 bytes in size (as opposed to the expected 6 bytes) once this is finished and the contents of myBytes makes no sense to me whatsoever.
Am I on completely the wrong track here or is there hope?
|
|
|
|
|
Here are many additional information is added with serialization like assembly name, version, class name, structure, ........
All this information is necessary to deserialize this object
|
|
|
|
|
Aha, that makes sense. So does that mean that [Serializable] would not be the proper route then?
Would you have any other suggestions?
|
|
|
|
|
Dewald wrote: Aha, that makes sense. So does that mean that [Serializable] would not be the proper route then?
Yes, if the array size is not critical.
If critical you have to realize you own convertion like:
<br />
public class myClass<br />
{<br />
public byte myByte1;<br />
public byte myByte2;<br />
public ushort myUshort1;<br />
public ushort myUshort2;<br />
<br />
public myClass()<br />
{<br />
<br />
}<br />
<br />
public static byte[] ToByteArray(myClass value)<br />
{<br />
byte[] result = new byte[6];<br />
<br />
result[0] = value.myByte1;<br />
result[1] = value.myByte2;<br />
unchecked<br />
{<br />
result[2] = (byte)(value.myUshort1 / 256);<br />
result[3] = (byte)(value.myUshort1 - result[2] * 256);<br />
result[4] = (byte)(value.myUshort2 / 256);<br />
result[5] = (byte)(value.myUshort2 - result[4] * 256);<br />
}<br />
<br />
return result;<br />
}<br />
<br />
public static myClass FromByteArray(byte[] array)<br />
{<br />
myClass result = new myClass();<br />
<br />
result.myByte1 = array[0];<br />
result.myByte2 = array[1];<br />
result.myUshort1 = (ushort)(array[2] * 256 + array[3]);<br />
result.myUshort2 = (ushort)(array[4] * 256 + array[5]);<br />
<br />
return result;<br />
}<br />
}<br />
P.S. usort is 16-bit type so we need to store it into two bytes.
|
|
|
|
|
Yip, this is exactly what I've been doing up until now but I was just hoping that there wuld be a more elegant sollution. The thing is that I have various classes that I'd like to be able to pack into byte arrays (basically the classes are just messages that I need to transmit and receive over a TCP socket).
I was hoping that I could write a base class that provides a method of packing the class into an array and then derrive all the other classes from that class. That way I wouldn't have to implement a huge amount of tedious code to pack every individual member variable of the class into the array.
|
|
|
|
|
Sorry, I have no ideas how to help you this case.
If you message is more than 6 byte the volume of valuable information will be more. Also you may try to zip it by DeflateStream....
|
|
|
|
|
Dewald, last night I got an idea: in unmanaged C++ we just need to look at the object as byte array:
<br />
MyClass myCl = new MyClass();<br />
byte[] array = meCl;
But in .Net we haven't references (that is very good, in my opinion), but we have some classes that allow to work with objects by references way.
One of them is System.Runtime.InteropServices.Marshal
I have never worked with, just herad about it.
I have being looking through this and find some pairs of static methods wich can help you (I guess):
To get IntPTR from your object:
public static extern void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld);
public static object PtrToStructure(IntPtr ptr, Type structureType)
To copy to byte array:
public static void Copy(IntPtr source, byte[] destination, int startIndex, int length);
public static void Copy(byte[] source, int startIndex, IntPtr destination, int length)
Or
public static extern byte ReadByte(object ptr, int ofs);
public static extern void WriteByte(IntPtr ptr, int ofs, byte val);
Regards.
P.S. IntPtr (intPointer) is Net analog of C++ pointers
-- modified at 2:48 Saturday 9th June, 2007
|
|
|
|
|
I've done the copying for value object (structure). One thing I can't make is to get a pointer (IntPtr) from reference object (class).
<br />
public struct myStuct<br />
{<br />
public byte myByte1;<br />
public byte myByte2;<br />
public ushort myUshort1;<br />
public ushort myUshort2;<br />
<br />
private const int MY_SIZE = 6;<br />
public static byte[] ToByteArray(myStuct source)<br />
{<br />
IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(MY_SIZE);<br />
System.Runtime.InteropServices.Marshal.StructureToPtr(source, pointer, false);<br />
<br />
byte[] result = new byte[MY_SIZE];<br />
System.Runtime.InteropServices.Marshal.Copy(pointer, result, 0, MY_SIZE);<br />
<br />
return result;<br />
}<br />
<br />
public static myStuct FromByteArray(byte[] source)<br />
{<br />
if (source == null) throw new ArgumentNullException("Source array can not be null");<br />
if (source.Length != MY_SIZE) throw new ArgumentException("Source array's size has to be " + MY_SIZE + " bytes");<br />
<br />
IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(MY_SIZE);<br />
System.Runtime.InteropServices.Marshal.Copy(source, 0, pointer, MY_SIZE);<br />
<br />
myStuct result = (myStuct)System.Runtime.InteropServices.Marshal.PtrToStructure(pointer, typeof(myStuct));<br />
<br />
return result;<br />
}<br />
}<br />
|
|
|
|
|
Thanks for all the effort AikinX,
I also discovered this approach but, like you, I couldn't get it to work for classes. I never even bothered to try using it for structures (silly of me) and this will do nicely.
It is a bit of a pity that I have to resort to unmanage code. I would have prefered if there was a way that I could do this without having to use pointers, and there probably is which is still elluding me.
Either way, for now, this will do. Thanks again.
|
|
|
|
|
I don't like pointers too. But it is the only way to do the serialization in general for any class.
So, I think you have to choose built-in serialization or do it manually.
Best Regards
|
|
|
|