473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

P/Invoke Interop Assistant uint type cast question

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
Sep 20 '08 #1
2 2127
"Tim Sprout" <tm**@ptialaska.netwrote in message
news:ut**************@TK2MSFTNGP04.phx.gbl...
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

--
Mark Salsbery
Microsoft MVP - Visual C++
>
Thanks,

Tim Sprout
Sep 20 '08 #2
Mark Salsbery [MVP] wrote:
"Tim Sprout" <tm**@ptialaska.netwrote in message
news:ut**************@TK2MSFTNGP04.phx.gbl...
>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
Sep 21 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
5
by: Duncan Mole | last post by:
Hi all, Does anyone know of a way to achieve multi-theaded platform invokes? I have been taking quite a performance hit using the syncronised attribute: . Cheers ---
1
by: Xia Wei | last post by:
Hi group, I'v got something wrong with P/Invoke. If I have two structs defined like this: typedef struct{ DWORD F1; }A, *PA; typedef struct{ A Data;
3
by: Charles Denny | last post by:
I'm trying to invoke CertFindCertificateInStore to find all certificates that have the Code Signing enhanced key attribute. I'd just like to find 1 cert (not even all at this point), however, I...
3
by: Charles Denny | last post by:
I'm trying to call CertFindCertificateInStore to find all certificates in the store that have the Code Signing enhanced key usage. I'm running into problems marshalling the array of OIDs in...
6
by: Nataraj1978 | last post by:
I have a requirement to use a dll written in Visual C++ 6.0 in my C# application. In order to establish the link between my application and the dll, I have written a ATL COM Component in Visual...
2
by: PickwickBob3 | last post by:
public class RAWINPUTDEVICELIST { public IntPtr Device; public int Type; } public static extern uint GetRawInputDeviceList( RAWINPUTDEVICELIST pRawInputDeviceList, ref uint...
2
by: Bob Gray | last post by:
Hi, Can anyone explain how P/Invoke is able to cast the 32-bit HANDLE value returned from CreateFile (for example) to a SafeFileHandle object? Have a look at this code snippet from the .NET...
2
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
I am trying to find a good example of SendInput. Doing a search on google I found two, one is incomplete and vague from the start and the other I copied the code and tried running it and it was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.