revisited: .NET app using Delphi DLL

I was blaming the VS .NET 4.0 debugger because that was what
changed when the error surfaced.  But consider this code:

{code:C#}
        static void Main()
        {
            for (int i = 30; i < 50; i++)
            {
                AddToList(i);
            }
        }
{code}

Full code at the bottom of the message.

Depending on how I assign the value of StrArray, I may
or may not get the error, but on LrgStr, not StrArray.

{code:Delphi}
procedure AddToList(value: int64); stdcall;
var
   TempStr, LrgStr: String;
begin
   TempStr := IntToStr(value);
   Str := TempStr;
   LrgStr := IntToStr(value * Mult);
   StrArray[value] := IntToStr(value);  // error
//   StrArray[value] := Str;  // no error

   if StrToInt(TempStr) <> (value) then
      WriteLn('Error Message 1: ' + TempStr);
   if StrToInt(Str) <> (value) then
      WriteLn('Error Message 2: ' + Str);
   if StrToInt(LrgStr) <> (value * Mult) then
      WriteLn('Error Message 3: ' + LrgStr);  // error here on LrgStr!
   if StrToInt(StrArray[value]) <> (value) then
      WriteLn('Error Message 4: ' + StrArray[value]);
   WriteLn(value);
end;
{code}

<Results: Every other LrgStr is wrong>
30
Error Message 3: 31313130
31
32
Error Message 3: 33333330
33
34
Error Message 3: 35353530
35
36
Error Message 3: 37373730
37
38
Error Message 3: 39393930
39
etc...
</Results>


Reverse the order of statements and change the behavior.
Now the StrArray is wrong.
{code:Delphi}
   StrArray[value] := IntToStr(value);  // switch these two
   LrgStr := IntToStr(value * Mult);    // this one second this time
   ...
   if StrToInt(StrArray[value]) <> (value) then
      WriteLn('Error Message 4: ' + StrArray[value]);

{code}

<Results: every other StrArray is wrong>
30
Error Message 4: 30
31
32
Error Message 4: 30
33
34
Error Message 4: 30
35
36
Error Message 4: 30
37
38
Error Message 4: 30
39
etc...
</Results>

Looks to me like the problem is in the Delphi code but
is only surfaced somehow by the VS client environment.

Thanks for any suggestions,
Brad.

{code:C#}
using System;
using System.Runtime.InteropServices;

namespace TestNet4interop
{
    static class Program
    {
        [DllImport("TestSimpleLibrary.dll", PreserveSig = true, 
CallingConvention = CallingConvention.StdCall)]
        public static extern void AddToList(long value);

        static void Main()
        {
            for (int i = 30; i < 50; i++)
            {
                AddToList(i);
            }
        }
    }
}
{code}

{code:Delphi}
library TestSimpleLibrary;

uses SysUtils;
{$R *.res}

var
   Str: String;
   StrArray: array[0..100] of String;

const
   Mult = 1010101;

procedure AddToList(value: int64); stdcall;
var
   TempStr, LrgStr: String;
begin
   TempStr := IntToStr(value);
   Str := TempStr;
   StrArray[value] := IntToStr(value);
   LrgStr := IntToStr(value * Mult);
//   StrArray[value] := Str;

   if StrToInt(TempStr) <> (value) then
      WriteLn('Error Message 1: ' + TempStr);
   if StrToInt(Str) <> (value) then
      WriteLn('Error Message 2: ' + Str);
   if StrToInt(LrgStr) <> (value * Mult) then
      WriteLn('Error Message 3: ' + LrgStr);
   if StrToInt(StrArray[value]) <> (value) then
      WriteLn('Error Message 4: ' + StrArray[value]);
   WriteLn(value);
end;

exports AddToList;

begin
end.
{code}

-- 
 


In general, the art of government consists in taking as much money 
as possible from one party of citizens to give to the other.
  -- Voltaire (1764)
0
Brad
1/14/2011 11:01:52 PM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

4 Replies
743 Views

Similar Articles

[PageSpeed] 14

Brad White has brought this to us :
> I was blaming the VS .NET 4.0 debugger because that was what
> changed when the error surfaced.

Forgot to mention:  If you want to reproduce the
problem, it only shows up in VS 2010, targeting
..NET 4.0, running under the debugger.

Somehow that affects the stack inside the Delphi routine.

Thanks,
Brad.

-- 
 


In general, the art of government consists in taking as much money 
as possible from one party of citizens to give to the other.
  -- Voltaire (1764)
0
Brad
1/14/2011 11:02:32 PM
It happens that Rudy Velthuis (TeamB) formulated :

Brad White wrote:

If I get this right, you are calling that code from C#. Are you sure
you actually declared everything correctly (in the attributes), so the
marshalling layer knows how to convert between C# and DLL?

It looks right to me, but absolutely certain?  No.

My only defense is that it works correctly, according
to my standards, when targeting 3.5 or when run
without the debugger.  Or if I change everything
to pass and receive integers.

There is something about value being an int64 (long)
and referencing it, that causes the variable referenced
in the previous line to get trashed.

Here are the declarations again:
procedure AddToList(value: int64); stdcall;
[DllImport("TestSimpleLibrary.dll", PreserveSig = true, 
CallingConvention = CallingConvention.StdCall)]
 public static extern void AddToList(long value);

Thanks for the interest,
Brad.


--



In general, the art of government consists in taking as much money as 
possible from one party of citizens to give to the other.
  -- Voltaire (1764)

-- 
 


In general, the art of government consists in taking as much money 
as possible from one party of citizens to give to the other.
  -- Voltaire (1764)
0
Brad
1/14/2011 11:03:18 PM
Brad White presented the following explanation :

I was blaming the VS .NET 4.0 debugger because that was what
changed when the error surfaced.  But consider this code:



Can anyone reproduce this?
Run it from VS (F5) and get an error message.
Run it without debugging (Ctrl-F5) and no message.

Thanks,
Brad.

-- 
 


In general, the art of government consists in taking as much money 
as possible from one party of citizens to give to the other.
  -- Voltaire (1764)
0
Brad
1/14/2011 11:03:58 PM
Brad White wrote:

> I was blaming the VS .NET 4.0 debugger because that was what
> changed when the error surfaced.  But consider this code:
> 
> {code:C#}
>         static void Main()
>         {
>             for (int i = 30; i < 50; i++)
>             {
>                 AddToList(i);
>             }
>         }
> {code}
> 
> Full code at the bottom of the message.
> 
> Depending on how I assign the value of StrArray, I may
> or may not get the error, but on LrgStr, not StrArray.
> 
> {code:Delphi}
> procedure AddToList(value: int64); stdcall;

You have a parameter size mismatch. As far as I know a C# int is a
32-bit integer, so equivalent to a Delphi integer, not to a int64. So
the .NET part pushes 4 bytes onto the stack and the Delphi routine then
pops 8 bytes off it when it ends, which trashes the stack and fouls up
the return address.


-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
1/15/2011 9:58:40 AM
Reply:

Similar Artilces:

delphi Win32 using delphi .NET dll
Hi, I'm trying to use a delphi.NET dll in delphi.WIN32. I am currently using CodeGear Delphi 2007 with version2(base version) of .NET I can get the dll to import into the WIN32 application the only problem is when i include things such as: "using Classes,DateUtils, SysUtils" in the .NET dll the win32 application will instantly hang when any of the dll functions are called. Any help would be great thanks. Also I have tried this example and it also crashes for me? http://cc.embarcadero.com/Item/22688 -Braden I also found this.. "The problem is that, wehn you instal...

Re: revisited: .NET app using Delphi DLL
Brad White presented the following explanation : > I was blaming the VS .NET 4.0 debugger because that was what > changed when the error surfaced. But consider this code: > Can anyone reproduce this? Run it from VS (F5) and get an error message. Run it without debugging (Ctrl-F5) and no message. Thanks, Brad. --   In general, the art of government consists in taking as much money as possible from one party of citizens to give to the other. -- Voltaire (1764) ...

.net app using Delphi DLL
Is anyone using a Delphi DLL from .NET 4.0 successfully? I get sporadic failures in the DLL if called from a 4.0 app. Called from a 2.0/3.5 app, everything works fine. Anyone else seen this? DLL is compiled with D2007. Reviewed calling conventions and everything seems to be StdCall as appropriate. Thanks, Brad. --   C++: an octopus made by nailing extra legs onto a dog -- unknown "Brad White" <bwhiteDONTINCLUDE@inebraska.com> wrote in message news:310851@forums.embarcadero.com... > Is anyone using a Delphi DLL from .NET 4.0 successfu...

reprise: problems with .NET app using Delphi DLL
We reproduced the problem with a simple test case. The following program generates 0,1,2,3,4,5,6,7,8,9 under 2.0/3.5, but 0,-1,-1,-1,-1,-1,-1,-1,-1 under .NET 4.0. Same results whether compiled with Delphi 2007 or 2010. using System; using System.Runtime.InteropServices; namespace TestNet4interop { static class Program { [DllImport("TestSimpleLibrary.dll", PreserveSig=true, CallingConvention = CallingConvention.StdCall)] public static extern void AddToList(long value); [DllImport("TestSimpleLibrary.dll", PreserveSig=true...

Delphi.NET loading Delphi.Win32 Driver
Hi, What I'm trying to do is marshal an array of cardinal (or integer) back into managed memory from a win32 dll. I know how to pass managed memory into a win32 dll {code} var aa : array of Integer; Buffer : IntPtr; begin SetLength(aa,2); aa[0] := 1; aa[1] := 80; if not Supports(ExtractFilePath(Application.ExeName)+'Win32_Library\SDK_Driver.Win32.io', TypeOf(IMyFunctions), MyFunctions) then Exit; //loads the driver into memory. MyFunctions contains the method names found in the SDK_Driver. Buffer := Marshal.AllocHGlobal(2 * {Marshal.SystemDefaultC...

Converting Delphi for Win32 to Delphi .Net(Prism)
Hi, I am currently migrating a project from Delphi for Win32 to Delphi.net. Part of my code currently goes into a directory and pulls out a random file from this directory and loads the contents of the file for me. This code doesn't seem to work in Delphi.Net. It uses PString and a number of functions in SysUtils that don't seem to be present in Delphi.net's SysUtils file. If anyone can help me please, it would be greatly appreciated! Many thanks, Jonathan Mackey Jonathan Mackey a écrit : > I am currently migrating a project from Delphi for Win32 to &...

Delphi and Delphi for .Net
It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. I would like to know is it true all .Net application is slower than Win32 native applicaiton or it is Delphi for .Net only. Your information is great appreciated, Inung On 2011-06-21 18:20:17 +0100, Inung Huang said: > It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. > I would like to know is it true all .Net application is slower than > Win32 native applicaiton or it is Delphi for .Net only. If you are only running the code in the application once then, yes, yo...

Trying to call a .DLL from Delphi XE (used to work with Delphi 6 :-) )
Hi everyone, I have a problem where perhaps some one could help point me in the right direction. In Delphi 6 I used a stand alone package to train a neural netowrk,. The trained neural network was stored as a .DEF file. The standalone software provided a .DLL and Delphi code to call the network and get the prediced values from your sw. (The .DLL was created in 2002 and apparently is 32 bit.) I have'nt used it for years but am now coming back to it.... I now use Delphi XE on Windows 7 (32 bit) The old .exe's I had compiled from that time work fine. However in Delphi XE ...

Convert Delphi 5 ADO App to Delphi 2007 ADO.NET
I have an appliction tha that is writen in Delphi 5 with ADOConnetion, ADOTable, and ADOQuery components. I need to upgrade to Delphi 2007 and use ADO.NET. I have the program building in with Delphi 2007. Is there anything I need to do to convert the ADO to ADO.NET? > I need to upgrade to Delphi 2007 and use ADO.NET. Why ? -- With best regards, Dmitry Arefiev AnyDAC (www.anydac.com) - Oracle, MS SQL Server, IBM DB2, MySQL, Firebird, Interbase, PostgreSQL, Sybase ASA, SQLite, MS Access, DBX, ODBC, InMem - universal high-speed native data access engine Reasons for ...

Will a dll referenced in an ASP.Net app use the same thread pool as the ASP.Net app?
I have a dll that is referenced by my ASP.Net app. I would like the dll to use a separate thread pool than the thread pool that's available to an ASP.Net app, so that the ASP.Net can better serve the large number of requests that it receives in real world scenario. My question is: Will a separate thread pool be used automatically for dll calls (i.e. calling a method of a class within this dll) OR the same thread pool as the ASP.Net thread pool will be used? Also, if same thread pool gets used, then how can I use a separate thread pool for my dll?sun21170...

How to use Delphi object embeded in a DLL in a C++Builder app
Hi all, I've written a small DLL in Delphi and a test program in Delphi too and everything works fine but I need to write a C++Builder app that will use the DLL too and I don't know / understand how to get access to the object defined in the DLL. MyDLL.dpr {code} library MyDLL; { Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies ...

Using a Delphi-ActiveX-DLL in VB.Net: Problems with strings
(Please note: Yesterday I've already asked the same question in the German forum "Delphi-Praxis", see here: http://www.delphipraxis.net/167520-delphi-activex-dll-vbulletin-net-problem-mit-strings.html) Hi there, I'm trying to develop an ActiveX-DLL which can be used to import data from a server over the network via a special protocol. But since this will be my first ActiveX-DLL I wanted to start with a few, simple tests. So I created an ActiveX-library "testStr32" in Delphi, publishing an object "StringOps" with two methods: "ReverseStr" sh...

Api to skydrive delphi or delphi.net
Dear Good afternoon ... Does anyone have any idea or some documentation on how to implement or delphi.net via delphi api to access skydrive files. Thank you for your help. ...

Using Delphi 7 and Delphi 2010
I had a project in Delphi 7. I worked on it in Delphi 2010. When I try to open the .dpr again in Delphi 7, the IDE toolbar widens across both monitors, and Delphi 7 locks up. It appears that Delphi 2010 modifies the .dpr in a way that makes Delphi 7 unable to open it again. All my forms & units appear to open fine in D7 after D2010, except for the warnings on the ExplicitHeight and ExplicitWidth properties being missing... I can easily recreate the .dpr, but is this expected behavior? Thanks. Tom Field wrote: > I had a project in Delphi 7. > > I worked o...

Web resources about - revisited: .NET app using Delphi DLL - embarcadero.delphi.win32

Resources last updated: 1/22/2016 3:12:55 AM