Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple DLL 1

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I need an example to create a basic DLL called from C# with just one function with 4 parameters. The 4 parameters will be 2 string values, one integer, and one boolean, and will return a boolean result. I need just a simple example to code the DLL in Delphi and hopefully also an example how to call it from C#. Please keep it simple.


JD Solutions
 
Code:
library mydll;

uses
   SysUtils, Classes, etc...

{$R *.res}

function myfunction(s1, s2: PChar; I1: Integer; B1:  BOOL):  BOOL; stdcall;
var
   Sa, Sb:  String;
begin
   RESULT := FALSE;
   Sa := s1; //if you prefer to work with strings internally, simply assign S1 and S2 to strings
   Sb := s2; //if not, continue with S1, S2
   {...}
   IF (..) THEN
      RESULT := TRUE;
end;

exports
   myfunction;

begin

end.
What are the valid range of values for I1?
You may want to change it to:
BYTE: 0 to 255
WORD: 0 to 65535
LONGINT/INTEGER: -2,147,483,648 to 2,147,483,647
INT64: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

You can try to use the native Delphi Boolean instead of BOOL, but you may have problems using the function from other languages, like C# as you mentioned.

The main problem with dll's created with delphi is passing strings to or from a dll. Since you are passing in two strings, notice the function is expecting two PChars.

As far as how you call it from C#, maybe someone else can assist there.
 
Thanks, that helped. DLL is going to be a mediator between my previously built delphi projects and a website built in ASP.NET in C#. I know nothing about C#, and that's another thing I will need to figure out. Most of my projects are in regular string, integer, etc. This would be my first DLL, as is also my first C# project. I will eventually make an API on both ends.

JD Solutions
 
When creating the DLL, you can pick a DLL as the type of project you are creating. That will get the structure of the DLL established automatically by delphi.
 
I got everything done and plugged in, but I'm getting an error here. Here's the details:

JDEncr.dll built in Delphi 7:

Code:
function CompareStrings(PlainText, EncryptedText: String;
  EncType: Integer; CaseSensitive: Bool): Bool; stdcall;

And at the end:

Code:
exports
   CompareStrings;

DLL works perfect when I call it from Delphi:

Declaration:

Code:
function CompareStrings(PlainText, EncryptedText: String;
  EncType: Integer; CaseSensitive: Bool):  Bool; stdcall;

Implementation:

Code:
function CompareStrings(PlainText, EncryptedText: String;
  EncType: Integer; CaseSensitive: Bool):  Bool;  external 'JDEncr';

Now I try to use the same DLL from ASP.NET 4.0 in C#:

Code:
    public class JDEncr
    {
        [DllImport("JDEncr.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]

        public static extern bool CompareStrings(String PlainText, String EncryptedText, int EncType, bool CaseSensitive);

    }

Then I use that function:

Code:
bool P = JDEncr.CompareStrings(pwd, drAuth["Password"].ToString(), 1, true);

However, I get the following error:


"Unable to load DLL 'JDEncr.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"


What am I doing wrong here?

JD Solutions
 
Remember that the delphi STRING type is not equivalent to what other languages. That's why it needs to be declared as a PChar in the DLL, which is a pointer to a string.

Code:
function CompareStrings(PlainText, EncryptedText: PChar;
  EncType: Integer; CaseSensitive: Bool): Bool; stdcall;

That will require that in your delphi program you convert your string to a PChar before passing it in.

Not sure if that is the basis of your problem, but it may be.
 
Update: I finally got it working :)

The DLL had to be in the website's bin folder, not in the root folder.

Everything works perfectly, thank you!


JD Solutions
 
I also did have to change Delphi's String to PChar. I'm using it as a regular String in C# however.

JD Solutions
 
Ok, I have done quite a bit with making methods in this DLL to use in the C# Web Application, which works just fine. Now, I need to know how to create a simple Object in this DLL and use this object class in the C# Application. In other words, I have created the following object in this DLL:

Code:
  TTestObject = class(TObject)
    String1: PChar;
    String2: PChar;
    Int1: Integer;
    Int2: Integer;
    Bool1: Bool;
    Bool2: Bool;
  end;

Now, how do I use this object from the C# Application?

(Perhaps I should be posting this in another forum?)



JD Solutions
 
You can make an activeX library and that can be imported without any problem in other environments (like C#, C++, VB, ...)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top