增加到200分,好像没有人能回答这个问题啊??? ( 积分: 200 )

  • 主题发起人 主题发起人 bbser
  • 开始时间 开始时间
B

bbser

Unregistered / Unconfirmed
GUEST, unregistred user!
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3065091
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3065091
 
Uses ActiveX;

initialization
CoInitialize(Nil);

finalization
CoUnInitialize;
 
同意xianguo,在线程文件中要先初始化com库才能调用webservice的
 
老兄,你这招不行,如果我将线程中的Synchronize去掉,则错误依旧,还是最初的那个coInitialize异常。Web Service文件加上了你的东东,如下:
unit MagicService;

interface

uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns, ActiveX;

type

MagicServiceSoap = interface(IInvokable)
['{E04062C9-6555-17A8-EC63-780D5C2FFD89}']
function GetRand(const userName: WideString): WideString; stdcall;
end;

function GetMagicServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): MagicServiceSoap;


implementation

function GetMagicServiceSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): MagicServiceSoap;
const
defWSDL = 'http://202.196.64.86/service/MagicService.asmx?wsdl';
defURL = 'http://202.196.64.86/service/MagicService.asmx';
defSvc = 'MagicService';
defPrt = 'MagicServiceSoap';
var
RIO: THTTPRIO;
begin
Result := nil;
if (Addr = '') then
begin
if UseWSDL then
Addr := defWSDL
else
Addr := defURL;
end;
if HTTPRIO = nil then
RIO := THTTPRIO.Create(nil)
else
RIO := HTTPRIO;
try
Result := (RIO as MagicServiceSoap);
if UseWSDL then
begin
RIO.WSDLLocation := Addr;
RIO.Service := defSvc;
RIO.Port := defPrt;
end else
RIO.URL := Addr;
finally
if (Result = nil) and (HTTPRIO = nil) then
RIO.Free;
end;
end;


initialization
InvRegistry.RegisterInterface(TypeInfo(MagicServiceSoap), 'http://tempuri.org/', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(MagicServiceSoap), 'http://tempuri.org/%operationName%');
InvRegistry.RegisterInvokeOptions(TypeInfo(MagicServiceSoap), ioDocument);
CoInitialize(Nil);

finalization
CoUnInitialize;

end.
 
线程文件如下:
unit ThrdGetRand;

interface

uses
Classes;

type
TThrdGetRandom = class(TThread)
private
FRandom: AnsiString;
procedure DispRandom;
protected
procedure Execute; override;
end;

implementation

uses MagicService, main;

procedure TThrdGetRandom.Execute;
begin
FRandom:=GetMagicServiceSoap.GetRand('pppp');
Synchronize(DispRandom);
end;

procedure TThrdGetRandom.DispRandom;
begin
Form1.edtRandom.Text:=FRandom;
end;

end.
 
Main文件如下:
unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, imGauge;

type
TForm1 = class(TForm)
Button1: TButton;
edtRandom: TEdit;
imGauge1: TimGauge;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses MagicService, ThrdGetRand;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Thrd:TThrdGetRandom;
begin
imGauge1.IsAnimating:=True;
Thrd:=TThrdGetRandom.Create(True);
Thrd.FreeOnTerminate:=True;
Thrd.Resume;

end;

end.
 
将这几句加到线程中也不行,仍是一样的异常,新线程文件如下:
unit ThrdGetRand;

interface

uses
Classes,ActiveX;

type
TThrdGetRandom = class(TThread)
private
FRandom: AnsiString;
procedure DispRandom;
protected
procedure Execute; override;
end;

implementation

uses MagicService, main;

procedure TThrdGetRandom.Execute;
begin
FRandom:=GetMagicServiceSoap.GetRand('pppp');
Synchronize(DispRandom);
end;

procedure TThrdGetRandom.DispRandom;
begin
Form1.edtRandom.Text:=FRandom;
end;

initialization
CoInitialize(Nil);

finalization
CoUnInitialize;
end.
 
我的意思是在线程里进行com库的初始化,而不是webservice。。
 
加到线程里试了,如上,错误依旧。请指点
 
在两位提示下我已经完成了。不能按照Xiangguo说的直接将那几个语句放在线程里,而是去掉initialization和finalization这两个关键字,将 CoInitialize(Nil)和CoUnInitialize放在Execute中执行。感谢!每人100分
 
多人接受答案了。
 
hehe,我意思本就是加到线程体里,可能没表达明白。。:p
 
后退
顶部