Solved

Delphi Dll in c#

Posted on 2004-04-22
10
3,589 Views
Last Modified: 2007-12-19
Hi,

I have a dll written in Delphi that I am trying to import into C#. But I get an exception each the program tries to access the dll. Here is my dll source code (in Delphi):

nit ActivationCodeMain;

interface
uses
  classes,
  sysutils,
  activation;

function getActivationCode(siteId: WideString; params: WideString; var ActivationCode: WideString): integer; stdcall;

implementation

function getActivationCode(siteId: WideString; params: WideString; var ActivationCode: WideString): integer; stdcall;
begin
  result := 0;
  ActivationCode := GenerateActivationCode(SiteId, 0);
  if (ActivationCode = '') then
  begin
    result := 1;
  end;
end;

end.

and here is my DllImport clause in C#:

[DllImport("ActivationCode.dll", CallingConvention=CallingConvention.StdCall)]
     public static extern System.Int32 getActivationCode(string a,string b,ref string c);

and I call this way:

string test = "";
System.Int32 n = getActivationCode("AAAAAAAAAA","BBBBBBBBB",ref test);

Does anybody have a solution?

Thanks.
0
Comment
Question by:dabitbol
10 Comments
 
LVL 8

Expert Comment

by:RomanPetrenko
Comment Utility
1. Use StringBuilder class as third parameter in C#
2. Instead Int32 use IntPtr

Is it works now?
0
 
LVL 3

Author Comment

by:dabitbol
Comment Utility
Would you mind giving an example please. I am not that familiar with StringBuilder
0
 
LVL 8

Expert Comment

by:RomanPetrenko
Comment Utility
[DllImport("ActivationCode.dll", CallingConvention=CallingConvention.StdCall)]
public static extern IntPtr getActivationCode(string a,string b,System.Text.StringBuilder c);

and c.ToString() will give you an Activation Code string
0
 
LVL 8

Expert Comment

by:RomanPetrenko
Comment Utility
Look here for "Call Unmanaged DLLs from C#"
http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
0
 
LVL 3

Author Comment

by:dabitbol
Comment Utility
Thanks, But now I have a System.NullReferenceException with:

Additional information: Object reference not set to an instance of an object

Here is my C# Code:

[DllImport("ActivationCode.dll", EntryPoint="getActivationCode", CallingConvention=CallingConvention.StdCall)]
      public static extern System.IntPtr getActivationCode(string a,string b,StringBuilder c);

...

StringBuilder buffer = new StringBuilder("");
int i = 1;
i = getActivationCode("2058126983","",buffer).ToInt32(); <-- Error on this line
button1.Text = buffer.ToString();
...

And I know this function works, I tried the wrapper in Delphi.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 500 total points
Comment Utility
You are exporting an unicode function. Make sure to add the CharSet=CharSet.Unicode to the marshal attribute (just after CallingConvention).
0
 
LVL 3

Author Comment

by:dabitbol
Comment Utility
Thanks Everybody for your help, it now works. I changed in my Delphi code widestrings to PChars and I use StringBuilder in .net
0
 

Expert Comment

by:erdosa
Comment Utility
Hi

I want use dll (Delphi 7) in c#

example

delphi

procedure GetNev(s:PChar);export;
 begin
  ShowMessage(s)   ;
 end;
exports GetNev;


c#

[DllImport( "project1.dll", CallingConvention = CallingConvention.StdCall, CharSet=CharSet.Unicode )]
public static extern void GetNev(string s);

GetNev("hello");


problem:
  Message is: */-/_:?  (not "Hello")

what is tha problem???

Thenks Erdosa
0
 
LVL 3

Author Comment

by:dabitbol
Comment Utility
You need first to write your call in delphi this way:

procedure GetNev(s:PChar);stdcall;
 begin
  ShowMessage(s)   ;
 end;

and call it this way in c#

[DllImport( "project1.dll", CallingConvention = CallingConvention.StdCall, CharSet=CharSet.Ansi )]
public static extern void GetNev(string s);


good luck
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

12 Experts available now in Live!

Get 1:1 Help Now