将字符串作为引用类型从C ++/CLI传递到C ++ [英] passing strings as reference type from C++/CLI to C++

查看:92
本文介绍了将字符串作为引用类型从C ++/CLI传递到C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++/CLI函数,该函数调用本机C ++函数,将字符串作为参考传递,如下所示:

I have a C++/CLI funtion that calls the native C++ function passing strings as reference as follows:

bool ConnectionsMgr::GetRespHeader([Out] String^ %eTimestamp,
                               [Out] String^ %UTimestamp,[Out] int% nCount)
    {
        bool bRet = true;
        int resultCount = 0;
        char* expTime;
        char* lastUpdatedTime ;
        
        //calling C++ function, I to pass the char* as reference
        lMgr->getResponseHeader(expTime, lastUpdatedTime , resultSCount) ;
        eTimestamp = gcnew String(expTime);
        UTimestamp = gcnew String(lastUpdatedTime);

        nSidsCount = resultSCount;
        return bRet;

    }



C ++函数看起来像这样



The C++ function looks like this

bool CLMgr::getResponseHeader(char *eTimestamp, char *UTimestamp, int &nSCount){
    
    int nCnt = 0;

    if(m_Response) {
        eTimestamp = timestampToNewString(m_Response->m_eTime);
        UTimestamp = timestampToNewString(m_Response->m_UTime);
        
        for( int n = 0; n < m_Response->m_Count; n++){
          if(bflag)
              nCnt++;
        }
    }
    nSCount = nCnt;
    return true;
}



我必须将eTimestamp,UTimestamp,SCount的值转换为C#代码.我在做这些事情时做错了什么.在执行完C ++函数后,我什至没有得到它.

预先表示感谢.



I have to get the value of eTimestamp, UTimestamp, SCount to my C# Code. What am I doing wrong to get these values. I am not even getting it after executing out of the C++ function.

Thanks in advance.

推荐答案

好吧,让我们看看... expTime未声明,lastUpdatedTime未声明...这甚至无法编译. ..这是您的实际代码吗?

另外,timestampToNewString()的作用是什么?如果它进行了new char[]调用,则在使用完它或发生内存泄漏后,您需要对其进行分配.

...这是什么目的:
Well, lets see... expTime is undeclared, lastUpdatedTime is undeclared... this wouldn''t even compile... is this your actual code?

Additionally, what does timestampToNewString() do? If it makes a new char[] call, then you need to deallocate that after you''re done with it or you have a memory leak.

...and what''s the purpose of this:
for( int n = 0; n < m_Response->m_Count; n++){
  if(bflag)
      nCnt++;
}

//This does the same exact thing (which doesn''t make sense why you''re making a copy of a variable either but...)
if(bflag) nCnt = m_Response->m_Count;


这篇关于将字符串作为引用类型从C ++/CLI传递到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆