Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, I have a big problem here. I have to make a C# programm that obtains the DUID for IPv6 number of a computer. I searched all over the internet but could not find a solution for my problem....I can't even know how to view this DUID from Windows....does anyone have an idea?? Thank you for your time!
Posted
Comments
UJimbo 25-Jul-11 7:47am    
Excuse me for asking, but is that supposed to be GUID?

I don't know which internet you searched but Google found this link[^] first time.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jul-11 11:01am    
Ha-ha, "Which Internet do I have to search to obtain DUID of a computer?". My 5.
--SA
Richard MacCutchan 25-Jul-11 11:52am    
I always use the one that has the answers. :)
First of all thanks for the link, i managed to obtain the DUID and the Physical Address modifying that code and it's working....the only problem is that I have to do it in C#, I have to convert it....do you have any solutions ?

#include <stdafx.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>
using namespace std;

#pragma comment(lib, "IPHLPAPI.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

int __cdecl main(int argc, char **argv)
{

    /* Declare and initialize variables */

    DWORD dwSize = 0;
    DWORD dwRetVal = 0;

    unsigned int i = 0;

    // Set the flags to pass to GetAdaptersAddresses
    ULONG flags = GAA_FLAG_INCLUDE_PREFIX;

    // default to unspecified address family (both)
    ULONG family = AF_UNSPEC;

    LPVOID lpMsgBuf = NULL;

    PIP_ADAPTER_ADDRESSES pAddresses = NULL;
    ULONG outBufLen = 0;

    PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
    PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
    PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;
    PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;
    IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
    IP_ADAPTER_PREFIX *pPrefix = NULL;

    family = AF_INET6;

    outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
    pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);

    // Make an initial call to GetAdaptersAddresses to get the 
    // size needed into the outBufLen variable
    if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW) {
        FREE(pAddresses);
        pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
    }

    if (pAddresses == NULL) {
        printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n");
        exit(1);
    }
    // Make a second call to GetAdapters Addresses to get the
    // actual data we want
    printf("Calling GetAdaptersAddresses function with family = ");
    if (family == AF_INET)
        printf("AF_INET\n");
    if (family == AF_INET6)
        printf("AF_INET6\n");
    if (family == AF_UNSPEC)
        printf("AF_UNSPEC\n\n");

    dwRetVal = GetAdaptersAddresses(family, NULL, NULL, pAddresses, &outBufLen);

    if (dwRetVal == NO_ERROR) {
        // If successful, output some information from the data we received
        pCurrAddresses = pAddresses;		
        while (pCurrAddresses) {
			if(pCurrAddresses->Dhcpv6ClientDuidLength){
			if (pCurrAddresses->PhysicalAddressLength != 0) {
                printf("\tPhysical address: ");
                for (i = 0; i < pCurrAddresses->PhysicalAddressLength;
                     i++) {
                    if (i == (pCurrAddresses->PhysicalAddressLength - 1))
                        printf("%.2X\n",(int) pCurrAddresses->PhysicalAddress[i]);
                    else
                        printf("%.2X-",(int) pCurrAddresses->PhysicalAddress[i]);
                }
			}
			
			if (pCurrAddresses->Dhcpv6ClientDuidLength != 0) {
                printf("\tDUID: ");
                for (i = 0; i < (int) pCurrAddresses->Dhcpv6ClientDuidLength;i++) {
                    if (i == (pCurrAddresses->Dhcpv6ClientDuidLength - 1))
                        printf("%.2X\n",(int) pCurrAddresses->Dhcpv6ClientDuid[i]);
                    else
                        printf("%.2X-",(int) pCurrAddresses->Dhcpv6ClientDuid[i]);
                }
            }
			printf("\n");
			}
            pPrefix = pCurrAddresses->FirstPrefix;
            pCurrAddresses = pCurrAddresses->Next;
        }
    } else {
        printf("Call to GetAdaptersAddresses failed with error: %d\n",dwRetVal);
        if (dwRetVal == ERROR_NO_DATA)
            printf("\tNo addresses were found for the requested parameters\n");
        else {

            if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   // Default language
                              (LPTSTR) & lpMsgBuf, 0, NULL)) {
                printf("\tError: %s", lpMsgBuf);
                LocalFree(lpMsgBuf);
                FREE(pAddresses);
                exit(1);
            }
        }
    }
    FREE(pAddresses);
	system ("pause");
    return 0;
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 27-Jul-11 4:37am    
The only way I know is to use P/Invoke, but some of these links may help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900