碰到一问题,搞了一下午没有搞定,100分送上,请指点迷路...

wolf2005 2004-01-14 03:57:12
有关Com组件的问题

创建了一个ActiveX Library然后加入了一个Automation Object其CoClassName为TAM
加了一个方法,显示一个界面。通过regsvr32注册。

用以下方法调用
procedure TForm1.Button1Click(Sender: TObject);
var
xx:variant;
begin
CoInitialize(nil);
xx:=CreateOleObject('TAM'); //这里有错误提示,CoInitialize没有调用
xx.M1;
end;
能不能用以上方法调用?如果能用该怎么样正解调用?
...全文
43 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiume 2004-01-15
  • 打赏
  • 举报
回复
//////////////////
// 以下是测试
// RegSvr32 ComTest.dll
/////////////////
//****************************************
//library

library ComTest;

uses
ComServ,
ComTest_TLB in 'ComTest_TLB.pas',
test in 'test.pas' {Test: CoClass};

exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;

{$R *.TLB}

{$R *.RES}

begin
end.
//****************************************
//TLB
unit ComTest_TLB;

// ************************************************************************ //
// WARNING
// -------
// The types declared in this file were generated from data read from a
// Type Library. If this type library is explicitly or indirectly (via
// another type library referring to this type library) re-imported, or the
// 'Refresh' command of the Type Library Editor activated while editing the
// Type Library, the contents of this file will be regenerated and all
// manual modifications will be lost.
// ************************************************************************ //

// PASTLWTR : 1.2
// File generated on 2004-1-15 8:49:04 from Type Library described below.

// ************************************************************************ //
// Type Lib: D:\Program Files\Borland\Delphi7\Projects\ComTest\ComTest.tlb (1)
// LIBID: {0EE41EE6-7588-4100-ADA4-2F856E48FABF}
// LCID: 0
// Helpfile:
// HelpString: ComTest Library
// DepndLst:
// (1) v2.0 stdole, (D:\WINDOWS\system32\stdole2.tlb)
// (2) v4.0 StdVCL, (D:\WINDOWS\system32\stdvcl40.dll)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface

uses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;


// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:
// Type Libraries : LIBID_xxxx
// CoClasses : CLASS_xxxx
// DISPInterfaces : DIID_xxxx
// Non-DISP interfaces: IID_xxxx
// *********************************************************************//
const
// TypeLibrary Major and minor versions
ComTestMajorVersion = 1;
ComTestMinorVersion = 0;

LIBID_ComTest: TGUID = '{0EE41EE6-7588-4100-ADA4-2F856E48FABF}';

IID_ITest: TGUID = '{FB6AD528-63FD-4C1F-887E-F7E7F3386CD1}';
CLASS_Test: TGUID = '{E015BFD2-C53B-4EFA-BB76-D0C2B21567C4}';
type

// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
ITest = interface;
ITestDisp = dispinterface;

// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
Test = ITest;


// *********************************************************************//
// Interface: ITest
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {FB6AD528-63FD-4C1F-887E-F7E7F3386CD1}
// *********************************************************************//
ITest = interface(IDispatch)
['{FB6AD528-63FD-4C1F-887E-F7E7F3386CD1}']
procedure GetMessage(out msg: WideString); safecall;
end;

// *********************************************************************//
// DispIntf: ITestDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {FB6AD528-63FD-4C1F-887E-F7E7F3386CD1}
// *********************************************************************//
ITestDisp = dispinterface
['{FB6AD528-63FD-4C1F-887E-F7E7F3386CD1}']
procedure GetMessage(out msg: WideString); dispid 201;
end;

// *********************************************************************//
// The Class CoTest provides a Create and CreateRemote method to
// create instances of the default interface ITest exposed by
// the CoClass Test. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoTest = class
class function Create: ITest;
class function CreateRemote(const MachineName: string): ITest;
end;

implementation

uses ComObj, Unit1;

class function CoTest.Create: ITest;
begin
Result := CreateComObject(CLASS_Test) as ITest;
end;

class function CoTest.CreateRemote(const MachineName: string): ITest;
begin
Result := CreateRemoteComObject(MachineName, CLASS_Test) as ITest;
end;

end.
//****************************************
//Co

unit test;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
ComObj, ActiveX, ComTest_TLB, StdVcl;

type
TTest = class(TAutoObject, ITest)
protected
procedure GetMessage(out msg: WideString); safecall;

end;

implementation

uses ComServ;

procedure TTest.GetMessage(out msg: WideString);
begin
msg := '调用自动化对象成功';
end;

initialization
TAutoObjectFactory.Create(ComServer, TTest, Class_Test,
ciMultiInstance, tmApartment);
end.
//****************************************
// Call

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

uses ComTest_TLB;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
msg: WideString;
t: ITest;
begin
t := CreateComObject(CLASS_Test) as ITest;
t.GetMessage(msg);
ShowMessage(msg);
end;

end.
//****************************************
hiflower 2004-01-14
  • 打赏
  • 举报
回复
应该是 萧寒 所说
hahahahaanni 2004-01-14
  • 打赏
  • 举报
回复
高深,不懂!!:(
jiangshi99 2004-01-14
  • 打赏
  • 举报
回复
up
  • 打赏
  • 举报
回复
我只想接分
aiunong 2004-01-14
  • 打赏
  • 举报
回复
study
S.F. 2004-01-14
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
xx:variant;
begin
CoInitialize(nil);
xx:=CreateOleObject('TAM'); //这里有错误提示,CoInitialize没有调用
xx.M1;
end;

你调用的是正确的,但是,你的注册的com的类名错了
TAM这个名称是错误的,正确的名称应该是有个点,也就是大概如这个形式 Project1.Tam

你需要检查你的com 的类名称。
aiirii 2004-01-14
  • 打赏
  • 举报
回复
我也不是很懂, 但就我所知
CoInitialize(nil);
應該放在你的Com實現裹面的!

程序導入其類型庫
然後用 CreateOleObject 之類就可!
CDSoftwareWj 2004-01-14
  • 打赏
  • 举报
回复
我的用法是这样的

oaDoc: IoaDoc;

这样声明

try
oaDoc := CooaDoc.Create;
except
MessageDlg('Error',
mtError, [mbOk], 0);
Halt;
end;

这样建立

CDSoftwareWj 2004-01-14
  • 打赏
  • 举报
回复
随手沾了一段,不过你看明白在那里用就 ok了 试试看行不行 ^^!
hch_45 2004-01-14
  • 打赏
  • 举报
回复
用这看看,也不清楚~

CoInitializeEx(nil, COINIT_MULTITHREADED);
try

finally
CoUnInitialize;
end;
CDSoftwareWj 2004-01-14
  • 打赏
  • 举报
回复
一般是这样用的

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,adodb,
activeX;

type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
type

TAdoc=class(Tcomponent)
private
FAdocc: TAdoConnection;
public
property Adocc: TAdoconnection read FAdocc;
constructor create(AOwner: Tcomponent);override;
end;
var
adoc: TAdoc;

var
Form1: TForm1;

implementation

{$R *.DFM}

constructor TAdoc.create(AOwner: Tcomponent);
begin
inherited;
FAdocc := Tadoconnection.Create(nil); //会提示错误码'标号没有引用存储'
end;

initialization
coinitialize(nil); //!!!???????
adoc := TAdoc.create(nil);
finalization

end.
Hedonism 2004-01-14
  • 打赏
  • 举报
回复
研究研究
zth215 2004-01-14
  • 打赏
  • 举报
回复
up
荣爵 2004-01-14
  • 打赏
  • 举报
回复
555~~~

抢了我的一楼
荣爵 2004-01-14
  • 打赏
  • 举报
回复
占一楼

研究研究...
akwa 2004-01-14
  • 打赏
  • 举报
回复
unit Project1_TLB;

// ************************************************************************ //
// WARNING
// -------
// The types declared in this file were generated from data read from a
// Type Library. If this type library is explicitly or indirectly (via
// another type library referring to this type library) re-imported, or the
// 'Refresh' command of the Type Library Editor activated while editing the
// Type Library, the contents of this file will be regenerated and all
// manual modifications will be lost.
// ************************************************************************ //

// PASTLWTR : 1.2
// File generated on 2004-01-14 16:05:48 from Type Library described below.

// ************************************************************************ //
// Type Lib: H:\Delphi7\Projects\Project1.tlb (1)
// LIBID: {4718A288-A712-4EB8-A9C5-E7349D74253D}
// LCID: 0
// Helpfile:
// HelpString: Project1 Library
// DepndLst:
// (1) v2.0 stdole, (G:\WINNT\system32\STDOLE2.TLB)
// (2) v4.0 StdVCL, (G:\WINNT\System32\stdvcl40.dll)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface

uses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;


// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:
// Type Libraries : LIBID_xxxx
// CoClasses : CLASS_xxxx
// DISPInterfaces : DIID_xxxx
// Non-DISP interfaces: IID_xxxx
// *********************************************************************//
const
// TypeLibrary Major and minor versions
Project1MajorVersion = 1;
Project1MinorVersion = 0;

LIBID_Project1: TGUID = '{4718A288-A712-4EB8-A9C5-E7349D74253D}';

IID_ITAM: TGUID = '{EF330366-C42B-43CE-9092-8FF4049C3A66}';
CLASS_TAM: TGUID = '{34271C2E-400F-4972-A464-81791D266415}';
type

// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
ITAM = interface;
ITAMDisp = dispinterface;

// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
TAM = ITAM;


// *********************************************************************//
// Interface: ITAM
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {EF330366-C42B-43CE-9092-8FF4049C3A66}
// *********************************************************************//
ITAM = interface(IDispatch)
['{EF330366-C42B-43CE-9092-8FF4049C3A66}']
procedure getname; safecall;
end;

// *********************************************************************//
// DispIntf: ITAMDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {EF330366-C42B-43CE-9092-8FF4049C3A66}
// *********************************************************************//
ITAMDisp = dispinterface
['{EF330366-C42B-43CE-9092-8FF4049C3A66}']
procedure getname; dispid 201;
end;

// *********************************************************************//
// The Class CoTAM provides a Create and CreateRemote method to
// create instances of the default interface ITAM exposed by
// the CoClass TAM. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoTAM = class
class function Create: ITAM;
class function CreateRemote(const MachineName: string): ITAM;
end;

implementation

uses ComObj;

class function CoTAM.Create: ITAM;
begin
Result := CreateComObject(CLASS_TAM) as ITAM;
end;

class function CoTAM.CreateRemote(const MachineName: string): ITAM;
begin
Result := CreateRemoteComObject(MachineName, CLASS_TAM) as ITAM;
end;

end.

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧