Click here to Skip to main content
15,921,660 members
Home / Discussions / C#
   

C#

 
GeneralRe: Call Shell Command Pin
Amauw Scritz2-May-03 20:02
Amauw Scritz2-May-03 20:02 
GeneralRe: Call Shell Command Pin
Amauw Scritz3-May-03 18:56
Amauw Scritz3-May-03 18:56 
GeneralRe: Call Shell Command Pin
Stephane Rodriguez.3-May-03 21:04
Stephane Rodriguez.3-May-03 21:04 
GeneralCalling unmanged DLL Pin
Andy Davey30-Apr-03 14:25
Andy Davey30-Apr-03 14:25 
GeneralRe: Calling unmanged DLL Pin
Andy Davey30-Apr-03 14:50
Andy Davey30-Apr-03 14:50 
GeneralRe: Calling unmanged DLL Pin
James T. Johnson30-Apr-03 15:07
James T. Johnson30-Apr-03 15:07 
GeneralRe: Calling unmanged DLL Pin
Andy Davey30-Apr-03 15:21
Andy Davey30-Apr-03 15:21 
GeneralRe: Calling unmanged DLL Pin
James T. Johnson30-Apr-03 15:42
James T. Johnson30-Apr-03 15:42 
Andy Davey wrote:
I had just discovered this attribute myself but was going to use UnmanagedType.LPStr

I'm not that familiar with interop but to me LPStr means a pointer to a string rather than embedding one within a struct. I think this is also how the Interop Marshaller will interpret it.

Andy Davey wrote:
Could you elaborate on the StringBuilder part?

Sure.

Strings in .NET are considered immutable; this allows for some optimizations to be made in both memory requirements and when comparing strings.

Because they are immutable you can have two strings that contain the same value point to the same block of memory. And rather than compare every single character to see if they are equal you can just compare whether the two strings point to the same object.

That is where string-interning comes in, it takes a string and adds it to an internal table which allows you to make use of those optimizations.

But there is a down side to strings being immutable. When you want to do lots of string operations (such as concatenation) you create a lot of new string objects using up memory that isn't used after the next operation.

That is where the StringBuilder comes in (full name: System.Text.StringBuilder) it maintains an internal buffer/array of characters (similar to C style strings) and does various operations on the array rather than creating new string objects.

When you are done modifying the StringBuilder you can then call .ToString() on it and get out the string that was built.

To link this in with P/Invoke, since you have a buffer that needs a string the proper usage is to supply that buffer with a StringBuilder object rather than a string (which isn't supposed to change).

To change the first struct (going from memory):

[StructLayout(Layout.Sequential, Pack=4, CharSet=CharSet.Ansi)]
struct FOO
{
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=20)]
  public System.Text.StringBuilder szSymbol;
  public uint dwVolume;
}
Then, because structs can't have default constructors you need to initialize szSymbol to szSymbol = new System.Text.StringBuilder(20); To provide a default constructor you can change FOO from a struct to a class (despite its name StructLayout can be applied to classes Smile | :) )

HTH,

James

"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation

GeneralRe: Calling unmanged DLL Pin
leppie1-May-03 7:13
leppie1-May-03 7:13 
GeneralCreating a control from a handle Pin
leppie30-Apr-03 13:21
leppie30-Apr-03 13:21 
GeneralRe: Creating a control from a handle Pin
Stephane Rodriguez.30-Apr-03 19:48
Stephane Rodriguez.30-Apr-03 19:48 
GeneralRe: Creating a control from a handle Pin
leppie1-May-03 7:08
leppie1-May-03 7:08 
GeneralRe: Creating a control from a handle Pin
J. Dunlap1-May-03 7:14
J. Dunlap1-May-03 7:14 
GeneralRe: Creating a control from a handle Pin
Stephane Rodriguez.1-May-03 7:30
Stephane Rodriguez.1-May-03 7:30 
GeneralRe: Creating a control from a handle Pin
leppie1-May-03 8:13
leppie1-May-03 8:13 
QuestionControl?? Pin
Silverdelange30-Apr-03 11:21
sussSilverdelange30-Apr-03 11:21 
AnswerRe: Control?? Pin
Ray Hayes30-Apr-03 12:10
Ray Hayes30-Apr-03 12:10 
GeneralRe: Control?? Pin
Jon Newman2-May-03 1:56
Jon Newman2-May-03 1:56 
GeneralCool tool Pin
Kant30-Apr-03 10:34
Kant30-Apr-03 10:34 
GeneralRe: Cool tool Pin
Stephane Rodriguez.30-Apr-03 10:51
Stephane Rodriguez.30-Apr-03 10:51 
GeneralRe: Cool tool Pin
J. Dunlap30-Apr-03 11:13
J. Dunlap30-Apr-03 11:13 
GeneralModal Forms Pin
Mark Kimball30-Apr-03 10:25
Mark Kimball30-Apr-03 10:25 
GeneralRe: Modal Forms Pin
Stephane Rodriguez.30-Apr-03 11:24
Stephane Rodriguez.30-Apr-03 11:24 
GeneralRe: Modal Forms Pin
RB@Emphasys1-May-03 5:12
RB@Emphasys1-May-03 5:12 
GeneralRe: Modal Forms Pin
Bo Hunter6-May-03 12:43
Bo Hunter6-May-03 12:43 

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.