Solved

Delphi Multithread error

Posted on 2013-05-22
4
734 Views
Last Modified: 2013-06-07
I use a free Opc client: Simple OPC Client.
I used a part of it in  TThreadbe (it needs to check if a parameter was changed in the background), but there is an error message when writing to the server after "while not Terminated do"  

The error: " Project xx.exe raised exception class EIntfCastError with message 'Interface not supported' "

Code snippet:
  IOPCSyncIO = interface(IUnknown)
    ['{39C13A52-011E-11D0-9675-0020AFD8ADB3}']
    function Read(
            dwSource:                   OPCDATASOURCE;
            dwCount:                    DWORD;
            phServer:                   POPCHANDLEARRAY; {was ptr mgl}
      out   ppItemValues:               POPCITEMSTATEARRAY;
      out   ppErrors:                   PResultList): HResult; stdcall;
    function Write(
            dwCount:                    DWORD;
            phServer:                   POPCHANDLEARRAY; {was ptr mgl}
            pItemValues:                POleVariantArray;
      out   ppErrors:                   PResultList): HResult; stdcall;
  end;
0
Comment
Question by:starhu
  • 2
4 Comments
 
LVL 25

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 250 total points
Comment Utility
In Threads OnExecute event you must call OleInitialize or CoInitialize to get things work:

uses ... ActiveX;
...
procedure TYourThread.Execute;
begin
  ActiveX.CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);
  try

    ....
  except
  end;
  ActiveX.CoUninitialize;
end;

Open in new window

0
 

Author Comment

by:starhu
Comment Utility
Hello,

We tried it, but unfortunately the error is the same.

I can send you more code in private if that helps...
0
 
LVL 6

Accepted Solution

by:
House_of_Dexter earned 250 total points
Comment Utility
There is a bug in Delphi since Delphi5...

procedure MyFoo;
var
    Foo: IFoo;
begin
    Foo:= CoFoo.Create;//_addref automatically called
    Foo:= Foo as IFoo; {<--this recast causes the compiler call _release then _addref again...booom}
end;

Open in new window


procedure MyFoo;
var
    Foo, Foo2: IFoo;
begin
    Foo:= CoFoo.Create;
    Foo2:= Foo as IFoo;
end;

Open in new window


Note you really haven't given us enough info to tell us where your problem is...
Is your thread in a DLL?  If so you should not call CoInitialize in your dll...

What apartment model are you using?  Single or Multi?

Are you sure your not calling on an interface that has been ref counted down to 0 by mixing Interfaces with Objects?

Is the Com object properly Synchronized when you call on outside resources?  Is the ComObject Thread safe?

http://msdn.microsoft.com/en-us/library/ms809971.aspx
0
 

Author Closing Comment

by:starhu
Comment Utility
Thank you for your answers!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now