描述:
大家好,刚学习ATL技术,。
我现在用VC以动态库的形式做了一个控件,现在我要把控件中的值传递到WEB页面中的text框中,该如何做?为什么我传出来的都是一些乱码,谢谢。
以下是我声明的代码:
.h
STDMETHOD(get_strBuf3)(/*[out, retval]*/ BSTR* pVal);
STDMETHOD(put_strBuf3)(/*[in]*/ BSTR newVal);
.cpp
STDMETHODIMP CMagic::get_strBuf2(BSTR *pVal)
{
// TODO: Add your implementation code here
// strMagicBuf2 = new TCHAR[50];
// strcpy (strMagicBuf2, "Get String");
ZeroMemory (m_szBuf2, sizeof(m_szBuf2));
strcpy ((char*)m_szBuf2, ("this is test"));
*pVal = (BSTR)m_szBuf2;
return S_OK;
}
为什么我在网页上接收到的都是乱码?
谢谢。100分
解决方案1:
或者直接用L"this is a test"
解决方案2: 其实,.ocx的控件和.dll的控件只是文件名不同!!
"this is test"是单字节字符串,不能强制copy到m_szBuf2.因为m_sz_Buf2是宽字节字符串
要通过wcstombs函数转化或宏转化后负值
字符串数组是不应该经过简单的类型转换就传递出去的,因为BSTR好像是宽字符,你要这么转换
*pVal = _com_util::ConvertStringToBSTR(user.name);
要把 comsupp.lib 假如到工程中
#include <comutil.h>
呵呵我是天地有情 蹭分来了
STDMETHODIMP CMagic::get_strBuf2(BSTR *pVal)
{
CString str("test");
*pVal = str.AllocSysString();
return S_OK;
}