本人写了一个delphi连接mq server6.0的程序,并读取一个远程队列的信息(xml信息),我在本机使用mq资源管理器在该远程队列中放入测试消息后,使用delphi可以读取出正确的消息。但如果是远程客户写入的消息,我读出来只显示"MDE "。该消息使用mq资源管理器中的浏览消息可以正常看到,而且如果将该消息copy出来,再使用本地放入测试消息也可以读出。请问一下是什么问题?是字符集不对吗?如果是,应该如何查看对方的字符集,本地程序要如何设置?本地读信息程序如下:
// ***********************************************************************
// Step 1 - connect to connection manager
// ***********************************************************************
IF Qmanager='' THEN
fSetMsg('Opening connection to default connection manager',3)
ELSE
fSetMsg('Opening connection to '+QMANAGER+'default connection manager',3);
MQCONN(Pchar(QMANAGER), // Connection manager name
HConn, // Connection Handle
CompCode, // Completition Code
CReason); // Reason
if (CompCode <> MQCC_OK) then begin
fSetMsg('MQCONN failed with CompCode:'+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
Exit;
end
else begin
fSetMsg('Connection manager connection opened',3);
end;
// ***********************************************************************
// Step 2 - Open Queue
// ***********************************************************************
// reset object descriptor structure to defaults
SetMQOD_DEFAULT(od);
// copy queue name string to object structure
StrPLCopy(od.ObjectName, QueueName, SizeOf(od.ObjectName));
// Set connection options
O_options := MQOO_INPUT_AS_Q_DEF // open queue for input - read, get
+ MQOO_OUTPUT // open queue for output - write, put
+ MQOO_FAIL_IF_QUIESCING; // but not if Message Queue Manager is in stopping state
// Finally open queue
fSetMsg('Opening queue: "'+QueueName+'"',3);
MQOPEN(Hconn, // connection handle
od, // object descriptor for queue
O_options, // open options
Hobj, // object handle
OpenCode, // completion code
Reason); // reason code
// Check the results of openning action
if (Reason <> MQRC_NONE) then begin
fSetMsg('MQOPEN ended with reason code CompCode:'+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
//WriteCommonError('MQOPEN ended with reason code ', OpenCode, Reason);
Exit;
end;
if (OpenCode = MQCC_FAILED) then begin
fSetMsg('Unable to open queue for input or output '+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
Exit;
end;
fSetMsg('Queue opened',3);
// ***********************************************************************
// Step 4 - Read messages from queue in loop
// ***********************************************************************
fSetMsg('Receive messages in loop',3);
fSetMsg('If programm can not read message in 15 seconds, loop is finished',3);
// reset message descriptor structure to defaults
SetMQMD_DEFAULT(md);
// reset Get Message Option structure to defaults
SetMQGMO_DEFAULT(gmo);
//gmo.Version = MQGMO_VERSION_2; // Avoid need to reset Message
//gmo.MatchOptions = MQMO_NONE; // ID and Correlation ID after
// every MQGET
gmo.Options := MQGMO_WAIT // wait for new messages
+ MQGMO_CONVERT; // convert if necessary
gmo.WaitInterval :=5000;
//strtoint(ucommon.ReadxtmisIniString('INIT','waitInterval','5000')); // 15 seconds limit for waiting
// assume that everything is OK with - see loop condition
CompCode := MQCC_OK;
// how much bytes my receive buffer can handle
// note - in this application my send and receive buffers are the same
buffer:='';
buflen :=8190;
messlen:=8190;
// enter loop in which programm receives messages from queue
while (CompCode <> MQCC_FAILED) do begin
// before message is received you always must
// reset this fields in Messsage Descriptor structure
move(MQMI_NONE, md.MsgId, SizeOf(md.MsgId));
move(MQCI_NONE, md.CorrelId, SizeOf(md.CorrelId));
// md.Encoding := MQENC_NATIVE;
md.Encoding := ucommon.ReadxtmisIniInteger('INIT','Encoding',546);//1381;546; --这个地代码是根据对方发送过来的消息进行了修改,但不起作用。
// md.CodedCharSetId := MQCCSI_Q_MGR;
md.CodedCharSetId := ucommon.ReadxtmisIniInteger('INIT','CodedCharSetId',1381);//1381; --这个地代码是根据对方发送过来的消息进行了修改,但不起作用。
MQGET(Hconn, // connection handle
Hobj, // object handle
md, // message descriptor
gmo, // get message options
buflen, // buffer length
@buffer, // message buffer
messlen, // message length
CompCode, // completion code
Reason); // reason code
if (CompCode <> MQCC_FAILED) then
begin
fSetMsg('Received message: '+buffer,3); ---在这个地方输出buffer=MDE
//导入相应的文件数据
if buffer='' then
begin
fsetMsg('无数据!',1);
// exit;
end
else
Begin
doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument;
doc.loadXML(buffer);
//增加数据luxx
TRY
acXmlToDb.OnExecute(sender);
i:=i+1;
EXCEPT
doc.save(ExtractFilePath(application.ExeName)+'\errData\err'+formatdatetime('yyyymmddhnnss',now)+'.xml');
fSetMsg('增加数据失败!!'+buffer,1);
END;
//
end;
end
else begin
if (Reason = MQRC_NO_MSG_AVAILABLE) then begin
fSetMsg('No more messages'+inttostr(CompCode)+'Reason:'+inttostr(Reason),3);
end
else if (Reason <> MQRC_NONE) then begin
fSetMsg('Get message failed! CompCode:'+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
Exit;
end;
end;
end;
// ***********************************************************************
// Step 5 - Close my connection to queue
// ***********************************************************************
if (OpenCode <> MQCC_FAILED) then begin
C_options := 0; // no close options
MQCLOSE(Hconn, // connection handle
Hobj, // object handle
C_options, // close options
CompCode, // completion code
Reason); // reason code
if (Reason <> MQRC_NONE) then begin
fSetMsg('MQCLOSE ended with reason code '+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
end
else begin
fSetMsg('Queue closed',3);
end;
end;
// ***********************************************************************
// Step 6 - Close my connection to queue manager
// ***********************************************************************
MQDISC(Hconn, // connection handle
CompCode, // completion code
Reason); // reason code
if (Reason <> MQRC_NONE) then begin
fSetMsg('MQDISC ended with reason code '+inttostr(CompCode)+'Reason:'+inttostr(Reason),1);
end
else begin
fSetMsg('Connection to Queue Manager closed',3);
end;
fSetMsg('共导入'+InttoStr(i)+'条数据记录!',3);
[本帖最后由 luxx 于 2009-4-29 15:48 编辑]