Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Delphi Across Platforms

How do I make a DLL in Delphi to use in C#?
Posted: 12 Dec 10 (Edited 2 Jan 11)

This is what I've learned today making a DLL in Delphi 7 to use in an ASP.NET 4.0 Website in C#. I'm brand new to C# and am basically teaching myself, all my previous knowledge has been in just Delphi and SQL.

This DLL is specifically one single function called to validate an encrypted string using one of my pre-built encryption objects in Delphi. I'm building a website which utilizes a SQL database from a larger Delphi package of mine, which I have specific encryption techniques I needed to use in the website.

Some of the code is specific to my project, but you can clear the function and change it accordingly.

Delphi 7: JDEncr.dll

Create a new DLL by going to File > New > Other... > New Tab > DLL Wizard

CODE


library JDEncr;

uses
  SysUtils,
  Classes,
  Windows,
  StrUtils,
  Encryption in '..\JDCommon\Encryption.pas';
  //Pre-made encryption unit

{$R *.res}

//Note: use PChar and not String, and Bool, not Boolean
//stdcall goes at the end of each declaration

function CompareStrings(PlainText, EncryptedText: PChar;
  EncType: Integer; CaseSensitive: Bool): Bool; stdcall;
var
  S: String;
  Enc: TEncType;
begin
  Enc:= TEncType.Create(EncType, 1);
  try
    Result := False;
    S:= DecryptString(EncryptedText, Enc);
    if CaseSensitive then begin
      if S = PlainText then
        Result:= True;
    end else begin
      if UpperCase(S) = UpperCase(PlainText) then
        Result:= True;
    end;
  finally
    Enc.Free;
  end;
end;

//exports section goes at end of unit before the begin/end.
//List everything you want to export, just like in the uses
exports
   CompareStrings;

begin

end.

Save this DLL in the 'bin' folder of the website's project.

C#: Class JDEncr.cs

In Visual Studio 2010, right-click the project's root in the Solution Explorer > Add > New Item... > C Sharp > Web > Class

Reminder: C# is case-sensitive

CODE


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
//use InteropServices to use DllImport

namespace JDWeb
{
    public class JDEncr
    {
        //Uses Ansi CharSet
        //Uses StdCall CallingConvention (like in Delphi)
        //uses String instead of PChar
        //Parameters recognize types only, and names can differ from original function in DLL
        [DllImport("JDEncr.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool CompareStrings(String PlainText, String EncryptedText, int EncType, bool CaseSensitive);
    }
}

C#: Utilization of JDEncr

CODE


  bool P = JDEncr.CompareStrings(EnteredPassword, RegisteredPassword, EncryptionType, true);

 

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close