C++ dll in C#

S

sovarschi_zsuzsa

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.
 
C

Claes Bergefall

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

John Torville

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).
 
W

Willy Denoyette [MVP]

Please re-read Nicholas answer, he is talking about the C# code.

Willy.

|> 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).
|
|
 
J

John Torville

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 :)
 
W

Willy Denoyette [MVP]

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.



|> 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 :)
|
|
 
S

sovarschi_zsuzsa

Hy!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top