P/Invoke Interop Assistant uint type cast question

T

Tim Sprout

The P/Invoke Interop Assistant (http://www.codeplex.com/clrinterop)
generates a signature for GetDefaultPrinter using an uint type for
pcchBuffer:

public static extern bool GetDefaultPrinter([OutAttribute()]
[MarshalAsAttribute(UnmanagedType.LPStr)] StringBuilder pszBuffer, ref
uint pcchBuffer);

However, StringBuilder only accepts type int. Is it safe to cast uint to
int?

The GetDefaultPrinter signature on pinvoke.net uses an int type for
pcchBuffer, so a cast is not necessary.

Thanks,

Tim Sprout
 
M

Mark Salsbery [MVP]

Tim Sprout said:
The P/Invoke Interop Assistant (http://www.codeplex.com/clrinterop)
generates a signature for GetDefaultPrinter using an uint type for
pcchBuffer:

public static extern bool GetDefaultPrinter([OutAttribute()]
[MarshalAsAttribute(UnmanagedType.LPStr)] StringBuilder pszBuffer, ref
uint pcchBuffer);

However, StringBuilder only accepts type int. Is it safe to cast uint to
int?

The GetDefaultPrinter signature on pinvoke.net uses an int type for
pcchBuffer, so a cast is not necessary.


It doesn't make a difference since an int and a uint are the same size.

Technically, ref uint is the correct type for the GetDefaultPrinter() call,
but if it's easier to use an int then go for it :)

I personally prefer to use the type that will need the least amount of
casting.

Mark
 
T

Tim Sprout

Mark said:
Tim Sprout said:
The P/Invoke Interop Assistant (http://www.codeplex.com/clrinterop)
generates a signature for GetDefaultPrinter using an uint type for
pcchBuffer:

public static extern bool GetDefaultPrinter([OutAttribute()]
[MarshalAsAttribute(UnmanagedType.LPStr)] StringBuilder pszBuffer, ref
uint pcchBuffer);

However, StringBuilder only accepts type int. Is it safe to cast uint
to int?

The GetDefaultPrinter signature on pinvoke.net uses an int type for
pcchBuffer, so a cast is not necessary.


It doesn't make a difference since an int and a uint are the same size.

Technically, ref uint is the correct type for the GetDefaultPrinter()
call, but if it's easier to use an int then go for it :)

I personally prefer to use the type that will need the least amount of
casting.

Mark

Thanks, Mark!

Tim Sprout
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top