P/Invoke Interop Assistant uint type cast question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Sprout

    P/Invoke Interop Assistant uint type cast question

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

    public static extern bool GetDefaultPrint er([OutAttribute()]
    [MarshalAsAttrib ute(UnmanagedTy pe.LPStr)] StringBuilder pszBuffer, ref
    uint pcchBuffer);

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

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

    Thanks,

    Tim Sprout
  • Mark Salsbery [MVP]

    #2
    Re: P/Invoke Interop Assistant uint type cast question

    "Tim Sprout" <tman@ptialaska .netwrote in message
    news:utq6sN0GJH A.1156@TK2MSFTN GP04.phx.gbl...
    The P/Invoke Interop Assistant (http://www.codeplex.com/clrinterop)
    generates a signature for GetDefaultPrint er using an uint type for
    pcchBuffer:
    >
    public static extern bool GetDefaultPrint er([OutAttribute()]
    [MarshalAsAttrib ute(UnmanagedTy pe.LPStr)] StringBuilder pszBuffer, ref
    uint pcchBuffer);
    >
    However, StringBuilder only accepts type int. Is it safe to cast uint to
    int?
    >
    The GetDefaultPrint er 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 GetDefaultPrint er() 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

    Comment

    • Tim Sprout

      #3
      Re: P/Invoke Interop Assistant uint type cast question

      Mark Salsbery [MVP] wrote:
      "Tim Sprout" <tman@ptialaska .netwrote in message
      news:utq6sN0GJH A.1156@TK2MSFTN GP04.phx.gbl...
      >The P/Invoke Interop Assistant (http://www.codeplex.com/clrinterop)
      >generates a signature for GetDefaultPrint er using an uint type for
      >pcchBuffer:
      >>
      >public static extern bool GetDefaultPrint er([OutAttribute()]
      >[MarshalAsAttrib ute(UnmanagedTy pe.LPStr)] StringBuilder pszBuffer, ref
      >uint pcchBuffer);
      >>
      >However, StringBuilder only accepts type int. Is it safe to cast uint
      >to int?
      >>
      >The GetDefaultPrint er 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 GetDefaultPrint er()
      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

      Comment

      Working...