none
Win32 API from C# PInvoke RRS feed

  • Question

  • I am accessing the Win32 API in my application using C#, however, I'm a little confused on a few things in the documentation. I was hoping to use this thread to clear up a little bit of my confusion. For starters, in the DHCPAPI_PARAMS (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363271%28v=vs.85%29.aspx) struct there is an OptionID where you pass in an unsigned 32-bit integer which will specify what information you want to request. However, on that page there is no information stating what my options are. Is this located somewhere else? I know what information I want from I just need to know what to pass.

    Next up, if you look at the documentation for the DhcpCApiInitialize (http://msdn.microsoft.com/en-us/library/windows/apps/aa363273.aspx) you will find that it also takes in an unsigned 32-bit integer. There is no information on that page either specifying what I need to pass in to tell it the version. Am I missing something on these pages?
    Wednesday, June 26, 2013 5:09 AM

Answers

  • Hi,

    if you take a look at: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363345%28v=vs.85%29.aspx you will see that the example includes the

    dhcpcsdk.h library. Looking into this file you will see:

    #ifndef DHCP_OPTIONS_DEFINED
    #define DHCP_OPTIONS_DEFINED
    #define OPTION_PAD  0
    #define OPTION_SUBNET_MASK  1
    #define OPTION_TIME_OFFSET  2
    #define OPTION_ROUTER_ADDRESS  3
    #define OPTION_TIME_SERVERS  4
    #define OPTION_IEN116_NAME_SERVERS  5
    #define OPTION_DOMAIN_NAME_SERVERS  6
    #define OPTION_LOG_SERVERS  7
    #define OPTION_COOKIE_SERVERS  8
    #define OPTION_LPR_SERVERS  9
    #define OPTION_IMPRESS_SERVERS  10
    #define OPTION_RLP_SERVERS  11
    #define OPTION_HOST_NAME  12
    #define OPTION_BOOT_FILE_SIZE  13
    #define OPTION_MERIT_DUMP_FILE  14
    #define OPTION_DOMAIN_NAME  15
    #define OPTION_SWAP_SERVER  16
    #define OPTION_ROOT_DISK  17
    #define OPTION_EXTENSIONS_PATH  18
    #define OPTION_BE_A_ROUTER  19
    #define OPTION_NON_LOCAL_SOURCE_ROUTING 20
    #define OPTION_POLICY_FILTER_FOR_NLSR  21
    #define OPTION_MAX_REASSEMBLY_SIZE  22
    #define OPTION_DEFAULT_TTL  23
    #define OPTION_PMTU_AGING_TIMEOUT  24
    #define OPTION_PMTU_PLATEAU_TABLE  25
    #define OPTION_MTU  26
    #define OPTION_ALL_SUBNETS_MTU  27
    #define OPTION_BROADCAST_ADDRESS  28
    #define OPTION_PERFORM_MASK_DISCOVERY  29
    #define OPTION_BE_A_MASK_SUPPLIER  30
    #define OPTION_PERFORM_ROUTER_DISCOVERY 31
    #define OPTION_ROUTER_SOLICITATION_ADDR 32
    #define OPTION_STATIC_ROUTES  33
    #define OPTION_TRAILERS  34
    #define OPTION_ARP_CACHE_TIMEOUT  35
    #define OPTION_ETHERNET_ENCAPSULATION  36
    #define OPTION_TTL  37
    #define OPTION_KEEP_ALIVE_INTERVAL  38
    #define OPTION_KEEP_ALIVE_DATA_SIZE  39
    #define OPTION_NETWORK_INFO_SERVICE_DOM 40
    #define OPTION_NETWORK_INFO_SERVERS  41
    #define OPTION_NETWORK_TIME_SERVERS  42
    #define OPTION_VENDOR_SPEC_INFO  43
    #define OPTION_NETBIOS_NAME_SERVER  44
    #define OPTION_NETBIOS_DATAGRAM_SERVER  45
    #define OPTION_NETBIOS_NODE_TYPE  46
    #define OPTION_NETBIOS_SCOPE_OPTION  47
    #define OPTION_XWINDOW_FONT_SERVER  48
    #define OPTION_XWINDOW_DISPLAY_MANAGER  49
    #define OPTION_REQUESTED_ADDRESS  50
    #define OPTION_LEASE_TIME  51
    #define OPTION_OK_TO_OVERLAY  52
    #define OPTION_MESSAGE_TYPE  53
    #define OPTION_SERVER_IDENTIFIER  54
    #define OPTION_PARAMETER_REQUEST_LIST  55
    #define OPTION_MESSAGE  56
    #define OPTION_MESSAGE_LENGTH  57
    #define OPTION_RENEWAL_TIME  58
    #define OPTION_REBIND_TIME  59
    #define OPTION_CLIENT_CLASS_INFO  60
    #define OPTION_CLIENT_ID  61
    #define OPTION_TFTP_SERVER_NAME  66
    #define OPTION_BOOTFILE_NAME  67
    #define OPTION_END  255
    #endif /* DHCP_OPTIONS_DEFINED */

    This isn`t all but related to your first part of the question.

    For the second part: The documentation tells you to pass a

    _Out_  LPDWORD Version

    after sucessfully ( returning ERROR_SUCCESS ) calling the function you will have a pointer to the DHCP Version inside the Version variable.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    My Forum Threads

    • Marked as answer by thughes01 Thursday, July 4, 2013 10:22 PM
    Wednesday, June 26, 2013 5:43 AM

All replies

  • Hi,

    if you take a look at: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363345%28v=vs.85%29.aspx you will see that the example includes the

    dhcpcsdk.h library. Looking into this file you will see:

    #ifndef DHCP_OPTIONS_DEFINED
    #define DHCP_OPTIONS_DEFINED
    #define OPTION_PAD  0
    #define OPTION_SUBNET_MASK  1
    #define OPTION_TIME_OFFSET  2
    #define OPTION_ROUTER_ADDRESS  3
    #define OPTION_TIME_SERVERS  4
    #define OPTION_IEN116_NAME_SERVERS  5
    #define OPTION_DOMAIN_NAME_SERVERS  6
    #define OPTION_LOG_SERVERS  7
    #define OPTION_COOKIE_SERVERS  8
    #define OPTION_LPR_SERVERS  9
    #define OPTION_IMPRESS_SERVERS  10
    #define OPTION_RLP_SERVERS  11
    #define OPTION_HOST_NAME  12
    #define OPTION_BOOT_FILE_SIZE  13
    #define OPTION_MERIT_DUMP_FILE  14
    #define OPTION_DOMAIN_NAME  15
    #define OPTION_SWAP_SERVER  16
    #define OPTION_ROOT_DISK  17
    #define OPTION_EXTENSIONS_PATH  18
    #define OPTION_BE_A_ROUTER  19
    #define OPTION_NON_LOCAL_SOURCE_ROUTING 20
    #define OPTION_POLICY_FILTER_FOR_NLSR  21
    #define OPTION_MAX_REASSEMBLY_SIZE  22
    #define OPTION_DEFAULT_TTL  23
    #define OPTION_PMTU_AGING_TIMEOUT  24
    #define OPTION_PMTU_PLATEAU_TABLE  25
    #define OPTION_MTU  26
    #define OPTION_ALL_SUBNETS_MTU  27
    #define OPTION_BROADCAST_ADDRESS  28
    #define OPTION_PERFORM_MASK_DISCOVERY  29
    #define OPTION_BE_A_MASK_SUPPLIER  30
    #define OPTION_PERFORM_ROUTER_DISCOVERY 31
    #define OPTION_ROUTER_SOLICITATION_ADDR 32
    #define OPTION_STATIC_ROUTES  33
    #define OPTION_TRAILERS  34
    #define OPTION_ARP_CACHE_TIMEOUT  35
    #define OPTION_ETHERNET_ENCAPSULATION  36
    #define OPTION_TTL  37
    #define OPTION_KEEP_ALIVE_INTERVAL  38
    #define OPTION_KEEP_ALIVE_DATA_SIZE  39
    #define OPTION_NETWORK_INFO_SERVICE_DOM 40
    #define OPTION_NETWORK_INFO_SERVERS  41
    #define OPTION_NETWORK_TIME_SERVERS  42
    #define OPTION_VENDOR_SPEC_INFO  43
    #define OPTION_NETBIOS_NAME_SERVER  44
    #define OPTION_NETBIOS_DATAGRAM_SERVER  45
    #define OPTION_NETBIOS_NODE_TYPE  46
    #define OPTION_NETBIOS_SCOPE_OPTION  47
    #define OPTION_XWINDOW_FONT_SERVER  48
    #define OPTION_XWINDOW_DISPLAY_MANAGER  49
    #define OPTION_REQUESTED_ADDRESS  50
    #define OPTION_LEASE_TIME  51
    #define OPTION_OK_TO_OVERLAY  52
    #define OPTION_MESSAGE_TYPE  53
    #define OPTION_SERVER_IDENTIFIER  54
    #define OPTION_PARAMETER_REQUEST_LIST  55
    #define OPTION_MESSAGE  56
    #define OPTION_MESSAGE_LENGTH  57
    #define OPTION_RENEWAL_TIME  58
    #define OPTION_REBIND_TIME  59
    #define OPTION_CLIENT_CLASS_INFO  60
    #define OPTION_CLIENT_ID  61
    #define OPTION_TFTP_SERVER_NAME  66
    #define OPTION_BOOTFILE_NAME  67
    #define OPTION_END  255
    #endif /* DHCP_OPTIONS_DEFINED */

    This isn`t all but related to your first part of the question.

    For the second part: The documentation tells you to pass a

    _Out_  LPDWORD Version

    after sucessfully ( returning ERROR_SUCCESS ) calling the function you will have a pointer to the DHCP Version inside the Version variable.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    My Forum Threads

    • Marked as answer by thughes01 Thursday, July 4, 2013 10:22 PM
    Wednesday, June 26, 2013 5:43 AM
  • Thank you. Though I would love to know how you looked into the file once you knew what file to look for.

    So I just need to create a variable and pass it's pointer to DhcpCApiInitialize then from that I should get either ERROR_SUCCESS or its failed equivalent?

    Wednesday, June 26, 2013 12:01 PM
  • Thank you. Though I would love to know how you looked into the file once you knew what file to look for.

    I just did a quick search for dhcpcsdk.h and found it.

    So I just need to create a variable and pass it's pointer to DhcpCApiInitialize then from that I should get either ERROR_SUCCESS or its failed equivalent?

    Should work this way.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    My Forum Threads

    Wednesday, June 26, 2013 12:06 PM
  • What I ended up doing with the OptionId was this:

    public enum OptionID : uint
        {
            OPTION_PAD = 0,
            OPTION_SUBNET_MASK = 1,
            OPTION_TIME_OFFSET = 2,
            OPTION_ROUTER_ADDRESS = 3,
            OPTION_TIME_SERVERS = 4,
            OPTION_IEN116_NAME_SERVERS = 5,
            OPTION_DOMAIN_NAME_SERVERS = 6,
            OPTION_LOG_SERVERS = 7,
            OPTION_COOKIE_SERVERS = 8,
            OPTION_LPR_SERVERS = 9,
            OPTION_IMPRESS_SERVERS = 10,
            OPTION_RLP_SERVERS = 11,
            OPTION_HOST_NAME = 12,
            OPTION_BOOT_FILE_SIZE = 13,
            OPTION_MERIT_DUMP_FILE = 14,
            OPTION_DOMAIN_NAME = 15,
            OPTION_SWAP_SERVER = 16,
            OPTION_ROOT_DISK = 17,
            OPTION_EXTENSIONS_PATH = 18,
            OPTION_BE_A_ROUTER = 19,
            OPTION_NON_LOCAL_SOURCE_ROUTING = 20,
            OPTION_POLICY_FILTER_FOR_NLSR = 21,
            OPTION_MAX_REASSEMBLY_SIZE = 22,
            OPTION_DEFAULT_TTL = 23,
            OPTION_PMTU_AGING_TIMEOUT = 24,
            OPTION_PMTU_PLATEAU_TABLE = 25,
            OPTION_MTU = 26,
            OPTION_ALL_SUBNETS_MTU = 27,
            OPTION_BROADCAST_ADDRESS = 28,
            OPTION_PERFORM_MASK_DISCOVERY = 29,
            OPTION_BE_A_MASK_SUPPLIER = 30,
            OPTION_PERFORM_ROUTER_DISCOVERY = 31,
            OPTION_ROUTER_SOLICITATION_ADDR = 32,
            OPTION_STATIC_ROUTES = 33,
            OPTION_TRAILERS = 34,
            OPTION_ARP_CACHE_TIMEOUT = 35,
            OPTION_ETHERNET_ENCAPSULATION = 36,
            OPTION_TTL = 37,
            OPTION_KEEP_ALIVE_INTERVAL = 38,
            OPTION_KEEP_ALIVE_DATA_SIZE = 39,
            OPTION_NETWORK_INFO_SERVICE_DOM = 40,
            OPTION_NETWORK_INFO_SERVERS = 41,
            OPTION_NETWORK_TIME_SERVERS = 42,
            OPTION_VENDOR_SPEC_INFO = 43,
            OPTION_NETBIOS_NAME_SERVER = 44,
            OPTION_NETBIOS_DATAGRAM_SERVER = 45,
            OPTION_NETBIOS_NODE_TYPE = 46,
            OPTION_NETBIOS_SCOPE_OPTION = 47,
            OPTION_XWINDOW_FONT_SERVER = 48,
            OPTION_XWINDOW_DISPLAY_MANAGER = 49,
            OPTION_REQUESTED_ADDRESS = 50,
            OPTION_LEASE_TIME = 51,
            OPTION_OK_TO_OVERLAY = 52,
            OPTION_MESSAGE_TYPE = 53,
            OPTION_SERVER_IDENTIFIER = 54,
            OPTION_PARAMETER_REQUEST_LIST = 55,
            OPTION_MESSAGE = 56,
            OPTION_MESSAGE_LENGTH = 57,
            OPTION_RENEWAL_TIME = 58,
            OPTION_REBIND_TIME = 59,
            OPTION_CLIENT_CLASS_INFO = 60,
            OPTION_CLIENT_ID = 61,
            OPTION_TFTP_SERVER_NAME = 66,
            OPTION_BOOTFILE_NAME = 67,
            OPTION_END = 255
        }

    Then I went back up into the part of my code where I was setting the OptionId and I set it this way:

    parToSend.OptionId = OptionID.OPTION_SUBNET_MASK;

    However, this causes me to have an error as it does not want to convert OptionID to an uint even though I'm specificy that the enum will be of the the type uint. Did I forget something when I was setting up this OptionId?

    Wednesday, June 26, 2013 10:36 PM
  • Unfortunately, you need to cast the enum value like:

    parToSend.OptionId = (uint)OptionID.OPTION_SUBNET_MASK;


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    My Forum Threads

    Thursday, June 27, 2013 9:05 AM
  • Hmm, I had originally tried that but it gave me another error. Now it's letting me do that with no errors. Odd.

    • Edited by thughes01 Thursday, July 4, 2013 10:22 PM Fixed post.
    Saturday, June 29, 2013 9:19 PM