C#中的等效char*我有一个用c++编写的dll。我正在调用一个函数调用。我有这个C++声明。intdll_registerAccount(char*用户名,char*密码);我做了这个dllimport声明:[DllImport("pjsipDlld")]staticexternintdll_registerAccount(IntPtrusername,IntPtrpassword);我的DllImport是否等同于使用IntPtr+的c++?非常感谢任何建议,C#方法是在处理字符串时让封送拆收器处理char*:[DllImport("pjsipDlld")]staticexternintdll_registerAccount([MarshalAs(UnmanagedType.LPStr)]stringusername,[MarshalAs(UnmanagedType.LPStr)]stringpassword);如果要处理宽字符,请将LPStr替换为LPWStr。char*的StringBuilder因为长度未知?[DllImport("pjsipDlld")]staticexternintdll_registerAccount(StringBuilder用户名,StringBuilder密码);[DllImport("pjsipDlld",CharSet=CharSet.Ansi)]staticexternintdll_registerAccount(stringusername,stringpassword);我通过P/Invoke帮助程序运行您的C++系列:publicpartialclassNativeMethods{///返回类型:int///username:char*///password:char*[System.Runtime.InteropServices.DllImportAttribute("",EntryPoint="dll_registerAccount")]publicstaticexternintdll_registerAccount(System.IntPtrusername,System.IntPtrpassword);所以你是对的,它应该工作。但是有一些可用的自动封送处理,你应该可以这样做:[DllImport("pjsipDlld")]staticexternintdll_registerAccount(stringusername,stringpassword);请参阅此MSDN页面以了解默认封送处理和覆盖它。注意调用约定,这让我很高兴。在我的例子中,我需要调用一个C++DLL,但使用C风格的导出,它使用cdecl调用约定。如果您有幸使用源VisualStudio解决方案,请转到属性->C/C++->高级,然后在“调用约定”下找到它。这解决了我:以上就是C#学习教程的全部内容:等价于C#中的char*。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注—[DllImport(DllName,CharSet=CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)]//boolMyFunction(char*fileName)本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
