Click here to Skip to main content
16,004,887 members
Home / Discussions / C#
   

C#

 
GeneralRe: zip compression Pin
Anonymous15-Nov-04 12:32
Anonymous15-Nov-04 12:32 
GeneralRe: zip compression Pin
leppie15-Nov-04 22:14
leppie15-Nov-04 22:14 
Generalputting icons in subitems of listview Pin
BlackDice15-Nov-04 7:59
BlackDice15-Nov-04 7:59 
GeneralRe: putting icons in subitems of listview Pin
Heath Stewart15-Nov-04 8:06
protectorHeath Stewart15-Nov-04 8:06 
GeneralGridItem in PropertyGrid Pin
Irie_Pansen15-Nov-04 6:24
Irie_Pansen15-Nov-04 6:24 
GeneralRe: GridItem in PropertyGrid Pin
Heath Stewart15-Nov-04 6:37
protectorHeath Stewart15-Nov-04 6:37 
GeneralAPI Call to GetProfileString Does Not Fill Buffer Pin
stoked15-Nov-04 6:13
stoked15-Nov-04 6:13 
GeneralRe: API Call to GetProfileString Does Not Fill Buffer Pin
Heath Stewart15-Nov-04 6:31
protectorHeath Stewart15-Nov-04 6:31 
First, never allocate a string buffer with spaces, use '\0' for null characters. This ensures that - so long as you don't have any buffer overrun - your string is null-terminated whether the API supports it or not. It's far better to use a StringBuilder however, which also helps ensure this and may increase performance since it is mutable, unlike string.

Because you're declaring GetProfileStringA, you must also specify the DllImportAttribute.ExactSpelling. While not necessary (the default CharSet is CharSet.Ansi, it may also be a good idea to specify CharSet=CharSet.Ansi for better readability, IMO.

This is all not only a waste of time, though, but also a security problem. Because you're P/Invoking to unmanaged code your code requires [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]. There's already a property defined by the BCL (and the BCL assemblies, by default, already have the necessary security permissions to execute) that does this: PrinterSettings.InstallPrinters. This makes the necessary calls and returns you a nice string[] array. Even using WMI is a better alternative to P/Invoking.

Why? Because the BCL assemblies allow partially-trusted assemblies and, since the BCL assemblies have the necessary permissions (by default), actually make the calls that require that permission and your code doesn't need that permission (it would still need the PrintingPermission, however, which is granted, by default, to even the Internet zone in .NET 1.1 (nothing is granted to the Internet zone by default in 1.0).

This also makes for cleaner code. Instead of allocating buffers, parsing strings, and everything that's necessary - and it may work differently, depending on what you're doing, in Windows vs. Windows NT - you enumerating one simple property: PrinterSettings.InstallPrinters.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: API Call to GetProfileString Does Not Fill Buffer Pin
stoked15-Nov-04 7:51
stoked15-Nov-04 7:51 
GeneralRe: API Call to GetProfileString Does Not Fill Buffer Pin
Heath Stewart15-Nov-04 8:02
protectorHeath Stewart15-Nov-04 8:02 
GeneralRe: API Call to GetProfileString Does Not Fill Buffer Pin
stoked15-Nov-04 8:24
stoked15-Nov-04 8:24 
GeneralRe: API Call to GetProfileString Does Not Fill Buffer Pin
Heath Stewart15-Nov-04 9:09
protectorHeath Stewart15-Nov-04 9:09 
GeneralRe: API Call to GetProfileString Does Not Fill Buffer Pin
stoked15-Nov-04 10:51
stoked15-Nov-04 10:51 
GeneralRe: API Call to GetProfileString Does Not Fill Buffer Pin
stoked15-Nov-04 8:03
stoked15-Nov-04 8:03 
General.Net Service & Notifyicon disappearing after log-off/log-on Pin
Steef (Stephan Deckers)15-Nov-04 6:07
Steef (Stephan Deckers)15-Nov-04 6:07 
GeneralRe: .Net Service & Notifyicon disappearing after log-off/log-on Pin
Heath Stewart15-Nov-04 6:45
protectorHeath Stewart15-Nov-04 6:45 
GeneralRe: .Net Service & Notifyicon disappearing after log-off/log-on Pin
Steef (Stephan Deckers)15-Nov-04 8:48
Steef (Stephan Deckers)15-Nov-04 8:48 
GeneralDatetime : Minutes between two date values Pin
Anonymous15-Nov-04 2:42
Anonymous15-Nov-04 2:42 
GeneralRe: Datetime : Minutes between two date values Pin
turbochimp15-Nov-04 3:07
turbochimp15-Nov-04 3:07 
GeneralRe: Datetime : Minutes between two date values Pin
Heath Stewart15-Nov-04 5:50
protectorHeath Stewart15-Nov-04 5:50 
GeneralRe: Datetime : Minutes between two date values Pin
turbochimp15-Nov-04 6:03
turbochimp15-Nov-04 6:03 
GeneralRe: Datetime : Minutes between two date values Pin
Stefan Troschuetz15-Nov-04 3:11
Stefan Troschuetz15-Nov-04 3:11 
GeneralInherited User Control: "System.NullReference" Pin
Jannigje15-Nov-04 2:06
Jannigje15-Nov-04 2:06 
GeneralRe: Inherited User Control: "System.NullReference" Pin
turbochimp15-Nov-04 3:13
turbochimp15-Nov-04 3:13 
GeneralRe: Inherited User Control: "System.NullReference" Pin
Jannigje15-Nov-04 3:42
Jannigje15-Nov-04 3:42 

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.