Solved

Using C# dll in Delphi 7

Posted on 2009-05-18
9
4,148 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

816 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

9 Experts available now in Live!

Get 1:1 Help Now