String Argument and Return Values From DLL

This is a discussion on String Argument and Return Values From DLL within the Windows Programming forums, part of the Platform Specific Boards category; I'm new to C programming and have a very simple task to do. A scripting language in another application needs ...

String Argument and Return Values From DLL

I'm new to C programming and have a very simple task to do. A scripting language in another application needs to convert Unicode strings to ASCII strings using the Windows API functions WideCharToMultiByte and MultiByteToWideChar. Unfortunately it doesn't support output values in function arguments like these functions use. So, I need to write a DLL to wrap those two functions and return the result string.

The other problem is that the scripting language cuts strings off at the first null character when passing them to a DLL, but unicode strings may have a null every other byte. I was thinking of converting the character codes to a hex string to avoid having nulls when passing the value, then do the opposite conversion to get the original string back. For example: "sampleŘ" would become "73616D706C6500".

Here's my test code. I couldn't find a simple example of the hex conversion so I didn't include it. I think the data type declarations/casts just need tweaked to allocate memory correctly. I just can't find the right combination. It also doesn't seem to like me declaring a variable in the middle of the function, although I copied that from some sample code on a web page.

I know I can successfully pass a CHAR* from the scripting language to the DLL and return it again successfully. However, I'm not sure if I can use a CHAR* data type for the input and output arguments of WideCharToMultiByte and MultiByteToWideChar. I've tried several different data types and either it won't compile or it crashes the calling application or the calling app just gets garbage return values.

I tried compiling this in TCC (Tiny C Compiler) or MS Visual C++ 6.0. I also have .Net compiler. I don't care which I use as long as it doesn't introduce additional dependencies for the DLL.