x


Returning variables in a plugin

Hey guys,

I've got this custom plugin I'm writing, and I've run into a bit of a snag.

//Plugin.m
const char* _TagForSource(const char *sender, const char *key){
    return "Hello!";
}

//PluginHandler.cs
[DllImport("__Internal")]
private static extern string _TagForSource(string sender, string key);

public static string TagForSource(string sender, string key){
    return _TagForSource(sender, key);
}

The problem I'm running into is that my app crashes whenever I call TagForSource, due to a malloc error(pointer was freed that wasn't malloc'd). Hardcoding the TagForSource in the PluginHandler to return a string doesn't throw an error, so I know that the problem is in Plugin.m - I just have no idea where I'm going wrong. What gives?

more ▼

asked Aug 25 '11 at 01:45 PM

flaminghairball gravatar image

flaminghairball
928 4 9 21

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You should set the return type of your imported function to System.IntPtr instead of string, then return Marshal.PtrToStringAnsi(_TagForSource(sender, key)) from your TagForSource method.

edit: and here's the reason why- http://www.mono-project.com/Interop_with_Native_Libraries#Strings_as_Return_Values

more ▼

answered Sep 06 '11 at 02:08 PM

boat365 gravatar image

boat365
106 2 2 6

I hadn't seen that page - thanks a bunch!

Sep 06 '11 at 02:14 PM flaminghairball
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1951
x419
x395
x28

asked: Aug 25 '11 at 01:45 PM

Seen: 1382 times

Last Updated: Sep 06 '11 at 02:14 PM