-7

I am trying to get MAC address on my Windows PC but I have been facing an error when trying to convert it to string.

This is my code:

string UniqueKeyGenerator::getMacAddress () {
 try {
 IP_ADAPTER_INFO adapterInfo[16];
 DWORD bufLen = sizeof(adapterInfo);
 string adapter;
 string mac_address;
 // Get the adapter info
 DWORD dwStatus = GetAdaptersInfo(adapterInfo, &bufLen);
 if (dwStatus != ERROR_SUCCESS) {
 std::cerr << "GetAdaptersInfo failed with error " << dwStatus << std::endl;
 return "";
 }
 // Iterate over the adapters and display the MAC addresses
 PIP_ADAPTER_INFO pAdapterInfo = adapterInfo;
 while (pAdapterInfo) {
 adapter = pAdapterInfo->AdapterName;
 std::cout << "Adapter: " << adapter << std::endl;
 // Skip empty MAC addresses
 if (pAdapterInfo->Address[0] == 0) {
 std::cerr << "Empty MAC address for adapter: " << adapter << std::endl;
 } else {
 mac_address = macAddressToString(pAdapterInfo->Address);
 std::cout << "MAC Address: " << mac_address << std::endl;
 return mac_address; // Return the first MAC address found
 }
 pAdapterInfo = pAdapterInfo->Next;
 }
 return "Mac address not found";
 
 } catch (const exception& e) {
 cerr<<"Exception in: "<< e.what() <<endl;
 return e.what();
 }
 
 }
 std::string macAddressToString(const BYTE* macAddr) {
 char macStr[18];
 snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", 
 macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
 return std::string(macStr); 
 }

The error occurs at line:

mac_address = macAddressToString(pAdapterInfo->Address);

that function returns only "".

This is the error:

FAILED: lv.exe 
C:\WINDOWS\system32\cmd.exe /C "cd . && C:\PROGRA~1\LLVM\bin\CLANG_~1.EXE -nostartfiles -nostdlib -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -Xlinker /subsystem:cons
ole -fuse-ld=lld-link CMakeFiles/lv.dir/main.cpp.obj -o lv.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:lv.lib -Xlinker /pdb:lv.pdb -Xlinker /version:0.0 LicenseVerifier.lib -lkernel32 -luser32 -lg
di32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames && cd ."
lld-link: error: undefined symbol: private: class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> __cdecl LicenseVerifier::UniqueKeyGenerator::macAddressToString(unsig
ned char const *)
>>> referenced by C:\Users\Do Duy Manh\Desktop\manh\tasks\license_verifier_c_cpp\license_verifier_c_cpp\src\UniqueKeyGenerator.cpp:103
>>> CMakeFiles/lv.dir/main.cpp.obj:(private: class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> __cdecl LicenseVerifier::UniqueKeyGenerator::getMacAdd
ress(void))
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
gre_gor
6,65812 gold badges105 silver badges106 bronze badges
asked Feb 11, 2025 at 3:49
8
  • 1
    Side node : If all you need is some unique number, you might want to have a look at : stackoverflow.com/questions/1327157/…. That mechanism stopped using MAC addresses because it left traces in the generated key. Commented Feb 11, 2025 at 3:55
  • Thank you for the link. However, I just want to return the MAC address as a string, and I don't understand why the error occurs at the line where the function is invoked: mac_address = macAddressToString(pAdapterInfo->Address); Commented Feb 11, 2025 at 4:28
  • 2
    Have you tried debugging yet? And checked that all your pointer and return values are what you would expect? pAdapterInfo = pAdapterInfo->Next; this looks suspicious, since you only have room in your buffer for one entry DWORD bufLen = sizeof(adapterInfo); not 16 Commented Feb 11, 2025 at 4:41
  • 2
    If you're seeing an error message, post the complete text of the message in your question. Many things could be wrong here, but you're making us guess. Commented Feb 11, 2025 at 4:48
  • 1
    You are ;facing an error' when compiling your code. Commented Feb 11, 2025 at 5:58

1 Answer 1

1

You are getting a linker error. Your linker cant find the implementation code for the function you are trying to call.

The error says it can't find UniqueKeyGenerator::macAddressToString() but the implementation of macAddressToString() you have shown is not a member of UniqueKeyGenerator.

It seems that you likely declared macAddressToString() as a member of UniqueKeyGenerator, but you did not define it as a member, like you did with getMacAddress(). Try adding that qualification to your function's definition:

 std::string UniqueKeyGenerator::macAddressToString(const BYTE* macAddr) {
 //^^^^^^^^^^^^^^^^^^^^
 char macStr[18];
 snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", 
 macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
 return std::string(macStr); 
 }
answered Feb 11, 2025 at 6:02
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. I just switched from Python to C++ for a few day and try to get familiar with it
@DDManh afraid it'll take more than a few days. C++ covers a lot of ground and is very picky.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.