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.

#include <windows.h>
#include <winnls.h>

#define DLL_EXPORT __declspec(dllexport)

//Test function receives and successfully returns a string
DLL_EXPORT char* GetString (char* pInput)
	return pInput;

DLL_EXPORT LPWSTR AnsiToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr)
	int cbMultiByte = -1;	//Don't need to specify length for null-terminated string
	int cchWideChar;
	cchWideChar = MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
	LPWSTR lpWideCharStr[cchWideChar];		//Set output buffer to required size. Need to allow space for null char?
	MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar);
	//lpWideCharStr = StringToHex(lpWideCharStr);  //Not sure how to do this
	return lpWideCharStr;

DLL_EXPORT LPSTR UnicodeToAnsi(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr)
	int cbMultiByte;
	int cchWideChar = -1; 	//Don't need to specify length for null-terminated string

	//lpWideCharStr = HexToString(lpWideCharStr);  //not sure how to do this
	cbMultiByte = WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, NULL, NULL);
	LPSTR lpMultiByteStr[cbMultiByte];		//Set output buffer to required size. Need to allow space for null char?
	WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, NULL, NULL);

	return lpMultiByteStr;
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.

Thanks for your help!