Solved

Delphi Multithread error

Posted on 2013-05-22
4
780 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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