CString 转 char*

    技术2022-05-12  9

    方法1.  强制类型转换

    CString st1 = "method1";

    LPCSTR lpbuff1= (LPSTR)(LPCSTR)st1;

    ....

    对st1的操作会对lpbuff1产生影响

    方法2.  利用strcpy

    CString st2 = "method2";

    char *lpbuff2 = new char[st2.GetLength()+1];

    scrcpy(lpbuff2, st2);

    ...

    delete []lpbuff2;

    方法3.  利用memcpy

    CString st3 = "method3";

    int ilen = st3.GetLength();

    char *lpbuff3 = new char[iLen+1];

    ZeroMemory(lpbuff3, iLen+1);

    memcpy(lpbuff3, st3, iLen);

    ...

    delete []lpbuff3;

    方法4.  利用GetBuffer

    CString st4 = "method4";

    char *lpbuff4 = st4.GetBuffer(st4.GetLength());

    ...

    对st4的操作会对lpbuff4产生影响


    最新回复(0)