Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Delphi
Hi all,
I have a Delphi built DLL contaning a function which takes a argument having data type as double. I need to call this function from C# code.Here is the code:-
 
[DllImport("C:\\Smhita20\\Dmp.dll",CallingConvention = CallingConvention.StdCall)]
      public static extern void PRINT_DOC(Double x, Double y, string printText, Int32 scriptCode);
 
And function call is like:-
 
Dmp.PRINT_DOC(X, Y, strTextToPrint, iScriptCode);
where, X & Y are double & strTextToPrint,iScriptCode are string & Int32 resp.
 
While calling the function I am getting the exception "PInvokeStackImbalance was detected".More info on this says that managed PInvoke signature does not match the unmanaged target signature.
 
Int32 & string data types are working for other dll function calls.But I am not sure about what to use for double? I tried float also but same error is coming.
 
Any help would be appreciated.Thanks.
 
Regards,
Saurabh.
Posted 9-Feb-11 2:21am
Edited 10-Feb-11 1:32am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Looks like it is a Single rather than a double.
 
Section 1.2 sort of suggests that: http://ultrastardeluxe.xtremeweb-hosting.net/wiki/doku.php?id=development:c_header_conversion[^]
  Permalink  
Comments
bsaurabh at 10-Feb-11 2:58am
   
Hey,thanks for reply.
As per your suggestion & article I tried with the Single.
Its giving me the access violation exception...
Could there be another solution? Now,I have done with all floating point types in c# ie float,double & single.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I haven't done pascal since 1990, so consider this to be a WAG...
 
Delphi uses the real type (and variants of it), and .Net has float and double. Since you've already tried double, try using float instead and see if that works.
 
Your problem may also be centered aroundf the string parameter. There is no equivalent for Delphi's string object in .Net unless the method you're calling is using string parameters of type PAnsiChar or PWideChar.
 
BTW, I found all this info on google. It's going to be an admittedly tough search, but I think you might find your answer there.
  Permalink  
v2
Comments
SAKryukov at 9-Feb-11 13:47pm
   
John, Real is a bad Delphi type, normally people use "double" and "extended". Double is double.
(I've done Delphi by 2004 or so...)
As far as I remember, Real is intermediate precision not supported by CPU (introduced when math co-processors were emerging, so it is emulated type.
--SA
bsaurabh at 10-Feb-11 3:02am
   
Thanks for reply John.
I have already tried using float but it is not working.
And I am sure about string parameter.It is correct & problem certainly revolves around the float/double.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Probably you should accept PChar parameter rather than string parameter to become compatible with .NET.
 
Double is compatible in both sides.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100