C#向C++编写的DLL传递字符串参数的办法

使用StringBuilder,举例:

 

C++代码中函数定义如下:

PVPOWERFORCASTDLL_API int PVPowerForcast(SForcastInfo &_ForcastInfo,char* DatePtr,char* Dir)

 

C#代码中这样使用:

[DllImport("PVPowerForcastDll.dll", EntryPoint = "PVPowerForcast", CallingConvention = CallingConvention.Cdecl)]
private static extern int PVPowerForcast(ref SForcastInfo ForcastInfo, StringBuilder date,StringBuilder dir);

 

另外,如果C++中SForcastInfo 定义为struct,在C#中也要使用struct对应定义,使用class时会出现问题

posted @ 2013-11-14 09:28  肖璟  阅读(1395)  评论(0编辑  收藏  举报