There are several drawbacks, as indicated in the various answers that you have. To summarize everything, here is what I suggest.
On the inside, declare something like this (assuming you are coding in C ++):
extern "C" __declspec(dllexport) const char* __cdecl Connect(const char* lpPostData) { static char buffer[1000]; ... return buffer; }
extern "C" makes it clear to the compiler that it should not distort the name of the function when exporting it, but should treat it the same way as processing the function C.
__ceclspec(dllexport) makes the function visible in the DLL as an exported entry point. You can then reference it from external projects.
__cdecl ensures that you use the C method to pass arguments on the stack. You could have badly damaged things if the caller does not accept the same argument passing scheme.
Use char sequentially: either sick before char (ANSI text) or wchar_t (16-bit Unicode text). And never, never, return a pointer to a local variable, as it was in your example. As soon as the function returns, this memory location will no longer be valid and may contain garbage. In addition, if the user changes the data stored there, this may lead to a program crash, as this will lead to a failure of the call stack.
On the managed side of C #, here's what I recommend:
[DllImport("test.dll", EntryPoint="Connect", CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.Cdecl)] private static extern System.IntPtr Connect(string postData);
which will be bound to your Connect entry point, use the ANSI conditional convention (8 bits per character, as expected by char ), and make sure the caller is expecting __cdecl convection.
You will need to march the IntPtr object to the string object by calling:
string value = Marshal.PtrToStringAnsi (Connect (postData));
Note: in my original post, I recommended declaring Connect as a return string , but as Mattias's comment fixes, this is incorrect. doing so. See this article in the CLR Inside Out for an explanation of how the CLR handles retval type string . Thanks for pointing this out to me (and thanks to romkyns ).
You can also consider copying a string from buffer to a piece of memory allocated through CoTaskMemAlloc in your own code and returning a pointer to it. Then you can simply declare the DllImport function returning a string and not need further sorting in a controlled world.