+ -
当前位置:首页 → 问答吧 → c++调用python模块无法取得返回值

c++调用python模块无法取得返回值

时间:2011-07-18

来源:互联网

在C++里调用python的rec模块
  pFunc = PyObject_GetAttrString(mReceiveEmailModule, "rec");
  result = PyEval_CallObject(pFunc, pArg);
  PyArg_Parse(result,"s", &iresult);
  cout << iresult << endl;
  我打印的时候发现我需要的值是空的,神马回事?

  python代码:
def rec(user,passwd,se,idx):
  global html
  global text
  try:
  try:
  server=poplib.POP3(se)
  except:
  print "link to server failed, try again"
  rec(user,passwd,se,idx)
  try:
  server.user(user)
  server.pass_(passwd)
  except:
  print "POP3 server login failed, please check the user name and password"
  return "2"
  mailCount,size=server.stat()
  print mailCount
  print size
  hdr,message,octet=server.retr(int(idx))
  mail=email.message_from_string(string.join(message,'\n'))
  subject=email.Header.decode_header(mail['subject'])[0][0]
  subcode=email.Header.decode_header(mail['subject'])[0][1]
  strr = subject
  if subcode == None:
  strr = subject
  else:
  strr = unicode(subject, subcode)
  html += "subject:\t" + strr +"\n"
  strr = email.utils.parseaddr(mail.get("from"))[1]
  html += "from:\t" + strr + "\n"
  strr = email.utils.parseaddr(mail.get("to"))[1]
  html += "to:\t" + strr + "\n"
  showmessage(mail)
  server.quit()
  print "success!"
  except Exception, e:
  print str(e)
  return "1"
  print "*************"
  return html

作者: zhyajshhz   发布时间: 2011-07-18

PyArg_Parse(result,"s", &iresult);这句不对吧,改下试试:
iresult = PyString_AsString(result);

作者: angel_su   发布时间: 2011-07-19