问:error C3861: “TryEnterCriticalSection”: 即使使用参数相关的查找,也未找到标识符
答:增加预处理定义:_WIN32_WINNT=0X500
问:ICE::Long类型变量不能正常输出
答:ICE::Long类型占8个字节,使用%I64d格式输出
问:JAVA客户端调用c++服务端,无论提交还是服务返回为乱码
答:编码格式不正确,在服务端增加编码处理,重新实现StringConverter,
//
StringConverterI.h
#ifndef STRING_CONVERTER_I_H
#define STRING_CONVERTER_I_H
#include < Ice / StringConverter.h >
namespace test
{
//
// UTF-8 converter for LATIN-1
//
class StringConverterI : public Ice::StringConverter
{
public:
StringConverterI();
~StringConverterI();
virtual Ice::Byte* toUTF8(const char*, const char*, Ice::UTF8Buffer&) const;
virtual void fromUTF8(const Ice::Byte*, const Ice::Byte*, std::string&) const;
};
}
#endif
// StringConverterI.cpp
#include " StringConverterI.h "
#include < Ice / LocalException.h >
using namespace std;
test::StringConverterI::StringConverterI()
{
}
test::StringConverterI:: ~ StringConverterI()
{
}
Ice::Byte *
test::StringConverterI::toUTF8( const char * sourceStart, const char * sourceEnd, Ice::UTF8Buffer & buffer) const
{
size_t inputSize = static_cast<size_t>(sourceEnd - sourceStart);
size_t chunkSize = std::max<size_t>(inputSize, 6);
size_t outputBytesLeft = chunkSize;
Ice::Byte* targetStart = buffer.getMoreBytes(chunkSize, 0);
size_t offset = 0;
for(unsigned int i = 0; i < inputSize; ++i)
{
unsigned char byte = sourceStart[i];
if(byte <= 0x7F)
{
if(outputBytesLeft == 0)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart + chunkSize);
offset = 0;
}
targetStart[offset] = byte;
++offset;
--outputBytesLeft;
}
else
{
if(outputBytesLeft <= 1)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart + chunkSize - outputBytesLeft);
offset = 0;
}
targetStart[offset] = 0xC0 | ((byte & 0xC0) >> 6);
targetStart[offset + 1] = 0x80 | (byte & 0x3F);
offset += 2;
outputBytesLeft -= 2;
}
}
return targetStart + offset;
}
void
test::StringConverterI::fromUTF8( const Ice::Byte * sourceStart, const Ice::Byte * sourceEnd,
string & target) const
{
size_t inSize = static_cast<size_t>(sourceEnd - sourceStart);
target.resize(inSize);
unsigned int targetIndex = 0;
unsigned int i = 0;
while(i < inSize)
{
if((sourceStart[i] & 0xC0) == 0xC0)
{
if(i + 1 >= inSize)
{
throw Ice::StringConversionException(__FILE__, __LINE__, "UTF-8 string source exhausted");
}
target[targetIndex] = (sourceStart[i] & 0x03) << 6;
target[targetIndex] = target[targetIndex] | (sourceStart[i + 1] & 0x3F);
i += 2;
}
else
{
target[targetIndex] = sourceStart[i];
++i;
}
++targetIndex;
}
target.resize(targetIndex);
}
#ifndef STRING_CONVERTER_I_H
#define STRING_CONVERTER_I_H
#include < Ice / StringConverter.h >
namespace test
{
//
// UTF-8 converter for LATIN-1
//
class StringConverterI : public Ice::StringConverter
{
public:
StringConverterI();
~StringConverterI();
virtual Ice::Byte* toUTF8(const char*, const char*, Ice::UTF8Buffer&) const;
virtual void fromUTF8(const Ice::Byte*, const Ice::Byte*, std::string&) const;
};
}
#endif
// StringConverterI.cpp
#include " StringConverterI.h "
#include < Ice / LocalException.h >
using namespace std;
test::StringConverterI::StringConverterI()
{
}
test::StringConverterI:: ~ StringConverterI()
{
}
Ice::Byte *
test::StringConverterI::toUTF8( const char * sourceStart, const char * sourceEnd, Ice::UTF8Buffer & buffer) const
{
size_t inputSize = static_cast<size_t>(sourceEnd - sourceStart);
size_t chunkSize = std::max<size_t>(inputSize, 6);
size_t outputBytesLeft = chunkSize;
Ice::Byte* targetStart = buffer.getMoreBytes(chunkSize, 0);
size_t offset = 0;
for(unsigned int i = 0; i < inputSize; ++i)
{
unsigned char byte = sourceStart[i];
if(byte <= 0x7F)
{
if(outputBytesLeft == 0)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart + chunkSize);
offset = 0;
}
targetStart[offset] = byte;
++offset;
--outputBytesLeft;
}
else
{
if(outputBytesLeft <= 1)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart + chunkSize - outputBytesLeft);
offset = 0;
}
targetStart[offset] = 0xC0 | ((byte & 0xC0) >> 6);
targetStart[offset + 1] = 0x80 | (byte & 0x3F);
offset += 2;
outputBytesLeft -= 2;
}
}
return targetStart + offset;
}
void
test::StringConverterI::fromUTF8( const Ice::Byte * sourceStart, const Ice::Byte * sourceEnd,
string & target) const
{
size_t inSize = static_cast<size_t>(sourceEnd - sourceStart);
target.resize(inSize);
unsigned int targetIndex = 0;
unsigned int i = 0;
while(i < inSize)
{
if((sourceStart[i] & 0xC0) == 0xC0)
{
if(i + 1 >= inSize)
{
throw Ice::StringConversionException(__FILE__, __LINE__, "UTF-8 string source exhausted");
}
target[targetIndex] = (sourceStart[i] & 0x03) << 6;
target[targetIndex] = target[targetIndex] | (sourceStart[i + 1] & 0x3F);
i += 2;
}
else
{
target[targetIndex] = sourceStart[i];
++i;
}
++targetIndex;
}
target.resize(targetIndex);
}
在初始化的时候,将初始化数据设置为:
Ice::InitializationData initData;
initData.stringConverter = new test::StringConverterI;
initData.stringConverter = new test::StringConverterI;
然后在java端,将得到的数据做如下处理:
TAHolder ta
=
new
TAHolder();
t.DoWork(ta);
byte [] b;
b = ta.value.s.getBytes( " ISO-8859-1 " ); // 中间用ISO-8859-1过渡
String name1 = new String(b, " GB2312 " );
t.DoWork(ta);
byte [] b;
b = ta.value.s.getBytes( " ISO-8859-1 " ); // 中间用ISO-8859-1过渡
String name1 = new String(b, " GB2312 " );