Plugins - Sending String Data

I tried this thread in the iOS section but no-one had a clue.

Is it possible to send string data to a C plugin from Unity? If so what type should the C script be expecting?

Im currently using the following:

[DllImport ("__Internal")] 
public static extern void ActivateTutorial( string description );

and the C Script:

extern 'C'
{
    void ActivateTutorial(  what magical type goes here? Im assuming char *  );
}

The code there is correct this way yeah.
sending over is char * and then using the NSString functions to get a string from utf8

the way back goes over cocoaToMonoString

Thanks. Solved in seconds.

This is probably more appropriate for the iOS forum, but I’m hoping someone following this thread knows the answer. What’s the non-pro equivalent of cocoaToMonoString? I can’t seem to find that function in my project.

I’m just string to get out the marketing version string like so:

	const char* getMarketingVersionString()
	{
		NSString *versionStr = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
		return [versionStr UTF8String];
	}

But calling the function triggers this error: malloc: *** error for object 0x5c9ca90: pointer being freed was not allocated

The function is the same. iOS plugin support is license independent, its the same for both.

But you need to declare the extern function etc.

Yes, I’ve wrapped my objective-C side function in an extern ā€œCā€, but Xcode complains: ā€œā€˜cocoaToMonoString’ was not declared in this scope.ā€ Where is this function defined? I can’t seem to find it when I try to find-in-files.

I found my answer in the Bonjour plugin sample. I thought I needed to alloc/init a new NSString before returning the C-string, but a simple malloc will do:

// Helper method to create C string copy
char* MakeStringCopy (const char* string)
{
	if (string == NULL)
		return NULL;
	
	char* res = (char*)malloc(strlen(string) + 1);
	strcpy(res, string);
	return res;
}
2 Likes