|
You could possibly use a class with FieldOffsetAttribute to replicate a C++ union; that might be a more flexible way of doing it.
[StructLayout(LayoutKind.Explicit)]
public class Converter
{
[FieldOffset(0)] public byte[] byteArray;
[FieldOffset(0)] public int intConverted;
[FieldOffset(0)] public string stringConverted;
} Maybe...
Paul
|
|
|
|
|
Thank you. I will look into this.
|
|
|
|
|
Konstantin Vasserman wrote:
System.Magic.ConvertEverythingToEverything kind of things... I guess, I am not that lucky...
There is one, in a way
Use the Marshal class in the following way.
1. Allocate a pointer to the size of your byte[]. Marshal.AllocHGlobal().
2. Copy the byte[] to the pointer. Marshal.Copy().
3. Copy the pointer to your selected array, eg int[] . Marshal.Copy().
4. Free the pointer. Marshal.FreeHGlobal().
Hope you get what I am saying..
Before you criticize a man, walk a mile in his shoes. That way, when you do criticize him, you'll be a mile away and have his shoes.
|
|
|
|
|
Interesting idea.
Thank you.
|
|
|
|
|
![Go to Parent](https://www.codeproject.com/App_Themes/CodeProject/Img/arrow-up24.png) This may not be the most efficient way of doing it, but it seems to work:
static Array ConvertByteArray(byte[] inArray, Type destType)
{
if (!destType.IsValueType)
throw new Exception("destType must be a value type!");
if (destType.IsEnum) destType = Enum.GetUnderlyingType(destType);
int iSize = System.Runtime.InteropServices.Marshal.SizeOf(destType);
if (iSize > 8)
throw new Exception("destType is too large!");
System.Diagnostics.Debug.Assert(
inArray.Length % iSize == 0,
"Invalid array length!");
Array outArray = Array.CreateInstance(
destType, inArray.Length / iSize);
for (int i=0, j=0;
i<inArray.Length && j<outArray.Length;
i += iSize, j++)
{
ulong val = 0;
for (int k=0; k<iSize && i+k < inArray.Length; k++)
val = (val * 256) + inArray[i + k];
for (int k=inArray.Length - i; k < iSize; k++)
val *= 256;
try
{
outArray.SetValue(Convert.ChangeType(val, destType), j);
}
catch (OverflowException ex)
{
ulong iMask = (ulong)Math.Pow(2, 8 * iSize - 1);
if ((val & iMask) != 0)
{
long val2;
if ((long)iMask < 0)
val2 = (long)val;
else
val2 = (long)(val & ~iMask) - (long)iMask;
outArray.SetValue(Convert.ChangeType(val2, destType), j);
}
else
throw ex;
}
}
return outArray;
}
If you know the type at compile-time, you can use:
byte[] a = new byte[6] {12, 34, 56, 78, 91, 111};
short[] b = (short[])ConvertByteArray(a, typeof(short));
This code will test the function:
static void TestConvert(Type T)
{
int tSize;
if (T.IsEnum)
tSize = System.Runtime.InteropServices.Marshal.SizeOf(
Enum.GetUnderlyingType(T));
else
tSize = System.Runtime.InteropServices.Marshal.SizeOf(T);
Random R = new Random();
int iBytes = R.Next(1, 10) * tSize;
Console.WriteLine("{0} bytes", iBytes);
byte[] a = new byte[iBytes];
for (int i=0; i<a.Length; i++)
{
a[i] = (byte)R.Next(255);
Console.WriteLine("a[{0}] = {1}", i, a[i]);
}
Array b = ConvertByteArray(a, T);
for (int i=0; i<b.Length; i++)
Console.WriteLine("b[{0}] = {1}", i, b.GetValue(i));
}
[Flags]
public enum TestEnum : byte
{
Value0 = 1,
Value1 = 2,
Value2 = 4,
Value3 = 8,
Value4 = 16,
Value5 = 32,
Value6 = 64,
Value7 = 128,
}
static void Main()
{
TestConvert(typeof(byte));
TestConvert(typeof(sbyte));
TestConvert(typeof(short));
TestConvert(typeof(ushort));
TestConvert(typeof(int));
TestConvert(typeof(uint));
TestConvert(typeof(long));
TestConvert(typeof(ulong));
TestConvert(typeof(TestEnum));
Random R = new Random();
byte[] a = new byte[4];
for (int i=0; i<4; i++)
{
a[i] = (byte)R.Next(255);
Console.WriteLine("a[{0}] = {1}", i, a[i]);
}
TestEnum[] b = (TestEnum[])ConvertByteArray(a, typeof(TestEnum));
for (int i=0; i < b.Length; i++)
Console.WriteLine("b[{0}] = {1}", i, b[i]);
}
|
|
|
|
|
Wow!
I think I can adjust your code to work with my data. My byte arrays don't come in any predefined length. It might be a byte[1] that I need to read as byte, it might be byte[8] that is int[2] or byte[16] that is uint[4] and stuff like that. Anyway, your code will help me a lot.
Thank you very much. ![Rose | [Rose]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/rose.gif)
|
|
|
|
|
Hi
Is there any reason why you could not update the data source in the DataTable.RowUpdated/RowDelete event functions. All the examples I've seen do the updating in another function and just use these events to (at the most) mark the grid as containing dirty data.
I don't want the user to click a button to update, I'd like it to be automatic, prompting the user if he changes or deletes a record (as these actions should be rare on the system I'm working on).
Cheers
Dave
|
|
|
|
|
Hi,
I have an application which part of its code should be done in script code in order to be modified by the user. I also should need to pass information to this script code, like instances of objects already created.
Is there any way to do it?, is there any sample or direct reference which explains it?
Thanks in advance,
Edgar
__________________________________________
Edgar Berengena Moreno
Software Engineer
Appeyron Research
|
|
|
|
|
I'm proud to announce the release of our first product Aspose.Obfuscator 1.0 on October 6, 2002! Aspose.Obfuscator is a .Net assembly obfuscator. With it, you can:
1. Obfuscate .Net Exe files while reserve all necessary names by itself
2. Obfuscate .Net Dll Files while reserve all necessary names by itself
3. Obufcate Asp.Net applications while reserve all necessary names by itself
4. Obfuscate applications whose type information is decided at runtime while reserve all necessary names by yourself
You can find more information at http://www.aspose.com and your comments or suggestions will be greatly appreciated.
Thanks,
Aspose Support Team
info@aspose.com
Aspose Pty Ltd
A .Net Component Developer
http://www.aspose.com
|
|
|
|
|
Nice cross-posting.
Who cares an obfuscator? We developers are spending most of our time trying to understand how .NET works. So obfuscation is just a useless thing to worry about.
You'd better improve your website. 15 clicks or so before I get anything shown on screen.
She's so dirty, she threw a boomerang and it wouldn't even come back.
|
|
|
|
|
StephaneRodriguez wrote:
Who cares an obfuscator?
YES, we obfuscate enough here in the forums
Before you criticize a man, walk a mile in his shoes. That way, when you do criticize him, you'll be a mile away and have his shoes.
|
|
|
|
|
Who cares?
This topic is very important (strategicquestion ) for me. And I wonder what is the best obfuscator.
|
|
|
|
|
i have created a setup wizard type project in dot net.
it contain an exe file that contain a service.
i want to install it during the setup wizered.
so that i could not do any thing manually.
i know its possible because i have seen many examples like that.
what stapes i have to perform to do that?
r00d0034@yahoo.com
|
|
|
|
|
I find some errors can not be caught in its original position if it is called in FORM_PAINT event.
a simple example, the hello function will raise a null pointer error.
In Paint, the debugger in VS will show highlight at Main, the root caller of whole application.
But in Click, the debugger will show the right error raising position in hello().
So what's on earth, the VS can't find the error source while it is called by Paint?
I recently turn all the Paint debug work to a click event.
But is there any setting to make Paint event accept the error?
void hello()
{
string s;
s = null;
s.IndexOf("hello");
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
hello();
}
private void Form1_Click(object sender, System.EventArgs e)
{
hello();
}
|
|
|
|
|
The OnPaint() method is called by the guts of the WinForms library, and is wrapped in a try/catch block. The exception is being handled in a catch() handler there, which is why the debugger reports to you that it does not have the source code for the execution point.
You can handle this two ways:
- You can use the Debug | Exceptions dialog box to tell VS.NET to break immediately after any exception is thrown (whether or not it is handled). This will cause VS.NET to break inside your OnPaint() method.
- You can wrap your entire OnPaint implementation in a try/catch block so you handle all exceptions yourself (and possibly re-throw ones you can't recover from).
I would suggest the second option.
--
Russell Morris
"Have you gone mad Frink? Put down that science pole!"
|
|
|
|
|
how can i achieve video streaming using .NET technology. that is, capture video from my logitech web cam and transfer it to client broswer?
thank you.
regards
yccheok
|
|
|
|
|
|
quite nice. that that is nothing to do with live video streaming ![Smile | :)](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|
|
------------------------------------
Bitmap b = new Bitmap("\\live.jpg");
this.pictureBox1.Image = b;
------------------------------------
i make my picture box point to live.jpg. when i try to edit the live.jpg (ie:when i try to replace the live.jpg, or i try to call Bitmap.Save("live.jpg") in another thread), i was not allowed to do so.
i try to change the code to:
------------------------------------
Bitmap b = new Bitmap("\\live.jpg");
this.pictureBox1.Image = b;
b.Dispose();
b = null;
------------------------------------
this will cause the run-time error n make the picture box diaplay a BIG cross instead of the content of live.jpg.
i then try to duplicate the bitmap using:
------------------------------------
Bitmap b = new Bitmap("\\live.jpg");
this.pictureBox1.Image = b.Clone(new Rectangle(0, 0, b.Size.Width, b.Size.Height), b.PixelFormat);
b.Dispose();
b = null;
------------------------------------
now, i still cannot edit the live.jpg (ie:when i try to replace the live.jpg, or i try to call Bitmap.Save("live.jpg") in another thread).
how can i release the handle to live.jpg file and at the same time let the picture box to have the content of live.jpg.
thank you.
regards
yccheok
|
|
|
|
|
Hi,
Just wondered if anyone has tried this already?
I'm trying to write a button control that use
APIs in uxtheme.dll to draw its background image.
I also want to have an alpha blend option for the
button. My solution is to draw the theme background
on a temporary bitmap, do the alpha blending, and
then, copy it to the screen.
But here comes a problem. If theme background already
contains some pixels which are semi-transparent, these
pixels are drawn in black color on the target bitmap.
While there isn't any problem if I want the theme
background image to be drawn on the screen (alpha pixels
are rendered correctly on the screen, perfectly drawn on
top of whatever sits there.) Does anyone know the
solution to draw a theme background correctly onto a
bitmap?
Thanks
Li-kai Liu
|
|
|
|
|
how to uninstall a software programatically ?
r00d0034@yahoo.com
|
|
|
|
|
how to creat a setup project that will install an application through a wizerd ?
how to creat DNS programatically ?
how to logoff and login programatically ?
r00d0034@yahoo.com
|
|
|
|
|
I hope anyway!
I want to do (for example):
StreamReader sr;<br />
if (cond1)<br />
sr = new StreamReader(@"C:\temp.txt");<br />
<br />
for (int i = 0 ; i < 100 ; i++)<br />
{<br />
if (cond1)<br />
sr.WriteLine("{0},{1}",v1,v2);<br />
}<br />
<br />
if (cond1)<br />
sr.Close()
but the compiler flags up (sr) as unassigned. What's the easy way out of this?
Rgdz
B.
|
|
|
|
|
Why are you writing to a StreamReader?
My guess is that you really want a StreamWriter and because the file doesn't exist, new StreamReader(...) , it's returning null .
Paul
|
|
|
|