Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return **C.Char #1

Closed
AlexDias79 opened this issue Jan 12, 2018 · 2 comments
Closed

Return **C.Char #1

AlexDias79 opened this issue Jan 12, 2018 · 2 comments

Comments

@AlexDias79
Copy link

AlexDias79 commented Jan 12, 2018

Been trying to return a string, not printing it to console, but there seem not to be a way.

I did try 2 different approaches

//export PrintHello2
func PrintHello2(Input *C.char, Output **C.char) int32 {
	*Output = C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))
	return 1
}

and

//export PrintHello4
func PrintHello4(Input *C.char) *C.char{
	return C.CString(fmt.Sprintf("From DLL: Hello, %s!\n", C.GoString(Input)))
}

I'm calling this code from .net using the following test code

class Program
{
	[DllImport("goDLL.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
	public static extern int PrintHello2([In] byte[] data, [MarshalAs(UnmanagedType.I1)] ref SByte[] output);

	[DllImport("goDLL.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
	public static extern SByte[] PrintHello4(byte[] data);

	static void Main(string[] args)
	{
		string res = "demo";
		SByte[] output= { };

		Console.WriteLine("PrintHello4 Returns: " + PrintHello4(Encoding.UTF8.GetBytes(res)).Length);

		Console.WriteLine("PrintHello2 Returns: " + PrintHello2(Encoding.UTF8.GetBytes(res), ref output));
		Console.WriteLine("Ref Val changed to: " + output.Length + "\n");
	}
}

None of the Go function works as expected and program chashes with System.Runtime.InteropServices.MarshalDirectiveException for any of the 2 functions

@AlexDias79
Copy link
Author

@z505
Copy link
Owner

z505 commented Jan 13, 2018

Good to hear there was a solution, I will close the issue now

@z505 z505 closed this as completed Jan 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants