我写一个简单的COM,如下:
unit UnitTesting;
{$WARN SYMBOL_PLATFORM OFF}interfaceuses
  ActiveX, Mtsobj, Mtx, ComObj, PrjTesting_TLB, StdVcl;type
  Ttesing = class(TMtsAutoObject, Itesing)
  protected
    function hello: WideString; safecall;  end;implementationuses ComServ;function Ttesing.hello: WideString;
begin
   result:='HelloWorld';
end;initialization
  TAutoObjectFactory.Create(ComServer, Ttesing, Class_tesing,
    ciMultiInstance, tmApartment);
end.然后我写一个DLL程序,使用COM中的方法。最后前端调用。DLL程序如下:
function getString():string;stdcall;
var
  proxyTest:Itesing;
begin
  coInitialize(nil);
  try
  proxyTest:=Cotesing.Create ;
  result:=proxyTest.hello;
  //finally
  //CoUninitialize;end;
exports
    getString;客户端引用DLL,直接显示返回的字符串。但是如果我在DLL中把coInitialize和CoUninitialize同时写上,就出现错误"Statement expected but 'Exports' found"
如果我只留下coInitialize,能够返回结果,但是执行完后就显示无效的指针操作,如果都不写也错误。
请问我如何写呢?

解决方案 »

  1.   

    function getString():string;stdcall;
    var
      proxyTest:Itesing;
    begin
      coInitialize(nil);
      try
        proxyTest:=Cotesing.Create ;
        result:=proxyTest.hello;
      finally
        CoUninitialize;
      end;///////////////end;
    exports
      

  2.   

    我就是这样写的,编译时产生错误"Statement expected but 'Exports' found",还有别的原因吗?
      

  3.   

    你的proxyTest是什么东东,创建它的实例了没有?
      

  4.   

    result:=proxyTest.hello;去掉看看还会不会出错,如果不出错就有proxyTest没创建它的实例
      

  5.   

    写上,并且
    你用的是string类型uses 中加上shareMem
    { Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }
      

  6.   

    实例我已经创建了,在这句话的前面,proxyTest:=Cotesing.Create ;里面什么也没有作,只有一个方法返回一个HELLOWORLD的字符。我试了还是不行。奇怪?
      

  7.   

    ljmanage(过客) 我把shareMem加上,编译后执行还是原来的错误,“invalid pointer operatation”
      

  8.   

    如果我写为function getString():string;stdcall;
    var
      proxyTest:Itesing;
    begin
      coInitialize(nil);
      //try
      proxyTest:=Cotesing.Create ;
      result:=proxyTest.hello;  {finally
        CoUninitialize;
      end;
      }
    end;
    客户端可以接收到返回的字符HELLOWORLD,执行完后,仍然是无效的操作(由于没有释放资源)
      

  9.   

    var
      proxyTest:Itesing;
    begin
      coInitialize(nil);
      try
      {proxyTest:=Cotesing.Create ;
      result:=proxyTest.hello;}//去掉这两句看看还没有错误,如果没就是proxyTest.hello有问题
      result := 'aa';  finally
        CoUninitialize;
      end;
      

  10.   

    如果在DLL中只是对字符操作不会出现错误,我现在是要在DLL中使用COM。那个COM方法没有问题,我测试过了。因为DELPHI在DLL中调用COM出现错误(好像是BUG),所以我才用coInitialize和CoUninitialize。
      

  11.   

    我在公司的机器上只作coInitialize不作CoUninitialize,结果正确没有任何错误消息提示。但是在家里的机器上就不行。就这样吧,有问题在说!
    结帖