将字符串作为引用类型从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 doestimestampToNewString()
do? If it makes anew 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;