Insanely simple interop question.
-
All I want to do is: I want to make a win32 DLL that takes two LPWSTR's. One [in], one [out]. That's ALL! The problem is, when I call it from C#, the out string is always unchanged. I know this must be simple to do- interop can't suck this bad. Here is my code: /// In the DLL: extern "C" { __declspec(dllexport) void transformIt(LPWSTR,[out] LPWSTR); void transformIt(LPWSTR markup,[out] LPWSTR changed) { changed= L"SOME STUFF"; } } // The Interop and the call: [System.Runtime.InteropServices.DllImportAttribute("mydll.dll")] private static extern void transformIt( [MarshalAs(UnmanagedType.LPWStr)] string markup, [Out,MarshalAs(UnmanagedType.LPWStr)] out string changed) ; string stuff="wefwefw"; string changed; //also tried string changed="",changed=null, and a StringBuilder transformIt( stuff, out changed); Console.WriteLine(" >>>>>>>>>>>>>>> " + changed); //empty I've tried this with a StringBuilder, I've tried with "ref" instead of an "out". No error is thrown, even when I do a try catch block. I know it's finding the DLL ok, becaause I made another test method that just returns an int and it worked. why why why why why why why Please tell me it's me that sucks and not interop. "Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read." -Groucho Marx
-
All I want to do is: I want to make a win32 DLL that takes two LPWSTR's. One [in], one [out]. That's ALL! The problem is, when I call it from C#, the out string is always unchanged. I know this must be simple to do- interop can't suck this bad. Here is my code: /// In the DLL: extern "C" { __declspec(dllexport) void transformIt(LPWSTR,[out] LPWSTR); void transformIt(LPWSTR markup,[out] LPWSTR changed) { changed= L"SOME STUFF"; } } // The Interop and the call: [System.Runtime.InteropServices.DllImportAttribute("mydll.dll")] private static extern void transformIt( [MarshalAs(UnmanagedType.LPWStr)] string markup, [Out,MarshalAs(UnmanagedType.LPWStr)] out string changed) ; string stuff="wefwefw"; string changed; //also tried string changed="",changed=null, and a StringBuilder transformIt( stuff, out changed); Console.WriteLine(" >>>>>>>>>>>>>>> " + changed); //empty I've tried this with a StringBuilder, I've tried with "ref" instead of an "out". No error is thrown, even when I do a try catch block. I know it's finding the DLL ok, becaause I made another test method that just returns an int and it worked. why why why why why why why Please tell me it's me that sucks and not interop. "Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read." -Groucho Marx
Have you tested your DLL in a non .Net environment? Maybe something is just not right with the DLL.
-
Have you tested your DLL in a non .Net environment? Maybe something is just not right with the DLL.
Well like I say, I tried making a simple function that just returns an int, in the same dll, and calling that from dot net and it worked fine. Hey can you tell me if I'm assigning the LPWSTR properly? Do I have to do like memory alloc or something, or make a char array? The compiler didn't throw any errors.. thanks "Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read." -Groucho Marx