|
Two ways to do this:
1) Pass in the real type:
Send(MyStruct s)
{
...
// inside fixed...
*((MyStruct) pBufer) = s;
2) pass in a byte* pointer, and pass things across directly. If you do this, you'll need to have the fixed statement in the calling routine, so that you can get the pointer.
|
|
|
|
|
Sorry, I guess I act as stupid but I still havent' understood 100% how to go.........
1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way?
2) Are those two method declarations equivalent?
3) If i pass void*, what would be the substitute for MyStruct?
4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object')
5) Could you give an example how to go, to receive the respective byte[] in this specific case?
your help is greatly appreciated!!
regards,
newbie stonee
public unsafe override bool SendPacket(void* PacketStart, long PacketSize) <br />
{ <br />
System.Console.WriteLine("Virtual SendPacket() called!");<br />
<br />
try<br />
<br />
{<br />
byte[] buffer = new byte[PacketSize];<br />
<br />
fixed (Byte* pBuffer = buffer)<br />
{<br />
<br />
*((MyStruct) pBuffer) = PacketStart; <br />
<br />
}<br />
m_socket.Send(buffer, buffer.Length,SocketFlags.None);<br />
<br />
return true; <br />
<br />
<br />
}<br />
<br />
catch(SocketException se)<br />
{<br />
System.Console.WriteLine(se.Message); <br />
return false; <br />
} <br />
}
|
|
|
|
|
I'll try to be clearer:
1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way?
You need to get from the the managed world to the pointer world somehow to use the pointer operations. If you pass in a MyStruct (for example), you would do the fixed statement in your routine. If you want to pass in a pointer, you'd have to do the fixed statement in the caller of the routine.
While you'll have to write a separate routine for each struct, I think that writing the managed version and putting all the ugliness in your routine is the rigth
2) Are those two method declarations equivalent?
No, though they are similar. I'm not sure the object version is feasible, though I've never tried it myself.
3) If i pass void*, what would be the substitute for MyStruct?
If you pass a byte* (on reflection, I don't think void* will work), you would need to copy the bytes over one by one to the byte buffer, based on the length you'd pass in.
4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object')
Object won't work here, because the compiler does't know how big the actual object is.
5) Could you give an example how to go, to receive the respective byte[] in this specific case?
Here's some code I wrote up. It compiles, but I haven't actually tested it:
using System;
using System.IO;
using System.Net.Sockets;
namespace ConsoleApplication7
{
struct MyStruct
{
int i;
float j;
}
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}
public unsafe void SendMyStruct(MyStruct myStruct, Socket socket)
{
byte[] buffer = new byte[sizeof(MyStruct)];
fixed (byte* pBuffer = buffer)
{
*((MyStruct*)pBuffer) = myStruct;
}
socket.Send(buffer);
}
public unsafe MyStruct ReadMyStruct(Socket socket)
{
byte[] buffer = new byte[sizeof(MyStruct)];
socket.Receive(buffer);
MyStruct myStruct;
fixed (byte* pBuffer = buffer)
{
myStruct = *((MyStruct*)pBuffer);
}
return myStruct;
}
}
}
|
|
|
|
|
Hi,
I'm building an application where I'd like to put icons in resources. However as I don't use VS.net but the free IDE SharpDevelop, this is not quite so simple. I created a new resource file by the name SharpPrivacyMain.resource with the Resourcer and saved it in the same directory as SharpPrivacyMain.cs.
In SharpPrivacyMain.cs I added the followin code:
<br />
MessageBox.Show(String.Join("\n\r", Assembly.GetExecutingAssembly().GetManifestResourceNames()));<br />
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SharpPrivacyMain)); <br />
this.bmpMnuClipboard = ((Bitmap)resources.GetObject("$this.menuClipboard"));<br />
I get only get to resources.GetObject, then a MissingResourceException is thrown. The messagebox shows me "SharpPrivacyMain.resources", so it seems that the resource was correctly compiled into the assembly. And the Bitmap $this.menuClipboard is also in the resource file. When I doubleclick it in the resource editor of SharpPrivacy, I can even see the image.
I don't know if the problem comes from the IDE, or if I just can't write working programs .
Any help would be really appretiated!
The exact Exception message is:
[thread 0xd04] Unhandled exception generated: (0x04a96490) <System.Resources.Mis
singManifestResourceException>
_className=<null>
_exceptionMethod=<null>
_exceptionMethodString=<null>
_message=(0x04a96508) "Could not find any resources appropriate for the specif
ied culture (or the neutral culture) in the given assembly. Make sure "SharpPri
vacyMain.resources" was correctly embedded or linked into assembly "SharpPrivacy
".
baseName: SharpPrivacyMain locationInfo: SharpPrivacy.SharpPrivacyMain resourc
e file name: SharpPrivacyMain.resources assembly: SharpPrivacy, Version=0.1.0.0
, Culture=neutral, PublicKeyToken=null"
_innerException=<null>
_helpURL=<null>
_stackTrace=(0x04a9685c) array with dims=[96]
_stackTraceString=<null>
_remoteStackTraceString=<null>
_remoteStackIndex=0x00000000
_HResult=0x80131532
_source=<null>
_xptrs=0x00000000
_xcode=0xe0434f4d
Thanks in Advance,
Daniel
|
|
|
|
|
Man, I play around with this &$§& for half a day and 5 minutes after I finally post the question, I find the answer:
It has to be
System.Resources.ResourceManager resources = new System.Resources.ResourceManager("SharpPrivacyMain", Assembly.GetExecutingAssembly()); <br />
instead of
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SharpPrivacyMain)); <br />
Could maybe someone explain the difference between those two to me?
Greets,
Daniel
|
|
|
|
|
The first yeilds this:
public ResourceManager(string baseName, Assembly assembly)
{
StackCrawlMark mark1;
base..ctor();
if (baseName == null)
{
throw new ArgumentNullException("baseName");
}
if (assembly == null)
{
throw new ArgumentNullException("assembly");
}
this.MainAssembly = assembly;
this._locationInfo = null;
this.BaseNameField = baseName;
this.CommonSatelliteAssemblyInit();
mark1 = 1;
this._callingAssembly = Assembly.nGetExecutingAssembly(mark1);
if ((assembly == typeof(object).Assembly) && (this._callingAssembly != assembly))
{
this._callingAssembly = null;
}
}
The second:
public ResourceManager(Type resourceSource)
{
StackCrawlMark mark1;
base..ctor();
if (resourceSource == null)
{
throw new ArgumentNullException("resourceSource");
}
this._locationInfo = resourceSource;
this.MainAssembly = this._locationInfo.Assembly;
this.BaseNameField = resourceSource.Name;
this.CommonSatelliteAssemblyInit();
mark1 = 1;
this._callingAssembly = Assembly.nGetExecutingAssembly(mark1);
if ((this.MainAssembly == typeof(object).Assembly) && (this._callingAssembly != this.MainAssembly))
{
this._callingAssembly = null;
}
}
Maybe I'm not reading it right, but both of them look identical. Not quite sure why the second one didn't work...
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
If C# is Microsoft's answer to SUN's Java,
then is C# Platform independent? Is C# more
similar to Jave to C++ or the opposite?
|
|
|
|
|
|
|
|
Thanks for the reply. You said something about parser,
would you mind explaning what is parser? I once read
an article in Java which mentioned something about
parser, I didn't understand at that time.(but neither
do I now)
Thanks.
|
|
|
|
|
Hi FriendS !! How do I draw graphics on the StatusBar ?
Thank you !!
|
|
|
|
|
Set the ShowPanels property to true, then add your panel to the StatusBar .
If you just want to draw an icon, set the Icon property to the icon you want to draw.
If you want to do something more complex, set the Style property on the panel to OwnerDraw then handle the DrawItem event on the StatusBar .
James
"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him
|
|
|
|
|
James Johnson is exactly right. After you create a panel for your status bar, just slap an icon on it. Like this:
//
// m_StatusBarPanel1
//
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.m_StatusBarPanel1.Icon = Icon;
this.m_StatusBarPanel1.Text = "My Application...";
this.m_StatusBarPanel1.Width = 500;
Hope this helps.
|
|
|
|
|
this is my message in the forum iam curently a vb.net developer how could i start with c# is there is a big differance between the two
Have a nice Programming
|
|
|
|
|
In the end it all gets converted to IL, so all that distingushes them is how you write them I prefer C# because it 'feels' better to write. Its far more C/C++-esque. With that Java sent to its syntax.
You get far more street cred for using C# and all the ladies will love you.
"I have a strange ginger man living on my roof!"
"One of the most important things you learn from the internet is that there is no ‘them’ out there. It’s just an awful lot of ‘us’."
-Douglas Adams
Jonathan 'nonny' Newman
Homepage [www.nonny.com] [^]
|
|
|
|
|
HusEgypt wrote:
iam curently a vb.net developer how could i start with c# is there is a big differance between the two
I was/am a VB developer. When .NET came out I decided to try another language, see what all the hype about brace syntax was.
The biggest learning curve was true OO concepts and the .NET Framework itself. There are a ton of classes to learn and remember. Nothing mind bending though.
The switch from VB to C# was pretty easy actually. Syntax is just syntax for 90% of your code. It is only the 10% of hardcore stuff where you are using the specifics of a language that you will have trouble with.
So give it a bash, it really is not hard and you will come to learn that brace syntax rocks
Paul Watson Bluegrass Cape Town, South Africa
Chris Losinger wrote:
i hate needles so much i can't even imagine allowing one near The Little Programmer
|
|
|
|
|
Hello,
I was just wondering if anyone has a good explanation for the differences between static readonly fields and const fields?
The differences I can come up with are:
1. const fields can only be initialized at declaration
2. static readonly can be initialized at declaration or in a static constructor (either directly in the static constructor or through another method called from the static constructor using ref or out )
3. static readonly are changeable at runtime (app start); meaning each time the app is run this field can change
Example:
public class myClass
{
static readonly DateTime m_MyStart = DateTime.Now;
}
4. const fields are hard coded into the MSIL at compile time; meaning an app might use a constant from an assembly and later that assembly might change the constants, but the app doesn't realize the update until the app itself is rebuilt. (Can anyone rephrase this better than that? I suck at trying to describe this...)
Anyone else have differences or corrections to these?
Thanks,
Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
That's pretty much it.
Const fields can only be used for types where there is a literal representation of the type, and the values are "burned into" the code that uses them.
Readonly fields can be used for any type, and the code that uses them references the constructed value at runtime.
|
|
|
|
|
Hi,
What is the best way to save an application and forms position (left & top) and size (width & height) so that when the user opens the app or form, it is displayed the same way it was last opened?
Is there a setting in .NET to accomplish this?
TIA,
Ben
|
|
|
|
|
Isn't that a Windows setting that user has control of ? I'm not sure if you can control it, but if so, it's likely one of the form properties.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
http://weblogs.asp.net/asmith/posts/3799.aspx
|
|
|
|
|
I have three combo boxes on a page. I have two tables, and have a dataset that access both of them. In the users table, I have an id, and an Employee name. The other table, inventory, stores references to these names, as well as supplies information. Now, the problem is, my combo box links to the employeeName, and sets that ID in the inventory table. With one combo box, this works fine. With two or more, what ever value you select in any combo box, will change all the other combo boxes to the same value. The reason for multiple combos, is I have fields in the inventory table, that record an employees name that signs a package in, and then another one for who is using it. Any suggestions? Would multiple dataSets be the solution?
|
|
|
|
|
Easy...
Combo.DataSource = Datatable.Copy();
|
|
|
|
|
This was asked on the DevelopMentor[^] lists not too long ago (02/25/03)... The solution as posted by Bob Beauchemin is to create a new binding context.
Current position in a DataSource is controlled by a CurrencyManager, which is associated with a DataSource/BindingContext pair. An application starts with a BindingContext per Form, controls inherit their parent's BindingContext.
Controls that have the same CurrencyManager (ie, same DataSource and same BindingContext) stay in sync. If you want controls that share the same DataSource NOT to stay in sync, you can allocate additional BindingContexts. This can be done for a group of controls through a common parent (ex. Panel) or on a per-control basis, ie.
dropdown1.DataSource = someds;
dropdown2.DataSource = someds;
// etc, binding other data properties
BindingContext bc = new BindingContext();
// don't want this dropdown synchronized
// if using same DataSource
dropdown2.BindingContext = bc;
Its a little heavy, but much better than making a copy of all of your data.
HTH,
James
"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him
|
|
|
|