Click here to Skip to main content
16,016,557 members
Home / Discussions / C#
   

C#

 
Questionasp.net script timeout? Pin
lelelele8-Jun-04 5:13
lelelele8-Jun-04 5:13 
AnswerRe: asp.net script timeout? Pin
Heath Stewart8-Jun-04 6:00
protectorHeath Stewart8-Jun-04 6:00 
GeneralCopy A Fixed String Buffer Into A Struct Pin
dgiljr8-Jun-04 5:05
dgiljr8-Jun-04 5:05 
GeneralRe: Copy A Fixed String Buffer Into A Struct Pin
Heath Stewart8-Jun-04 5:53
protectorHeath Stewart8-Jun-04 5:53 
GeneralRe: Copy A Fixed String Buffer Into A Struct Pin
dgiljr8-Jun-04 6:24
dgiljr8-Jun-04 6:24 
GeneralRe: Copy A Fixed String Buffer Into A Struct Pin
Heath Stewart8-Jun-04 6:31
protectorHeath Stewart8-Jun-04 6:31 
GeneralRe: Copy A Fixed String Buffer Into A Struct Pin
dgiljr8-Jun-04 7:01
dgiljr8-Jun-04 7:01 
GeneralRe: Copy A Fixed String Buffer Into A Struct Pin
Heath Stewart8-Jun-04 8:17
protectorHeath Stewart8-Jun-04 8:17 
Again, you don't want to use FieldOffsetAttribute, because that would cause you to have to change the offset for each field. If you read the documentation, it is the offset from the beginning of the structure - not the last field.

AS I said, though, this can still be done using the Marshal class. See the Marshal.PtrToStructure method, for example. To get the IntPtr for the string, use GCHandle.Alloc.

There's one problem you're also overlooking: in VB6 and below, strings are not null terminated. In most Windows APIs - including the .NET Framework - strings are null terminated.

So, lets say you use UnmanagedType.ByValTStr that I mentioned before. While Marshal.PtrToStructure would honor the StructLayoutAttribute, the problem is that the character sequences in your string will not be delimited with null characters. You need to write a work-around for this, or implement an ICustomMarshaler and implement the work-around.

Stil, though, nothing says that simply parsing the string has to be difficult. When you read in a substring, use the index and get the next substring starting from that index. This still allows you to change one line without changing others.

Consider the following example:
public struct Phone
{
  public int CountryCode;
  public int AreaCode; // It's better to use properties, but this is an example
  public string Number;
  public static Phone Parse(string value)
  {
    // Assuming it's the right format.
    Phone p = new Phone();
    int index = 0;
    p.CountryCode = int.Parse(value.Substring(0, 1); index += 1;
    p.AreaCode = int.Parse(value.Substring(index, 3); index += 3;
    p.Number = value.Substring(index, 7);
  }
}
See? All relative. You just need to change the length of the string to grab with String.Substring plus the index addition/assignment right operand.

To recap, though, the reason what you had worked in VB6 is because strings are null-terminated, allow for a sequence of characters to be packed in a struct. While conceptually the same is possible, it's the null termination that will cause you a problem.

With some fanagling, you could get it to work. See the .NET Framework SDK documentation for the Marshal class for lots of helpful methods.

And remember, the FieldOffsetAttribute won't work for what you want because it'll force you to go through and chance all the preceeding offsets if you change one offset since they are the offset from the beginning of the struct (offset 0, of course).

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Copy A Fixed String Buffer Into A Struct Pin
dgiljr8-Jun-04 10:32
dgiljr8-Jun-04 10:32 
Generaltextbox control with multi colored rows Pin
gmahesh8-Jun-04 2:05
gmahesh8-Jun-04 2:05 
GeneralRe: textbox control with multi colored rows Pin
Aryadip8-Jun-04 2:48
Aryadip8-Jun-04 2:48 
GeneralRe: textbox control with multi colored rows Pin
gmahesh8-Jun-04 2:57
gmahesh8-Jun-04 2:57 
GeneralRe: textbox control with multi colored rows Pin
Aaron Eldreth8-Jun-04 3:47
Aaron Eldreth8-Jun-04 3:47 
GeneralRe: textbox control with multi colored rows Pin
gmahesh9-Jun-04 0:58
gmahesh9-Jun-04 0:58 
QuestionLoad file into string? Pin
Dominik Reichl8-Jun-04 1:43
Dominik Reichl8-Jun-04 1:43 
AnswerRe: Load file into string? Pin
Werdna8-Jun-04 3:27
Werdna8-Jun-04 3:27 
GeneralRe: Load file into string? Pin
Dominik Reichl8-Jun-04 4:14
Dominik Reichl8-Jun-04 4:14 
AnswerRe: Load file into string? Pin
Heath Stewart8-Jun-04 4:00
protectorHeath Stewart8-Jun-04 4:00 
GeneralRe: Load file into string? Pin
Dominik Reichl8-Jun-04 4:19
Dominik Reichl8-Jun-04 4:19 
GeneralRe: Load file into string? Pin
Heath Stewart8-Jun-04 4:22
protectorHeath Stewart8-Jun-04 4:22 
GeneralRe: Draw Shape To Screen Pin
SJ_Phoenix8-Jun-04 0:24
SJ_Phoenix8-Jun-04 0:24 
GeneralRe: Draw Shape To Screen [EDITED] Pin
Dave Kreskowiak8-Jun-04 3:17
mveDave Kreskowiak8-Jun-04 3:17 
GeneralConverting an array list to double array Pin
_Searcher_8-Jun-04 0:01
_Searcher_8-Jun-04 0:01 
GeneralRe: Converting an array list to double array Pin
Aryadip8-Jun-04 2:08
Aryadip8-Jun-04 2:08 
GeneralRe: Converting an array list to double array Pin
_Searcher_8-Jun-04 3:59
_Searcher_8-Jun-04 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.