Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Managed C# DLL in Delphi



Permlink Replies: 3 - Last Post: Aug 17, 2017 3:38 PM Last Post By: Bismark Appah
Fred Smith

Posts: 81
Registered: 12/4/15
Managed C# DLL in Delphi
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2015 6:45 AM
Hi,
If I compile this C# into a Yahoo.DLL, how can included that DLL into a Delphi project ?
The C# code works and compiles without any errors, I'm interested in the steps I need to take to include it in a Delphi project.
Thanks for your help.

I'm using:
Delphi Studio 10 Seattle
Visual Studio 2015 for the C# code

//


C# code
using System.Net;
using System.Globalization;

public class YahooAPI
{
private static readonly WebClient webClient = new WebClient();

private const string UrlTemplate = "http://finance.yahoo.com/d/quotes.csv?s={0}&f={1}";

private static double ParseDouble(string value)
{
return double.Parse(value.Trim(), CultureInfo.InvariantCulture);
}

private static string[] GetDataFromYahoo(string symbol, string fields)
{
string request = string.Format(UrlTemplate, symbol, fields);

string rawData = webClient.DownloadString(request).Trim();

return rawData.Split(',');
}

public double GetBid(string symbol)
{
return ParseDouble(GetDataFromYahoo(symbol, "b3")[0]);
}

public double GetAsk(string symbol)
{
return ParseDouble(GetDataFromYahoo(symbol, "b2")[0]);
}

public string GetCapitalization(string symbol)
{
return GetDataFromYahoo(symbol, "j1")[0];
}

public string[] GetValues(string symbol, string fields)
{
return GetDataFromYahoo(symbol, fields);
}
}

Edited by: Fred Smith on Nov 30, 2015 6:56 AM
William Egge

Posts: 98
Registered: 11/29/06
Re: Managed C# DLL in Delphi
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 3, 2015 6:33 AM   in response to: Fred Smith in response to: Fred Smith
.Net "dll"'s are not DLL's - they are byte code files and require a .Net host to interpret and run them.
Danny Heijl

Posts: 8
Registered: 8/28/00
Re: Managed C# DLL in Delphi
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 7, 2015 6:00 AM   in response to: Fred Smith in response to: Fred Smith
Fred Smith wrote:
Hi,
If I compile this C# into a Yahoo.DLL, how can included that DLL into a Delphi project ?
The C# code works and compiles without any errors, I'm interested in the steps I need to take to include it in a Delphi project.
Thanks for your help.

You can expose the C# class as a COM object or you could use "unmanaged exports" (https://www.nuget.org/packages/UnmanagedExports) in your C# project to expose C# code as a "regular dll".

Danny
Bismark Appah

Posts: 24
Registered: 3/13/15
Re: Managed C# DLL in Delphi
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 16, 2017 11:24 PM   in response to: Fred Smith in response to: Fred Smith
Fred Smith wrote:
Hi,
If I compile this C# into a Yahoo.DLL, how can included that DLL into a Delphi project ?
The C# code works and compiles without any errors, I'm interested in the steps I need to take to include it in a Delphi project.
Thanks for your help.

I'm using:
Delphi Studio 10 Seattle
Visual Studio 2015 for the C# code

//


C# code
using System.Net;
using System.Globalization;

public class YahooAPI
{
private static readonly WebClient webClient = new WebClient();

private const string UrlTemplate = "http://finance.yahoo.com/d/quotes.csv?s={0}&f={1}";

private static double ParseDouble(string value)
{
return double.Parse(value.Trim(), CultureInfo.InvariantCulture);
}

private static string[] GetDataFromYahoo(string symbol, string fields)
{
string request = string.Format(UrlTemplate, symbol, fields);

string rawData = webClient.DownloadString(request).Trim();

return rawData.Split(',');
}

public double GetBid(string symbol)
{
return ParseDouble(GetDataFromYahoo(symbol, "b3")[0]);
}

public double GetAsk(string symbol)
{
return ParseDouble(GetDataFromYahoo(symbol, "b2")[0]);
}

public string GetCapitalization(string symbol)
{
return GetDataFromYahoo(symbol, "j1")[0];
}

public string[] GetValues(string symbol, string fields)
{
return GetDataFromYahoo(symbol, fields);
}
}

Edited by: Fred Smith on Nov 30, 2015 6:56 AM

Hi Fred,

You can use the .Net Runtime Library for Delphi to access the assembly (Yahoo.DLL), its types and members. There are several ways to access these assemblies using the .Net Runtime Library for Delphi:

1. You can create a dispatch attribute around the YahooAPI class in c# and also create an equivalent dispinterface in delphi just like COM, however unlike COM, the .Net Runtime Library does not require prior registration of your DLL as COM.

Example: Let look at you dll code in c#, you need to add dispID to the public members of the class and compile:

 public class YahooAPI
 {  
     private static readonly WebClient webClient = new WebClient();
  
     private const string UrlTemplate = "http://finance.yahoo.com/d/quotes.csv?s={0}&f={1}";
  
      private static double ParseDouble(string value)
     {
          return double.Parse(value.Trim(), CultureInfo.InvariantCulture);
     }
  
      private static string[] GetDataFromYahoo(string symbol, string fields)
     {
         string request = string.Format(UrlTemplate, symbol, fields);
  
         string rawData = webClient.DownloadString(request).Trim();
  
         return rawData.Split(',');
     }	
	 
     [Dispid(0)]
     public double GetBid(string symbol)
     {
         return ParseDouble(GetDataFromYahoo(symbol, "b3")[0]);
     }
  
     [Dispid(1)]
     public double GetAsk(string symbol)
     {
         return ParseDouble(GetDataFromYahoo(symbol, "b2")[0]);
     }
  
     [Dispid(2)]
     public string GetCapitalization(string symbol)
     {
         return GetDataFromYahoo(symbol, "j1")[0];
     }
  
     [Dispid(3)]
     public object/*string[]*/ GetValues(string symbol, string fields)
     {
         return GetDataFromYahoo(symbol, fields);
     }
 }


Now, you need to write Delphi type equivalent of the above c# code will look like this:

  _YahooAPI = dispinterface
  ['{D77959BD-C7AC-4D65-9980-A88510F776B8}']
    function GetBid(symbol : WideString) : Double; dispid 0;
    function GetAsk(symbol : WideString) : Double; dispid 1;
    function GetCapitalization(symbol : WideString) : WideString; dispid 2;
    function GetValues(symbol, fields : WideString) : OleVariant; dispid 3;
  end;


To use this methods, load the dll using TClrAssembly Class and create instance of the YahooAPI type using TClrDispatchActivator class:

var
   YahooAPI : _YahooAPI;
   Value1, Value2: Double;
   Cap: WideString;
   sArray: _StringArray;
begin
   TClrAssembly.LoadFrom('YahooAPI.dll'); //Assuming the dll is in the same location as the EXE, otherwise specify the full path.
       
   YahooAPI := _YahooAPI(TClrDispatchActivator.CreateInstance('YahooAPI')); //Full type name including the namespace.
       
   Value1 := YahooAPI.GetBid('MySymbol');
   Value2:= YahooAPI.GetAsk('MySymbol');
   Cap := YahooAPI.GetCapitalization('MySymbol');
   sArray := CoStringArray.Wrap(YahooAPI.GetValues('MySymbol', 'MyFielids');
end;


The second approach is to use the .Net Assembly/WSDL Importer tool to import the YahooAPI dll and generate delphi codes from the assemblies and types of the dll using the runtime library.

For more information about the .Net Runtime Library for Delphi, go to https://crystalnet-tech.com/RuntimeLibrary/RuntimeLibrary4Delphi.aspx

For more information about the .Net Assembly/WSDL Importer tool, go to https://crystalnet-tech.com/RuntimeLibrary/RuntimeLibUtilities.aspx

Edited by: Bismark Appah on Aug 16, 2017 11:47 PM

Edited by: Bismark Appah on Aug 17, 2017 3:36 PM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02