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];

                }
            }

5
  • 2
    Looks like GPUModel is CFDataRef. There is all C functions you need developer.apple.com/documentation/corefoundation/… Commented Aug 30, 2021 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. Commented Aug 30, 2021 at 15:54
  • Thanks guys, you really rock. As I tested it is Null terminated, but I will check it one more time. Commented Aug 30, 2021 at 16:00
  • Yeah, recieved string lenght is 28. Commented Aug 30, 2021 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. Commented Aug 31, 2021 at 5:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

Solution thanks to Cy-4AH and Rob Napier.

const char *gpu_name = (char *)CFDataGetBytePtr(GPUModel);
return gpu_name;

3 Comments

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
I don't use xcode, so i'll just have to check it by ascii values.
Then you need to find how to turn on the same behaviour in yours environment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.