I don't believe that C# has the capacity to automatically understand what a
string
type is in C++, it's natively unable to convert a C#
string
into a C++
std::string
or otherwise.
Instead of using
base::string&
as the last parameter consider using
char*
instead, and then assign the value within the method. Alternatively you can use a
StringBuilder
so that the C++ method can write to the value.
For example:
VIXHZ_EXPORT long _VixHz_LoginDevice(char *szIp, int nPort, char* szUserName, char* szPassword, char* strOutInfo, int strOutInfoLength);
[DllImport(dll)]
public static extern long _VixHz_LoginDevice(string szIp, int nPort, string szUserName, string szPassword, StringBuilder strOutInfo, int strOutInfoLength);
StringBuilder strOut = new StringBuilder(32);
_VixHz_LoginDevice("1.2.3.4", 1234, "username", "password", strOutInfo, strOutInfo.Capacity);
You can omit the length parameter if you know exactly what the maximum length is expecting to be and the C++ isn't going to exceed it. An example with the
StringBuilder
can be found here:
Default Marshaling for Strings | Microsoft Docs[
^]