I recommend reading my PKCS#11 Terminology blog before trying this. In this blog, there is a simple program that gets the information about the HSM slots using the standard PKCS#11 library functions.
In the above program statements, it loads the cryptoki library DLL provided by the HSM/token vendors.
The above statements return the address of the C_GetFunctionList function. It is a standard PKCS#11 library function defined in a header file that is distributed as part of the PKCS#11 specifications.
CK_FUNCTION_LIST_PTR is pointer to a CK_FUNCTION_LIST structure. The CK_FUNCTION_LIST structure contains the function pointers to the standard PKCS#11 cryptoki library functions.
Once you have the address of the C_GetFunctionList function, you can call the function by passing the CK_FUNCTION_LIST_PTR pointer. This CK_FUNCTION_LIST_PTR does not be point to any location before passing it to the C_GetFunctionList function. Once the C_GetFunctionList function returns without any error, the CK_FUNCTION_LIST_PTR starts pointing to the CK_FUNCTION_LIST. Now you can call the PKCS#11 library functions as shown in the below code.
In the above program statements, C_Initialize function initializes the cryptoki library.
The first C_GetSlotList function populates the ulSlotCount with the total number of slots on which token is present by specifying the boolean flag CK_TRUE.
Based on the returned slot counts, the next statement allocates a memory for the slot identifiers. The second C_GetSlotList function populates the slot identifiers array pointed by CK_SLOT_ID_PTR. Finally, it prints the slot identifiers on the console.
In the above code, the C_GetSlotInfo function takes the slot identifier and populates the CK_SLOT_INFO structure if there is no error. The CK_SLOT_INFO structure prints different information about the slot e.g. description, manufacturerID, hardware and firmware version etc.
Finally, the C_Finalize function indicates that the application is done with the crypto processing and clears all the resources. Also it unloads the cryptoki DLL library from the memory.
The above program is tested using the Visual Studio Community Edition 2013 and HSM simulator.