Solved

Delphi Multithread error

Posted on 2013-05-22
4
764 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 27

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 250 total points
ID: 39189296
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
ID: 39190574
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
ID: 39209474
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
ID: 39228508
Thank you for your answers!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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