[英]Binding a C++ _TCHAR to a C# string in a SWIG interface
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#. 我有一些旧的C ++代码,试图将其编译为DLL,以便可以在C#Xamarin.IOS项目中使用它,并使用SWIG为C ++和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. 到目前为止,我已经能够使它适用于所有接受或返回int
, double
或bool
参数的方法,但是我对如何处理_TCHAR
。
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. 在C ++代码使用_TCHAR
作为参数类型的地方,无论我在SWIG接口文件中尝试过什么,任何应该采用string
方法的C#参数都以SWIGTYPE_p__TCHAR
,据我所知是SWIG在不知道应使用的情况下产生的结果。
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! 我确信这应该是在SWIG接口文件中使用正确的typemap的情况,但是到目前为止我没有尝试过任何方法,任何建议将不胜感激!
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. TCHAR
是一个预处理器定义的常量,它基于UNICODE
预处理器常量在编译时windows.h
设置:如果定义了UNICODE
,则它将为wchar_t
,否则将为char。 But you have to choose one: is your module unicode or ascii? 但是您必须选择一个:模块是unicode还是ascii? If former, you should set UNICODE as part of your SWIG command: 如果是以前的版本,则应将UNICODE设置为SWIG命令的一部分:
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). 由于您大概是在做后者,并收到此错误,因此您可能忘记了在序言之后和声明之前%include <windows.i>
(如果遇到问题,请显示.i
我可以更具体地介绍)。
最后,C ++代码的维护者将其代码更改为使用标准char
而不是_TCHAR
并且我设法使此工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.