C# 调用 C dll 传值 char*

来源:互联网 发布:weka的朴素贝叶斯算法 编辑:程序博客网 时间:2024/05/19 02:44

C# 调用C 写的dll 里面有char * 之前一直是用string,可是这个函数怎么也不好用了,别人告诉我用intptr,好用了,虽然我也不知道为什么。给大家看看吧,也许也能用的着。

CP5200API int  CALLING_CONVCP5200_RS232_InitEx(const char *fName, int nBaudrate, DWORDdwTimeout);

声明为

 [DllImport("CP5200.dll", CharSet =CharSet.Auto)]
       public static extern int CP5200_RS232_InitEx(IntPtr fName, intnBaudrate, int dwTimeout);
 

调用:

int nRet = 0;
 

          string strPort;
               strPort = "COM" + m_nPort.ToString();
               nRet =CP5200.CP5200_RS232_InitEx(Marshal.StringToHGlobalAnsi(strPort),m_nBaudrate, m_nTimeout);



想了一下,这块还是要传入一个地址进去才可以。

原创粉丝点击