dabitbol
asked on
Delphi Dll in c#
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(Sit eId, 0);
if (ActivationCode = '') then
begin
result := 1;
end;
end;
end.
and here is my DllImport clause in C#:
[DllImport("ActivationCode .dll", CallingConvention=CallingC onvention. 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("AAAAAAA AAA","BBBB BBBBB",ref test);
Does anybody have a solution?
Thanks.
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(Sit
if (ActivationCode = '') then
begin
result := 1;
end;
end;
end.
and here is my DllImport clause in C#:
[DllImport("ActivationCode
public static extern System.Int32 getActivationCode(string a,string b,ref string c);
and I call this way:
string test = "";
System.Int32 n = getActivationCode("AAAAAAA
Does anybody have a solution?
Thanks.
ASKER
Would you mind giving an example please. I am not that familiar with StringBuilder
[DllImport("ActivationCode .dll", CallingConvention=CallingC onvention. StdCall)]
public static extern IntPtr getActivationCode(string a,string b,System.Text.StringBuilde r c);
and c.ToString() will give you an Activation Code string
public static extern IntPtr getActivationCode(string a,string b,System.Text.StringBuilde
and c.ToString() will give you an Activation Code string
Look here for "Call Unmanaged DLLs from C#"
http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
ASKER
Thanks, But now I have a System.NullReferenceExcept ion with:
Additional information: Object reference not set to an instance of an object
Here is my C# Code:
[DllImport("ActivationCode .dll", EntryPoint="getActivationC ode", CallingConvention=CallingC onvention. StdCall)]
public static extern System.IntPtr getActivationCode(string a,string b,StringBuilder c);
...
StringBuilder buffer = new StringBuilder("");
int i = 1;
i = getActivationCode("2058126 983","",bu ffer).ToIn t32(); <-- Error on this line
button1.Text = buffer.ToString();
...
And I know this function works, I tried the wrapper in Delphi.
Additional information: Object reference not set to an instance of an object
Here is my C# Code:
[DllImport("ActivationCode
public static extern System.IntPtr getActivationCode(string a,string b,StringBuilder c);
...
StringBuilder buffer = new StringBuilder("");
int i = 1;
i = getActivationCode("2058126
button1.Text = buffer.ToString();
...
And I know this function works, I tried the wrapper in Delphi.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Everybody for your help, it now works. I changed in my Delphi code widestrings to PChars and I use StringBuilder in .net
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
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,
public static extern void GetNev(string s);
GetNev("hello");
problem:
Message is: */-/_:? (not "Hello")
what is tha problem???
Thenks Erdosa
ASKER
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
procedure GetNev(s:PChar);stdcall;
begin
ShowMessage(s) ;
end;
and call it this way in c#
[DllImport( "project1.dll", CallingConvention = CallingConvention.StdCall,
public static extern void GetNev(string s);
good luck
2. Instead Int32 use IntPtr
Is it works now?