C# – Using Win32 dll in c# Application(returning char* to c# problem)


I am working on c# application(Using win 32 dll in my apllication)…I am trying something like this
In DLL(test.dll):

char* Connect(TCHAR* lpPostData)
char buffer[1000];
return buffer;

IN c# application:

[DllImport("test.dll", EntryPoint = "Connect", CharSet = CharSet.Unicode)]
 [return: MarshalAs(UnmanagedType.LPWStr)]
 public static extern string Connect(StringBuilder postdata);

string returnedData = Connect(postdata);

But returning of data is not happening properly….
Pls can any body tell where i am going wrong
Thanks in advance

Best Solution

TCHAR * in tells me it's a unicode input (UNICODE is defined under CE), but the char * tells me it's likely a byte array (or ascii string) and whoever created this API should be slapped for mixing the two, as it's really, really poor design.

You certainly can't marshal the return as a wide character string, because it isn't one. On the desktop you'd use Tony's suggestion, but MSDN (and practice) clearly shows that it's not available in the CF (no idea why MS thought we wouldn't need it).

The Smart Device Framework does have it. The other option is to use Marshal to copy from the returned pointer to a byte array and then use Encoding.ASCII to turn that array into a string. Of course that points out the other obvious flaw in this API that it shouldn't be returning a string in the first place.


Since I'm seeing other suggestions on what you should do that I don't really agree with, I suppose I should give you an example:

Your native call should look more like this:

extern "C" 
const BOOL __cdecl Connect(TCHAR* lpPostData, 
                         TCHAR *returnBuffer, 
                         DWORD *returnSize) 
  // validate returnSize, returnBuffer, etc
  // write your data into returnBuffer

  TCHAR *data = _T("this is my data");

  _tcscpy(returnBuffer, data);
  *returnSize = (_tcslen(data) + 1) * sizeof(TCHAR);

  return succeeded;

Note that I'm simply returning a success code. The text data is passed in as a pointer, along with a length for it (so the API knows how much space it can use and can return how much it did use). Also not that I'm consistent with my string variable data types, and I'm using the TCHAR macros which will become wchar_t under CE, which is consistent with the rest of the OS (which has almost no ASCII APIs to begin with).

Most of the WIn32 API set works this exact same way.

Your P/Invoke declaration, then is very simple:

[DllImport("test.dll", SetLastError=true)] 
private static extern bool Connect(string postData, 
                                   StringBuilder data, 
                                   ref int length);

Using it is also straightforward:

void Foo()
  int length = 260;
  StringBuilder sb = new StringBuilder(length);
  if(Connect("Bar", sb, ref length))
    // do something useful

Note that the StringBuilder has to get initialized to some size, and that size is what you pass in fore the third parameter.

Related Question