以下C++代码如何翻译成C#代码?
typedef int     s32,BOOL32;
#if defined(_WIN64) || defined(WIN64) ||defined (__LP64__) || defined (__64BIT__) || defined (_LP64) || (__WORDSIZE == 64) 
typedef unsigned int    u32; 
#define KD_PTR          void*
#else
typedef unsigned long    u32; 
#define KD_PTR           u32
#endif
typedef unsigned char   u8;
typedef unsigned short  u16;
typedef short           s16;
typedef char            s8;

typedef enum eTransType
{
    emTransChnCOMPort = 0, // 设备COM口透明数据
    emTransChnCameraTaskConfig = 1, // 摄像机任务配置数据
    emTransChnExtData = 2, // 透明数据,支持请求应答通知三态模型
}TransChnType;

typedef struct tagTransData
{
    tagTransData()
    {
        memset(m_byTransBuffer,0,sizeof(m_byTransBuffer));
        m_wTransBufferLen = 0;
        m_byAppType = 0;
    }
    u8 m_byTransBuffer[MAX_TRANSCHAN_DATA_LEN+1];
    u16 m_wTransBufferLen;
    s8 m_byAppType;
} TTransChnData;

typedef struct tagTransData_V2
{
    tagTransData_V2()
    {
        memset(m_byTransBuffer,0,sizeof(m_byTransBuffer));
        m_wTransBufferLen = 0;
        m_byAppType = 0;
        m_TransChnType = emTransChnCOMPort;
    }
    u8 m_byTransBuffer[MAX_TRANSCHAN_DATA_LEN+1];
    u16 m_wTransBufferLen;
    u16 m_byAppType; // 透明通道消息类型
    TransChnType m_TransChnType; // 透明通道类型
} TTransChnData_V2;

typedef struct tagTransData_V3
{
    tagTransData_V3()
    {
        m_pTransBuffer = NULL;
        m_wTransBufferLen = 0;
        m_byAppType = 0;
        m_TransChnType = emTransChnCOMPort;
    }
    u8 *m_pTransBuffer;
    u16 m_wTransBufferLen; // 透明串口数据大小最大值是8KB
    u16 m_byAppType; // 透明通道消息类型
    TransChnType m_TransChnType; // 透明通道类型
} TTransChnData_V3;

typedef struct tagTransDataRsp
{
    tagTransDataRsp()
    {
        m_pTransBuffer = NULL;
        m_wTransBufferLen = 0;
        m_byAppType = 0;
        memset(m_szUserName, 0, sizeof(m_szUserName));
        m_nPtzLevel = 0;
        m_dwBeginTime = 0;
        m_dwEndTime = 0;
        m_bHasNotify = FALSE;
        m_nErrorCode = 0;
        memset(m_szErrorDesc, 0, sizeof(m_szErrorDesc));
    }
    u8 *m_pTransBuffer; // 透明通道应答数据,大小至少为8KB。内存由上层应用维护。
    u16 m_wTransBufferLen; // 透明通道应答数据大小
    u16 m_byAppType; // 透明通道消息类型
    char m_szUserName[MAX_NAME_LEN + 1];
    int m_nPtzLevel;
    u32 m_dwBeginTime;
    u32 m_dwEndTime;
    BOOL32 m_bHasNotify;
    int m_nErrorCode;
    char m_szErrorDesc[MAX_DESC_LEN + 1];
} TTransChnDataRsp;

KDM_MCUSDK_API BOOL32 STDCALL Kdm_SendTransData(IN void *pMcuHandle, 
      IN DEVCHN tDevChn, IN TTransChnData *pTransData, OUT u32 *pErrorCode);

    KDM_MCUSDK_API BOOL32 STDCALL Kdm_SendTransData_V2(IN void *pMcuHandle, 
      IN DEVCHN tDevChn, IN TTransChnData_V2 *pTransData, OUT u32 *pErrorCode);

    KDM_MCUSDK_API BOOL32 STDCALL Kdm_SendTransData_V3(IN void *pMcuHandle, 
      IN DEVCHN tDevChn, IN TTransChnData_V3 *pTransData, 
      OUT TTransChnDataRsp *pTransDataRsp, OUT u32 *pErrorCode);

void CMainFrame::SaveSensorConfigInfo()
{
    u32 dwErrorCode = 0;
    TCHAR szContent[MAX_PATH] = {0};

    CStdString strTemp;
    strTemp = m_pConfigNvrIDEdit->GetText();
    strcpy(m_tSensorCfgParam.achTemID, strTemp.GetData());

    strTemp = m_pConfigWidthEdit->GetText();
    strcpy(m_tSensorCfgParam.achJieMianKuanDu, strTemp.GetData());

    for (int nIndex = 0; nIndex < NVR_MAX_SENSOR_OSD_LINE_NUM; ++nIndex)
    {
        strTemp = m_pArrayConfigLineEdit[nIndex]->GetText();
        strcpy(m_tSensorCfgParam.achOsdLine[nIndex], strTemp.GetData());
    }

    strTemp = m_pConfigOSDXEdit->GetText();
    m_tSensorCfgParam.nOsdX = atoi(strTemp.GetData());

    strTemp = m_pConfigOSDYEdit->GetText();
    m_tSensorCfgParam.nOsdY = atoi(strTemp.GetData());

    strTemp = m_pConfigFtpAddressEdit->GetText();
    strcpy(m_tSensorCfgParam.achFtpServerIp, strTemp.GetData());

    strTemp = m_pConfigFtpPortEdit->GetText();
    m_tSensorCfgParam.wFtpServerPort = atoi(strTemp.GetData());

    strTemp = m_pConfigFtpUsrnameEdit->GetText();
    strcpy(m_tSensorCfgParam.achFtpServerUsr, strTemp.GetData());

    strTemp = m_pConfigFtpPasswordEdit->GetText();
    strcpy(m_tSensorCfgParam.achFtpServerPwd, strTemp.GetData());

    strTemp = m_pConfigHttpAddressEdit->GetText();
    strcpy(m_tSensorCfgParam.achHttpServerIp, strTemp.GetData());

    strTemp = m_pConfigHttpPortEdit->GetText();
    m_tSensorCfgParam.wHttpServerPort = atoi(strTemp.GetData());

    strTemp = m_pConfigHttpUploadURLEdit->GetText();
    strcpy(m_tSensorCfgParam.achHttpUpLoadUrl, strTemp.GetData());

    strTemp = m_pConfigHttpKeepURLEdit->GetText();
    strcpy(m_tSensorCfgParam.achHttpKeepAliveUrl, strTemp.GetData());


    int nSel = m_pPlatformNvrCombo->GetCurSel();
    if (nSel != -1)
    {
        int nTag = m_pPlatformNvrCombo->GetItemAt(nSel)->GetTag();

        // 透明传输
        DEVCHN devChn;
        memset(&devChn, 0, sizeof(devChn));
        devChn.deviceID = m_vcDevInfo[nTag].deviceID;
        devChn.domainID = m_vcDevInfo[nTag].domainID;

        TNvrTransData tNvrTransData;
        memset(&tNvrTransData, 0, sizeof(tNvrTransData));
        tNvrTransData.wNvrMsgType = PLAT_NVR_CONFIG_SENSOR_REQ;
        tNvrTransData.wNvrBuffLen = sizeof(m_tSensorCfgParam);
        memcpy(tNvrTransData.m_abyNvrBuff, &m_tSensorCfgParam, sizeof(m_tSensorCfgParam));

        TTransChnData_V3 tTransChnData_V3;
        memset(&tTransChnData_V3, 0, sizeof(tTransChnData_V3));

        tTransChnData_V3.m_pTransBuffer = (u8*)&tNvrTransData;
        tTransChnData_V3.m_wTransBufferLen = sizeof(tNvrTransData);
        tTransChnData_V3.m_TransChnType = emTransChnExtData;

        TTransChnDataRsp tTransChnDataRsp;
        memset(&tTransChnDataRsp, 0, sizeof(tTransChnDataRsp));
        tTransChnDataRsp.m_pTransBuffer = m_byteTransChnDataBuffer;
        tTransChnDataRsp.m_wTransBufferLen = sizeof(m_byteTransChnDataBuffer);
        BOOL32 bRet = Kdm_SendTransData_V3(m_pMcuSdkHandle, devChn, &tTransChnData_V3, &tTransChnDataRsp, &dwErrorCode);
        if (!bRet)
        {
            sprintf(szContent, "保存设备配置数据失败,errcode:%u", dwErrorCode);
            GM.DuiMessageBox(m_hWnd, szContent, "kedacom", MB_OK);
        }
        else
        {
            memset(&tNvrTransData, 0, sizeof(tNvrTransData));
            memcpy(&tNvrTransData, m_byteTransChnDataBuffer, sizeof(tNvrTransData));

            if (tNvrTransData.wErrorCode != 0)
            {
                sprintf(szContent, "保存设备配置数据失败,errcode:%u", tNvrTransData.wErrorCode);
                GM.DuiMessageBox(m_hWnd, szContent, "kedacom", MB_OK);
            }
        }
    }
}

尝试修改为以下C#代码,运行成功,但是执行时候报错:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public struct tagTransData
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 513)]
    public byte[] m_byTransBuffer;
    public ushort m_wTransBufferLen;
    public char m_byAppType;
}

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public struct tagTransData_V2
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 513)]
    public byte[] m_byTransBuffer;
    public ushort m_wTransBufferLen;
    public ushort m_byAppType; // 透明通道消息类型
    public eTransType m_TransChnType; // 透明通道类型
}

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public unsafe struct tagTransData_V3
{
    public byte* m_pTransBuffer;
    public ushort m_wTransBufferLen; // 透明串口数据大小最大值是8KB
    public ushort m_byAppType; // 透明通道消息类型
    public eTransType m_TransChnType; // 透明通道类型
}

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public unsafe struct tagTransDataRsp
{
    public byte* m_pTransBuffer; // 透明通道应答数据,大小至少为8KB。内存由上层应用维护。
    public ushort m_wTransBufferLen; // 透明通道应答数据大小
    public ushort m_byAppType; // 透明通道消息类型

    public char* m_szUserName;
    public int m_nPtzLevel;
    public uint m_dwBeginTime;
    public uint m_dwEndTime;

    public uint m_bHasNotify;
    public int m_nErrorCode;

    public char* m_szErrorDesc;
}

        [DllImport(@"mcusdk.dll")]
        public extern static uint Kdm_SendTransData(IntPtr pMcuHandle, DEVCHN tDevChn, tagTransData pTransData, ref uint pErrorCode);

        [DllImport(@"mcusdk.dll")]
        public extern static uint Kdm_SendTransData_V2(IntPtr pMcuHandle, DEVCHN tDevChn, tagTransData_V2 TransData, ref uint pErrorCode);

        [DllImport(@"mcusdk.dll")]
        public extern static uint Kdm_SendTransData_V3(IntPtr pMcuHandle, DEVCHN tDevChn, tagTransData_V3 pTransData,
          ref tagTransDataRsp pTransDataRsp, ref uint pErrorCode);

private unsafe void btnSave_Click(object sender, EventArgs e)
        {
            uint dwErrorCode = 0;
            TSensorCfgParam m_tSensorCfgParam=new TSensorCfgParam();
            sbyte[] szContent = new sbyte[DefineConstants.MAX_PATH];

            m_tSensorCfgParam.achTemID = txtNVRID.Text;


            m_tSensorCfgParam.achJieMianKuanDu = txtWidth.Text;

            //for (int nIndex = 0; nIndex < NVR_MAX_SENSOR_OSD_LINE_NUM; ++nIndex)
            //{
            //    strTemp = m_pArrayConfigLineEdit[nIndex].GetText();
            //    m_tSensorCfgParam.achOsdLine[nIndex] = strTemp.GetData();
            //}

            //strTemp = m_pConfigOSDXEdit.GetText();
            //m_tSensorCfgParam.nOsdX = Convert.ToInt32(strTemp.GetData());

            //strTemp = m_pConfigOSDYEdit.GetText();
            //m_tSensorCfgParam.nOsdY = Convert.ToInt32(strTemp.GetData());

            //strTemp = m_pConfigFtpAddressEdit.GetText();
            //m_tSensorCfgParam.achFtpServerIp = strTemp.GetData();

            //strTemp = m_pConfigFtpPortEdit.GetText();
            //m_tSensorCfgParam.wFtpServerPort = Convert.ToInt32(strTemp.GetData());

            //strTemp = m_pConfigFtpUsrnameEdit.GetText();
            //m_tSensorCfgParam.achFtpServerUsr = strTemp.GetData();

            //strTemp = m_pConfigFtpPasswordEdit.GetText();
            //m_tSensorCfgParam.achFtpServerPwd = strTemp.GetData();

            //strTemp = m_pConfigHttpAddressEdit.GetText();
            //m_tSensorCfgParam.achHttpServerIp = strTemp.GetData();

            //strTemp = m_pConfigHttpPortEdit.GetText();
            //m_tSensorCfgParam.wHttpServerPort = Convert.ToInt32(strTemp.GetData());

            //strTemp = m_pConfigHttpUploadURLEdit.GetText();
            //m_tSensorCfgParam.achHttpUpLoadUrl = strTemp.GetData();

            //strTemp = m_pConfigHttpKeepURLEdit.GetText();
            //m_tSensorCfgParam.achHttpKeepAliveUrl = strTemp.GetData();


            int nSel = lbDevice.SelectedIndex;
            if (nSel != -1)
            {
                //int nTag = m_pPlatformNvrCombo.GetItemAt(nSel).GetTag();

                // 透明传输
                DEVCHN devChn = new DEVCHN();
                //C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: The memory management function 'memset' has no equivalent in C#:
                //Comm.memset(devChn, 0, sizeof(DEVCHN));
                devChn.deviceID = deviceList[nSel].deviceID;
                devChn.domainID = deviceList[nSel].domainID;

                TNvrTransData tNvrTransData = new TNvrTransData();
                //C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: The memory management function 'memset' has no equivalent in C#:
                //Comm.memset(tNvrTransData, 0, sizeof(TNvrTransData));
                tNvrTransData.wNvrMsgType = DefineConstants.PLAT_NVR_CONFIG_SENSOR_REQ;
                //tNvrTransData.wNvrBuffLen = sizeof(m_tSensorCfgParam);
                //C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: The memory management function 'memcpy' has no equivalent in C#:
                //Comm.memcpy(tNvrTransData.m_abyNvrBuff, m_tSensorCfgParam, sizeof(m_tSensorCfgParam));

                tagTransData_V3 tTransChnData_V3 = new tagTransData_V3();
                //C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: The memory management function 'memset' has no equivalent in C#:
                //Comm.memset(tTransChnData_V3, 0, sizeof(TTransChnData_V3));

                tTransChnData_V3.m_pTransBuffer = (byte)&tNvrTransData;
                //tTransChnData_V3.m_pTransBuffer = (byte*) ref (tNvrTransData)[0];
                //tTransChnData_V3.m_wTransBufferLen = sizeof(TNvrTransData);
                tTransChnData_V3.m_TransChnType = eTransType.emTransChnExtData;

                tagTransDataRsp tTransChnDataRsp = new tagTransDataRsp();
                //C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: The memory management function 'memset' has no equivalent in C#:
                //Comm.memset(tTransChnDataRsp, 0, sizeof(tagTransDataRsp));
                m_byteTransChnDataBuffer = new byte[8 * 1024];
                tTransChnDataRsp.m_pTransBuffer = (byte*)m_byteTransChnDataBuffer[0];
                //tTransChnDataRsp.m_wTransBufferLen = sizeof(m_byteTransChnDataBuffer);
                uint bRet = myAPI.Kdm_SendTransData_V3(mcuHandle, devChn, tTransChnData_V3, ref tTransChnDataRsp, ref dwErrorCode);
                if (bRet == 0)
                {
                    //szContent = string.Format("保存设备配置数据失败,errcode:{0:D}", dwErrorCode);
                    //GM.DuiMessageBox(m_hWnd, szContent, "kedacom", MB_OK);
                }
                else
                {
                    //C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: The memory management function 'memset' has no equivalent in C#:
                    //memset(tNvrTransData, 0, sizeof(TNvrTransData));

                    //C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: The memory management function 'memcpy' has no equivalent in C#:
                    //memcpy(tNvrTransData, m_byteTransChnDataBuffer, sizeof(TNvrTransData));

                    if (tNvrTransData.wErrorCode != 0)
                    {
                        //szContent = string.Format("保存设备配置数据失败,errcode:{0:D}", tNvrTransData.wErrorCode);
                        //GM.DuiMessageBox(m_hWnd, szContent, "kedacom", MB_OK);
                    }
                }
            }
        }

求大神翻译

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!