Solved

Using C# dll in Delphi 7

Posted on 2009-05-18
9
4,122 Views
Last Modified: 2012-05-07
I have been supplied with a dll and tlb written in C# called comwrapper

I registered the dll using

regasm  comwrapper

I opened a new project and used the import type library option to import this and create a comwrapper_tlb.bas file. IT has 2 compnents - Twrapper and Tchannel

This has been added to my project and  my test program has the following code

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,comwrapper_tlb;

.
.
.
implementation

{$R *.dfm}

var
  ocwrap: Twrapper;
.
.
    ocwrap:=twrapper.create(self);
.
.

However whenever I try to access functions or procedures in the tlb I get a compile error

eg

   ocwrap.inittimechannel(s);

Gives "Undeclared Identifier: 'inittimechannel'"

 The comwrapper_tlb.bas has this function declared
.
.
// *********************************************************************//
// Interface: Dispatch_OcWrapper
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {80C594DF-DC34-3C8C-AB52-C1FD15BA1361}
// *********************************************************************//
  Dispatch_OcWrapper = interface(IDispatch)
    ['{80C594DF-DC34-3C8C-AB52-C1FD15BA1361}']
    function InitTimeChannel(const sChannelName: WideString): WordBool; safecall;
    procedure Connect; safecall;
    procedure Disconnect; safecall;
    function GetData: WideString; safecall;
    function SetStartTimeIndex(startTime: Double): WordBool; safecall;
  end;

// *********************************************************************//
// DispIntf:  Dispatch_OcWrapperDisp
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {80C594DF-DC34-3C8C-AB52-C1FD15BA1361}
// *********************************************************************//
  Dispatch_OcWrapperDisp = dispinterface
    ['{80C594DF-DC34-3C8C-AB52-C1FD15BA1361}']
    function InitTimeChannel(const sChannelName: WideString): WordBool; dispid 1;
    procedure Connect; dispid 2;
    procedure Disconnect; dispid 3;
    function GetData: WideString; dispid 4;
    function SetStartTimeIndex(startTime: Double): WordBool; dispid 5;
  end;
.
.

Other factors.

We have a VB6 running using the same TLB, although it won't run in the IDE only as an exe.
When the objectname is typed the popup completion list includes the functions as listed above.

The tlb file is described as

C:\Program Files\Borland\Delphi7\Imports\COMWrapper_TLB.pas

in the title of the code list. However I can not see that file in windows explorer.
Show hidden files is on, hide protected operating system files is off.

The dpk file itself is supposed to be in C:\Program Files\Borland\Delphi7\lib but again is not visible in Explorer.

Any help would really be appreciated.
0
Comment
Question by:Kymberley
9 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24413441
0
 

Author Comment

by:Kymberley
ID: 24413703
Nothing really helpful there.

I did try tlibimport.exe to recreate the _tlb.pas file.  I then replace the original file with the new one.

This caused the error to occur on the declaration of the Twrapper type
0
 

Author Comment

by:Kymberley
ID: 24413784
When I switched back to the _tlb.pas in the delphi 7 subdirectory  that error goes away but I can't reference  the functions.

Interesting sideline. If I try to add the comwrapper.bpl or the compwrapper_tlb to pas files to the project, they do appear in the open file dialog, but not in explorer itself.
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 24454706
First Make sure there's no problems with your com object...import it into a new C# project and try to call it...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Kymberley
ID: 24464140
The dll/tlb work with VB6 on my PC and i was supplied with a C# exe that was the original demo.

THe problem seems to be setting up the references in Delphi 7, assuming that this is possible in the first place.
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 24466855
Calling it through VB6 tells us nothing...I don't know if your calling it through a variant using the IDispatch...You need to import into a highly typed language like Delphi or C#...or a .Net language...if all else fails do it like VB6 and use a variant and call it through the IDispatch...
0
 

Author Comment

by:Kymberley
ID: 24542754
Sorry for the apparent lack of interest but I have left the US and am now back home in Australia and finally getting back on to this problem.

the VB code starts like this

Public ocWrap As Dispatch_OcWrapper
.
    Set ocWrap = New Wrapper
.
   ocWrap.InitTimeChannel("DMEA")
.
   ocWrap.Connect

The connect command then brings up a dialog that is embedded in the dll.


0
 

Accepted Solution

by:
Kymberley earned 0 total points
ID: 24666636
It seems the answer to my problem was to use Managed VCL.  I found this out myself and was helped by the developer of that product.
0
 

Expert Comment

by:charleslogan
ID: 25307564
You can use Cross-Talk Project here

http://www.atozed.com/CrossTalk/Blog/20090503.en.aspx

CrossTalk is a project for Delphi that allows native Delphi code to consume .NET code in a very transparent way, very much like referencing a package. Select .NET classes you want to use from Delphi, and that's it. Very much like in C#, or VB.NET. Or at least as close as possible without Delphi being managed code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 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

15 Experts available now in Live!

Get 1:1 Help Now