|
IS there a way of converting a struct to an byte array.
My struct has two fields an int in the form of an enum and a string
thestruct
{
enum_Do_something; // goes to a switch case with a list of commans.
string command; // what to do
}
is there a way to just convert this to an byte array? or should i just send each field. This is for a server/client apps.
modified 16-May-21 21:01pm.
|
|
|
|
|
If both the client and server are .NET applications, you could always use a BinaryFormatter (or any formatter, using the proper Encoding class if using text I/O) which would be much easier. You could just write to the NetworkStream for the TcpClient , for example, using this method.
One way is a bit of a hack, but should work:
int size = Marshal.SizeOf(typeof(thestruct));
IntPtr ptr = Marshal.AllocHGlobal(int);
byte[] buffer = new byte[size];
Marshal.Copy(ptr, buffer, 0, size);
Marshal.FreeHGlobal(ptr);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
My connection is between a OC and a pocket PC, so im using the .Net Compact framework, i should have posted that.
Well then i got the thinking, pulling weird stuff out of my head, "what if i cast the struct to an object and send the object.ToSting() ?, no that will never work"
But i still tried it, so i just threw something together:
aStruct qwerty = new aStruct( 35, "Football");
object wed = (object) qwerty;
aStruct ytrewq = ( aStruct ) wed;
Console.WriteLine( ytrewq.aInt.ToString() );
out of dumb luck it worked, strange. Well is this way bad? i havent tried to send it yet (ByteArrayData = Encoding.ASCII.GetBytes( wed.ToString()), but it worked.
modified 16-May-21 21:01pm.
|
|
|
|
|
i go ahead of myself ( i do that alot) yea the above code works but not the .ToString i should have known that
modified 16-May-21 21:01pm.
|
|
|
|
|
No, that's not a good way because you can't as easily parse the string back into a struct. ToString overrides are meant to represent an instance of a class or struct. Some classes won't output near enough information to reconstitute it, and any struct that overrides ToString (I do it quite a bit) won't have enough information either.
Since you're using the .NET CF (which would've been nice to know before writing all that stuff that isn't supported in it ), you're definitely in a bind - but not an impossible solution (I don't know the meaning of it...just improbable solutions ).
The best way is to do basically what a formatter would do - use reflection to get the public and private fields of a structure. It you don't have any nested compex types, this is pretty much all you'd have to do. Then, for each type check if it has a TypeConverter using TypeDescriptor.GetConverter and call ConvertToString on that. That will often give a more accurate representation as a string, and ConvertFromString can reconstitute the member Type. Be careful, though - while most types you'd typically find in a struct have a TypeConverter , many types throughout the .NET Framework don't.
See the TypeConverter and TypeDescriptor class documentation in the .NET Framework SDK for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi!
I wanted to find out if we can control the 'min occurs' and 'max occurs' values in a the wsdl that is generated automatically when ?WSDL is used. I am wondering if there is some attribute that we could use with the webservice method code itself.
Thanks!
|
|
|
|
|
If you're looking to change the WSDL that is generated, see the System.Web.Services.Description namespace in the .NET Framework SDK. There are ways to modify the WSDL, but unless the client understands what to make of those changes, it really doesn't matter. The other option is to write-out the WSDL yourself, which isn't unheard of (several articles on MSDN deal with this). As an alternative, you could grab the auto-generated WSDL once and modify it.
As far as attribute parameters in either of the System.Web.Services sub-namespaces or the System.Xml.Serialization namespace, there's none that I've seen or that are documented.
You might check out the Web Service Enchancements from Microsoft at http://msdn.microsoft.com/webservices/building/wse/default.aspx[^]. It implements several of the Web Services specs and recommendations and may include what you're looking for. I've really only dealt with the WS-Encryption and WS-Signature portions of it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Is there a tutorial for all the C# keywords in a way that a n00b can understand? I think MSDN is little confusing and not much example on each keywords.
Also, I heard that using C# with OpenGL is easier to learn and faster than Managed DirectX, is there a good online resource site that contains C# with OpenGL tutorials for n00bs?
http://www.shintasoft.com
Bring you English Version Super Robot Wars games
|
|
|
|
|
|
As Heath has pointed out to you, MSDN is the place you need to visit for keyword docs.
Regarding OpenGL: unless there is some kind of managed interface, you are going to be better off with Managed DirectX as a noob.
HTH
R.Bischoff .NET, Kommst du mit?
Great Freeware -> Abilon - Rss Reader | Zip Genius | SmartFTP
|
|
|
|
|
there is opengl for C#, it's called Tao Framework, i don't like msdn, it's not n00b friendly ^_^;
i need a n00b source for the C# keywords tutorials by examples X_x;;; don't tell me msdn cuz i already know where to find it >_<
http://www.shintasoft.com
Bring you English Version Super Robot Wars games
|
|
|
|
|
Well, it's the most complete documentation on C#. Other then that, try searching Amazon for beginning C# books.
RageInTheMachine9532
|
|
|
|
|
Hi, I have CCW written and I want to make it to be portable( to be running on different PC without need to recompile). I need to make some kind of installation program to registry this COM when it's brought on new PC.
Question: 1. is it possible?
2. if it is possible - is there any code sample or tutorial for it?
Thanks
|
|
|
|
|
Yes it is possible. Your installer should run "regasm.exe /codebase assembly.dll" on the target machine when the assembly has been copied over. This assembly can be found in the "%WINDIR%\Microsoft.NET\Framework\v1.x.xxxx" directory of the target machine, if .NET is installed. Replace "x.xxxx" with the version you want to target, or detect the latest version of .NET.
How you do it depends on what installer you use. You could use the Windows Installer project in VS.NET to do this. This allows you to also get the path of the file easily what it will be when copied to the target machine.
On a side note, make sure you followed good COM guidelines, like using fixed GUIDs using the GuidAttribute for your interfaces and classes, and never use auto-generated class interfaces. Also specify ClassInterfaceType.None on your class, explicitly define your class interface, and implement that as the first interfaces. Microsoft doesn't do this in their samples in the .NET Framework SDK, but they do recommend it elsewhere and as someone with many years of COM experience I can tell you that you must follow good COM guidelines or you'll run into problems, such as never changing a released interface. Always derive a new one, append "2", "3", and so on for each new version (just like you'd see in various API documentation on MSDN).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Is there an easy way to delete the (old) last lines of a multiline TextBox? Cos now I have to set the MaxLength to a huge number...
Thanks!!!
|
|
|
|
|
I'm still not all that familiar with Windows Forms, although I've studied it. Can't you do something like this? (Warning: poorly-designed code)
public static void RemoveTopLines(TextBox textBox, int linesToRemove) {
if (textBox == null) {
throw new ArgumentNullException();
}
else if (linesToRemove < 0) {
throw new ArgumentException("Invalid line count (" + linesToRemove + ")");
}
string[] lines = textBox.Lines;
int currentLineCount = lines.Length;
int newLineCount = lines.Length - linesToRemove;
if (newLineCount < 0) {
newLineCount = 0;
}
string[] newLines = new string[newLineCount];
if (newLineCount > 0) {
Array.Copy(lines, linesToRemove, newLines, 0, newLineCount);
}
textBox.Lines = newLines;
}
|
|
|
|
|
Jeff Varszegi wrote:
Warning: poorly-designed code
I beg to differ This is most likely the best way to make large changes to lines.
|
|
|
|
|
Thanks! I mostly mean that I didn't take the time to think about synchronization issues, and I didn't spend the normal half hour agonizing over every parameter name, etc. I also didn't test whether it'd be faster just getting the text as a string, and calling IndexOf repeatedly to skip past the indicated number of new-lines in the string; that might be faster, but I didn't have the time.
|
|
|
|
|
I am must be a total moron, but I can figure out how debugging works. I set the breakpoints and everything, but I can't find a window that tells me what variable equals what. I am using Visual Studion C#
Thanks
|
|
|
|
|
As a test, hit F10. Do you see a "Locals" window pop up anywhere? If not, hit "Debug - Windows - Locals". Now what do you see?
|
|
|
|
|
Also, is your build configuration set to 'Release' or 'Debug'?
|
|
|
|
|
|
Hi, I'm using the following code to get the Machine name in an app
System.Environment.MachineName
On most computers this works fine but on one of our testers computers (machine is a windows 2000 desktop pc, not a networking share either), it's throwing the following exception.
Request for the permission of type System.Security.Permissions.EnvironmentPermission, mscorlib, Version = 1.0.50000.0, Culture = neutral, publicKeyToken = blahblahblah failed.
I have no idea what is causing this. I tried messing around with the .Net Configuration. Figured mscorlib didn't have permission rights.
also.........
I can't get any computer information in general. Trying to do some WMI core queries as well from code and getting a "Security Error" Exception. Below is the code that gets the disk size.
public string GetsDiskSize(){
ManagementClass cimobject;
ManagementObjectCollection moc;
cimobject = new ManagementClass("Win32_DiskDrive");
moc = cimobject.GetInstances();
try { // BIOS INFO
foreach(ManagementObject mo in moc){
this.m_sDiskSize = GetSystemValue("Size",mo);
mo.Dispose();
break;
}
}
catch(Exception ex){
System.Windows.Forms.MessageBox.Show("GetsDiskSize " + ex.Message);
cimobject.Dispose();
moc.Dispose();
return ex.Message;
}
cimobject.Dispose();
moc.Dispose();
return this.m_sDiskSize;
}
Thx for your time,
Paul
|
|
|
|
|
What are the permissions of the person running the program (your tester) on the machine?
|
|
|
|
|
User is logged in as an admin. Not sure what you mean by permission?
|
|
|
|