0

I wanted to get gpu name, and found the code that does this using IOKit. The only problem is that this code was in Objective-C, while my project is in C, and I don't know how to use C-string instead of NSString.

const void *GPUModel = CFDictionaryGetValue(serviceDictionary, CFSTR("model"));

            if (GPUModel != NULL) {
                if (CFGetTypeID(GPUModel) == CFDataGetTypeID()) {
                    //(Original Comment) Create a string from the CFDataRef.
                    //NSString *modelName = [[NSString alloc] initWithData:
                    //                       (NSData *)GPUModel encoding:NSASCIIStringEncoding];

                }
            }

  • 2
    Looks like `GPUModel` is `CFDataRef`. There is all C functions you need https://developer.apple.com/documentation/corefoundation/cfdata?language=objc – Cy-4AH Aug 30 '21 at 15:45
  • 1
    The specific routine you want is CFDataGetBytePtr. Note that it likely won't be NULL-terminated, so you'll probably need to copy the values into another buffer to add the NULL. – Rob Napier Aug 30 '21 at 15:54
  • Thanks guys, you really rock. As I tested it is Null terminated, but I will check it one more time. – DesantBucie Aug 30 '21 at 16:00
  • Yeah, recieved string lenght is 28. – DesantBucie Aug 30 '21 at 16:13
  • 2
    The code you posted is C, not Objective-C. The question is how do I convert a `CFDataRef` to a C string. – Willeke Aug 31 '21 at 05:09

2 Answers2

1

C-like strings are actually char pointers aka char array, that stores each character. In the case of Obj-c NSString is a class that has a char array inside, and those methods that are used to modify your string value are just methods, and NSString itself uses C-style strings such as char array. So if you want to work with a C-style string you Have to use char arrays that is compatible with Obj-C because it is just a primitive data type.

If you will need further assistance, feel free to reply. Best, regards.

SANDRUX
  • 31
  • 2
  • Yes I know that C-strings are char pointers to array and this is not about converting NSString to C-String. I need to covert this: `NSString *modelName = [[NSString alloc] initWithData: (NSData *)GPUModel encoding:NSASCIIStringEncodi` to C code (without Foundation as it doesn't work with C). The problem is, you cannot just change NSString to char * and assign it to GPUModel void pointer, as this won't work. – DesantBucie Aug 30 '21 at 15:05
0

Solution thanks to Cy-4AH and Rob Napier.

const char *gpu_name = (char *)CFDataGetBytePtr(GPUModel);
return gpu_name;
  • You need turn on `malloc scribble` in xcode run options, to be sure, that you have null-terminated string. There is exist possibility that you just have tested on clear memory segment – Cy-4AH Aug 31 '21 at 10:15
  • I don't use xcode, so i'll just have to check it by ascii values. – DesantBucie Sep 01 '21 at 19:26
  • Then you need to find how to turn on the same behaviour in yours environment – Cy-4AH Sep 02 '21 at 08:25