Thread: passing nullable type to a P/invoked function

  1. #1
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    passing nullable type to a P/invoked function

    Does anyone know of a way I can pass a nullable type to a p/invoked function? specifically I need parameter 1 of:
    Code:
    [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, int? enumerator, IntPtr hwndParent, uint Flags);
    to be passable so I can avoid have to overload the function.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    SetupDiGetClassDevs

    From looking at that I'd use the signature below and pass IntPtr.Zero for NULL.

    Code:
    [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SetupDiGetClassDevs(           // 1st form using a ClassGUID
       ref Guid ClassGuid,
       IntPtr Enumerator,
       IntPtr hwndParent,
       int Flags
    );

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Quote Originally Posted by valaris View Post
    SetupDiGetClassDevs

    From looking at that I'd use the signature below and pass IntPtr.Zero for NULL.

    Code:
    [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SetupDiGetClassDevs(           // 1st form using a ClassGUID
       ref Guid ClassGuid,
       IntPtr Enumerator,
       IntPtr hwndParent,
       int Flags
    );
    I meant parameter 1 of the function. not the enumerator the ref Guid ClassGuid variable. Also I should say I changed the enumerator to string type variable. The original posting of the function was a typo.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    A Guid can't be null, but you can also use this second signature:

    Code:
    [DllImport("setupapi.dll", CharSet = CharSet.Auto)] 
    static extern IntPtr SetupDiGetClassDevs(
       IntPtr ClassGuid,
       string Enumerator,
       IntPtr hwndParent,
       int Flags
    );
    Code:
    SetupDiGetClassDevs(IntPtr.Zero, ...........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM