[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Delphi Multithread error

Posted on 2013-05-22
4
Medium Priority
?
816 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 28

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 500 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 500 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

649 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