Solved

Delphi Dll in c#

Posted on 2004-04-22
10
3,673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 8

Expert Comment

by:RomanPetrenko
ID: 10893785
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
ID: 10893801
Would you mind giving an example please. I am not that familiar with StringBuilder
0
 
LVL 8

Expert Comment

by:RomanPetrenko
ID: 10893953
[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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

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

Author Comment

by:dabitbol
ID: 10894309
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
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10895376
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 500 total points
ID: 10897240
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
ID: 10903441
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
ID: 11115981
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
ID: 11116999
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.NET Enums [Flags] & Bitwise Design Question 6 51
How to repeat the data 4 42
c# ftp code 3 53
async questions 5 46
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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