I know this message probably should go to the Interop forum, so if a mod could copy it there, that would be great, but that forum doesn't seem to get very much action.
Here is my problem. I'm trying to make a call into an unmanaged dll through C#. Here is the prototype of the unmanaged function:
void MyFunc(PCWSTR a, PCWSTR b, PWSTR* c);
Here is how I am currently making the call work:
// The import [DllImport("mydll.dll")] public static extern void MyFunc([MarshalAs(UnmanagedType.BStr)] string a, [MarshalAs(UnmanagedType.BStr)] string b, ref IntPtr c);
// The actual call in code IntPtr foo = Marshal.AllocCoTaskMem(1024); MyFunc(somestring, anotherstring, ref foo);
unsafe{ string output = new string((char*)foo.ToPointer()); }
Marshal.FreeCoTaskMem(foo);
Ok, that works great for getting my correct output from the function, MyFunc. Now, I really really really hate that I had to use an unsafe code block in order to do this. What is another way of accomplishing this same task without having to use the unsafe block I've tried using StringBuilder for the the last parameter as well, didn't work. I've tried passing it as ref, didn't work, I've tried MarshalAs(UnmanagedType.LPWStr), didn't work....they all fail...(the ref one fails because it's just completely wrong, exception). The others, besides the attempt at passing StringBuilder by ref, did not return correct strings, there were of length 2, when they should be ~100, and the characters were garbage.
Any help at all would be great! Seems like i've been working on this for ages now...thanks so much!
PWSTR* being passed into unmanaged dll
manilahunk
Try this one instead of your unsafe code block:
string output = System.Runtime.InteropServices.Marshal.PtrToStringUni(foo);
Tihomir Ignatov
I know this message probably should go to the Interop forum, so if a mod could copy it there, that would be great, but that forum doesn't seem to get very much action.
Here is my problem. I'm trying to make a call into an unmanaged dll through C#. Here is the prototype of the unmanaged function:
void MyFunc(PCWSTR a, PCWSTR b, PWSTR* c);
Here is how I am currently making the call work:
// The import
[DllImport("mydll.dll")]
public static extern void MyFunc([MarshalAs(UnmanagedType.BStr)] string a,
[MarshalAs(UnmanagedType.BStr)] string b,
ref IntPtr c);
// The actual call in code
IntPtr foo = Marshal.AllocCoTaskMem(1024);
MyFunc(somestring, anotherstring, ref foo);
unsafe{
string output = new string((char*)foo.ToPointer());
}
Marshal.FreeCoTaskMem(foo);
Ok, that works great for getting my correct output from the function, MyFunc. Now, I really really really hate that I had to use an unsafe code block in order to do this. What is another way of accomplishing this same task without having to use the unsafe block I've tried using StringBuilder for the the last parameter as well, didn't work. I've tried passing it as ref, didn't work, I've tried MarshalAs(UnmanagedType.LPWStr), didn't work....they all fail...(the ref one fails because it's just completely wrong, exception). The others, besides the attempt at passing StringBuilder by ref, did not return correct strings, there were of length 2, when they should be ~100, and the characters were garbage.
Any help at all would be great! Seems like i've been working on this for ages now...thanks so much!
Jason
schnee@<no_spam>vrac.iastate.edu