1

I have to call an external library that is using some non-standard types, so I've created a C++ wrapper around this external library. To try to save some pain I've setup on of the arguments as a wchar_t*:

void StartBatchJob(wchar_t* server, unsigned short port, int timeout, wchar_t* xml, int length);

When I'm calling the function:

var xml = new StringBuilder("... xml string removed for brevity");
StartBatchJob(((Char*)hostCharPointer), 6015, 60000, ((Char*)Marshal.StringToHGlobalUni(xml.ToString())), xml.ToString().Length);

However, when the function gets called it appears to be cutoff at 256 characters. When I debug/watch the xml parameter in the C++ code I see it's cutoff at 256 characters.

Now, I haven't done any C++ or native code calling in .NET in a very long time, so I have forgotten all the details of it. I don't think it's a problem with the Marshal.StringTo... call as that appears to return a fully rendered char*. It appears it's when the runtime tries to pass the arguments.

Update:

By request, here is the implementation of the StartBatchJob method:

extern "C" ZCHAR * JDEWINAPI jdeXMLRequest(const JCHAR *szHostName, unsigned short usPort, const int nNetTimeout, void *xml, int size);

void JDEService::StartBatchJob(wchar_t* server, unsigned short port, int timeout, wchar_t* xml, int length)
{
    ZCHAR* presp = jdeXMLRequest(reinterpret_cast<JCHAR *>(server), port, timeout, xml, length);

}

Any help is greatly appreciated!

Thanks

Matthew Bonig
  • 2,001
  • 2
  • 20
  • 40
  • 2
    Not sure if we can help you without knowing the body of `StartBatchJob` :\ – user541686 Jan 03 '12 at 20:52
  • 1
    Show us the c++ prototype and the lines where you declare the c++ function for c#. – rekire Jan 03 '12 at 20:55
  • What's the p/invoke defintion for `StartBatchJob` look like? Also you need to save the output of `Marshall.StringToHGlobal` and call `Marshal.FreeHGlobal` on it. – shf301 Jan 03 '12 at 20:57
  • I was originally using STringToCoTaskMemUni. Once the call to the C++ function/method is done, I do call a FreeCoTaskMem. I didn't include that because I didn't think it mattered for the problem I'm having – Matthew Bonig Jan 03 '12 at 21:05
  • `jdeXMLRequest` takes `xml` as a `void*`, and it takes an `int size` to say how many characters the `xml` string contains. However, without knowing the size of each character (one byte, two bytes, etc.), `jdeXMLRequest` cannot figure out how many bytes the entire string is. What type does `jdeXMLRequest` expect the string to be in, or how do you tell `jdeXMLRequest` the length of each character? – Max Lybbert Jan 03 '12 at 21:54
  • @MaxLybbert, I don't think that's the problem. I believe the char* was getting cutoff well before it was ever passed to the jdeXmlRequest call. I don't know if it's a legit problem, though, because all I've been able to do is check it's value with the debugger. – Matthew Bonig Jan 04 '12 at 18:15

0 Answers0