日期:2014-05-18  浏览次数:21037 次

关于VS2010字符串传入C++dll问题
c++dll中有这样一个方法:
C/C++ code
GIS_API bool LT_Open(char* pPath);

C#调用部分:
C# code
        [DllImport("LTGisEngine.dll", EntryPoint = "LT_Open", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private static extern bool LT_Open(string pPath);
        public static bool LT_Open(string pPath)
        {
                bool bOper = false;
                bOper = LT_OpenMap(pDataPath);
                return bOper;
        }


用vs2010编写的测试程序,当C#程序调用XXX.LT_Open(@"D:\data\data\data");c++接收到的字符串为"D"
用vs2008编写的测试程序,当C#程序调用XXX.LT_Open(@"D:\data\data\data");c++接收到的字符串为"D:\data\data\data"

求解为何?2010和2008对字符串的处理有不一样的地方吗?还是我有数据类型搞错了?谢谢各位!

------解决方案--------------------
数据类型用错了,那你就可能会报错,
 我想知道这个函数LT_OpenMap(pDataPath);
------解决方案--------------------
charset ansi
------解决方案--------------------
可能是CharSet = CharSet.Auto,在两个版本中CharSet.Auto含义不同,你应该根据c++程序的字符集显式指定CharSet ,而不要用CharSet.Auto,CharSet.Auto的定义可能会随着版本变化,
------解决方案--------------------
我猜测,你的c++程序应该是多字符编码,如果是这样,应该用CharSet = CharSet.Ansi