Click here to Skip to main content
15,888,341 members
Home / Discussions / C#
   

C#

 
GeneralRe: Period key in numpad Pin
Heath Stewart8-Jun-04 5:45
protectorHeath Stewart8-Jun-04 5:45 
GeneralRe: Period key in numpad Pin
machocr8-Jun-04 6:37
machocr8-Jun-04 6:37 
Questionwindows that don't receive focus?? Pin
jremignanti8-Jun-04 5:32
jremignanti8-Jun-04 5:32 
AnswerRe: windows that don't receive focus?? Pin
Heath Stewart8-Jun-04 6:06
protectorHeath Stewart8-Jun-04 6:06 
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 
dgiljr wrote:
Does anyone know how to copy a fixed string buffer directly into a struct in 50 lines or less?

Why don't you tell us what you're trying to accomplish? Do you need to marshal this struct to unmanaged code, or just parse it? If you need to marshal it, then you also need to declare the character encoding. A field length of 4 bytes for each field is only 2 characters using the native Unicode encoding of .NET (strings are stored and manipulated using Unicode). You should also use the MarshalAs(UnmanagedType.ByValTStr), and don't forget to set the CharSet field on the StructLayoutAttribute accordingly. Again, though, 4 bytes still isn't enough.

Now, to marshal this struct to unmanaged code as string reference, what you have would work on a 32-bit processor. Typically, you want to use LayoutKind.Sequential and make sure your fields are declared properly (like unmanaged long is actually a managed int - both 32 bits). Then, on a 64-bit OS (when .NET 2.0 is released that supports 64-bit processors) the string references would actually be 64-bits (the size of a handle, or unsigned int).

If you just need to parse a string, either use the String class methods (like Substring) or use a StringReader (see the StringReader.Read(char[], int, int) method in teh .NET Framework SDK). There's other ways, as well; after all, this is just simply string parsing.

 

Microsoft MVP, Visual C#
My Articles
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 
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 

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.