PC Review


Reply
Thread Tools Rate Thread

C++ dll in C#

 
 
sovarschi_zsuzsa
Guest
Posts: n/a
 
      25th Aug 2006
Hello everybody!

I've written a simple C++ dll:

//Simple.dll

#include <stdio.h>

extern "C" {

__declspec(dllexport) long __cdecl Add(long a, long b) {
return (a+b);
}

__declspec(dllexport) long __cdecl Multiple(long a, long b) {
return (a*b);
}

}

This DLL file ready work if I call the functions from C++, but I've
tried to call these functions from a C# project:

using System;
using System.Runtime.InteropServices;

namespace Class1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("Simple.dll")]
//CallingConvention=CallingConvention.Cdecl)]
public static extern long Add(long a, long b);

[DllImport("Simple.dll")]
//CallingConvention=CallingConvention.Cdecl)]
public static extern long Multiple(long a, long b);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
long number1 = 30;
long number2 = 4;

//long number3 = Multiple(number1, number2);
Console.WriteLine ( "Add: " + Add(number1, number2) );
Console.ReadLine();
}
}
}

The result on the screen is: Add: 17179869214

Could somebody help what the problem is? 30 + 4 = 34 not
17179869214.....Is there some problems with parameters?
More thank, Zsuzsa.

 
Reply With Quote
 
 
 
 
Claes Bergefall
Guest
Posts: n/a
 
      25th Aug 2006
Change your C# definitions to use int instead of long. A long in C# is 64
bits. A long in C++ is only 32 bits.

/claes


"sovarschi_zsuzsa" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello everybody!
>
> I've written a simple C++ dll:
>
> //Simple.dll
>
> #include <stdio.h>
>
> extern "C" {
>
> __declspec(dllexport) long __cdecl Add(long a, long b) {
> return (a+b);
> }
>
> __declspec(dllexport) long __cdecl Multiple(long a, long b) {
> return (a*b);
> }
>
> }
>
> This DLL file ready work if I call the functions from C++, but I've
> tried to call these functions from a C# project:
>
> using System;
> using System.Runtime.InteropServices;
>
> namespace Class1
> {
> /// <summary>
> /// Summary description for Class1.
> /// </summary>
> class Class1
> {
> [DllImport("Simple.dll")]
> //CallingConvention=CallingConvention.Cdecl)]
> public static extern long Add(long a, long b);
>
> [DllImport("Simple.dll")]
> //CallingConvention=CallingConvention.Cdecl)]
> public static extern long Multiple(long a, long b);
>
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main(string[] args)
> {
> long number1 = 30;
> long number2 = 4;
>
> //long number3 = Multiple(number1, number2);
> Console.WriteLine ( "Add: " + Add(number1, number2) );
> Console.ReadLine();
> }
> }
> }
>
> The result on the screen is: Add: 17179869214
>
> Could somebody help what the problem is? 30 + 4 = 34 not
> 17179869214.....Is there some problems with parameters?
> More thank, Zsuzsa.
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      25th Aug 2006
Sovarschi,

Why did you comment out the calling convention? That's an absolute must
if you want the stack to be cleaned up correctly.

Also, you should declare your parameters as int. In C++ a long is a
32-bit integer. The .NET equivalent is int. So, your declarations should
be:

[DllImport("Simple.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int Add(int a, int b);

[DllImport("Simple.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int Multiple(int a, int b);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"sovarschi_zsuzsa" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello everybody!
>
> I've written a simple C++ dll:
>
> //Simple.dll
>
> #include <stdio.h>
>
> extern "C" {
>
> __declspec(dllexport) long __cdecl Add(long a, long b) {
> return (a+b);
> }
>
> __declspec(dllexport) long __cdecl Multiple(long a, long b) {
> return (a*b);
> }
>
> }
>
> This DLL file ready work if I call the functions from C++, but I've
> tried to call these functions from a C# project:
>
> using System;
> using System.Runtime.InteropServices;
>
> namespace Class1
> {
> /// <summary>
> /// Summary description for Class1.
> /// </summary>
> class Class1
> {
> [DllImport("Simple.dll")]
> //CallingConvention=CallingConvention.Cdecl)]
> public static extern long Add(long a, long b);
>
> [DllImport("Simple.dll")]
> //CallingConvention=CallingConvention.Cdecl)]
> public static extern long Multiple(long a, long b);
>
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main(string[] args)
> {
> long number1 = 30;
> long number2 = 4;
>
> //long number3 = Multiple(number1, number2);
> Console.WriteLine ( "Add: " + Add(number1, number2) );
> Console.ReadLine();
> }
> }
> }
>
> The result on the screen is: Add: 17179869214
>
> Could somebody help what the problem is? 30 + 4 = 34 not
> 17179869214.....Is there some problems with parameters?
> More thank, Zsuzsa.
>



 
Reply With Quote
 
John Torville
Guest
Posts: n/a
 
      25th Aug 2006
> Also, you should declare your parameters as int. In C++ a long is a
> 32-bit integer.


Not a problem as long as his app is confined to 32-bit Windows (since "long"
is implementation-defined in C++ though it's always a minimum of 32 bits).


 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      25th Aug 2006
Please re-read Nicholas answer, he is talking about the C# code.

Willy.

"John Torville" <no_spam@_nospam.com> wrote in message
news:%(E-Mail Removed)...
|> Also, you should declare your parameters as int. In C++ a long is a
| > 32-bit integer.
|
| Not a problem as long as his app is confined to 32-bit Windows (since
"long"
| is implementation-defined in C++ though it's always a minimum of 32 bits).
|
|


 
Reply With Quote
 
John Torville
Guest
Posts: n/a
 
      25th Aug 2006
> Please re-read Nicholas answer, he is talking about the C# code.

His statement that "in C++ a long is a 32-bit integer" is incorrect. Nor is
a C++ "int" 32 bits for that matter. C++ doesn't define the size of these
types and I was pointing that out more for the benefit of the original op
(it's just good to know, especially when writing portable code). C# has
nothing to do it


 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      25th Aug 2006
I know that the standard doesn't define the size of the int and long types,
but in the real world most (if not all) C++ compilers on windows do define
an int = long = 32 bit. You said it's not a problem as long as his app is
confined to 32 bit windows, which is not true, if the C++ long is a 32 bit
value the .NET value must be an int, not a long.
A .NET long (C# or whatever) is a 64 bit integer value. That means that the
C++ long MUST be an 64 bit integer too, which is not true for most (if not
all) DLL's compiled with a 32 bit compiler on windows 32 bit and that's what
Nicholas assumed when he said that the OP should change it's (C#) long's
into int's.


Willy.



"John Torville" <no_spam@_nospam.com> wrote in message
news:(E-Mail Removed)...
|> Please re-read Nicholas answer, he is talking about the C# code.
|
| His statement that "in C++ a long is a 32-bit integer" is incorrect. Nor
is
| a C++ "int" 32 bits for that matter. C++ doesn't define the size of these
| types and I was pointing that out more for the benefit of the original op
| (it's just good to know, especially when writing portable code). C# has
| nothing to do it
|
|


 
Reply With Quote
 
sovarschi_zsuzsa
Guest
Posts: n/a
 
      1st Sep 2006

Hy!

I have tried to use "int" under C# and it works. Thanks a lot!
Regards, Sovarschi.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:30 AM.