In the end, the maintainer of the C++ code changed his code to use standard char
s instead of _TCHAR
s and I have managed to get this working.
Binding a C++ _TCHAR to a C# string in a SWIG interface
-
30-06-2023 - |
Question
I have some old C++ code that I'm trying to compile as a DLL so that it can be used in a C# Xamarin.IOS project, using SWIG to create bindings for a wrapper between the C++ and the C#.
So far I have been able to get it working for all methods that take or return int
, double
, or bool
parameters, but I'm stuck with how to deal with _TCHAR
s.
Where the C++ code uses _TCHAR
s as the parameter types, no matter what I've tried in the SWIG interface file, the C# parameters for any methods which should take a string
are coming up as SWIGTYPE_p__TCHAR
, which is as far as I can tell is what SWIG produces when it doesn't know what it should use.
I'm sure that this should be a case of using the right typemap in the SWIG interface file, but nothing I've tried so far has worked, any suggestions would be much appreciated!
Solution 2
OTHER TIPS
TCHAR
is a preprocessor defined constant that is set at compile time windows.h
based on the UNICODE
preprocessor constant: if UNICODE
is defined, it will either be wchar_t
, otherwise it will be char. But you have to choose one: is your module unicode or ascii? If former, you should set UNICODE as part of your SWIG command:
swig -DUNICODE -c++ -csharp example.i
else
swig -c++ -csharp example.i
Since you are presumably doing latter, and getting this error, you probably forgot to %include <windows.i>
after the preamble and before your declarations (if you have trouble, show your .i
then I can be more specific).