C#, C++, 32 Bit and 64 Bit.

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am trying to use a C++ library in my C# code.

The C++ library is 32 bit. But I am using Windows Vista 64 Bit.
When I tried to access the library in Matlab I got an error because of
that.

Can the C# wrapper be used in 64 bits even if the C++ library is in 32
bits?

I am using the following:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace test.Inds {
[Serializable]
public class JBA : Ind {
protected double fSmh = 14;
protected double fPhe = 0;
protected int iSrsID = 0;
protected int iHhi = -1;

[DllImport("JBA32.dll")]
unsafe public static extern int BA(int iSize, [In] double* pdSg,
double dSmh, double dPhe, [Out] double* pdFilter); [DllImport
("JRS_32.dll")]
public static extern unsafe int BART(double dSrs, double dSmh,
double dPhe, [Out] double* pdOutput, int iDestroy, int* piSrsID);
[Category("Parameters"), Description("")]
[IndParameter(0)]
public double Smh { get { return fSmh; } set { fSmh = value; Init
(); } }

[Category("Parameters"), Description("")]
[IndParameter(1)]
public double Phe { get { return fPhe; } set { fPhe = value; Init
(); } }

protected override void Init() {fName = "JBA (" + fSmh + ", " +
fPhe + ")"; fTitle = "JBA";
Clear();
fCalculate = true;
iHhi = -1;
if (iSrsID != 0) {
unsafe {
fixed (int* ID = &iSrsID) {
BART(0, 0, 0, null, 1, ID);
}
}
iSrsID = 0;
}
}

public JBA() {
Init();
}

public JBA(TimeSrs input, double smooth, double phase) : base
(input) {
fSmh = smooth;
fPhe = phase;
Init();
}

protected override void Calculate(int index) {
double dBA = double.NaN;
if (index <= iHhi)
return;

while (iHhi < index) {
++iHhi;
if (iHhi < fInput.FirstIndex)
continue;
int iErr;
unsafe {
fixed (int* piSrsID = &iSrsID) {
iErr = BART(fInput[iHhi, fOption], Smh, Phe, &dBA, 0,
piSrsID);
}
}
if (iHhi < 30)
dBA = double.NaN;
ReportJError(iErr);
Add(fInput.GetDateTime(index), dBA);
}
}

private static void ReportJError(int iErr) {
switch (iErr) {
case 0:
break;
case -1:
Console.WriteLine("-1");
break;
case 10112:
Console.WriteLine("10112");
break;
case 10114:
Console.WriteLine("10114");
break;
case 10115:
Console.WriteLine("10115");
break;
default:
Console.WriteLine("default");
break;
}
}

~JBA() { // deallocate memory in the DLL when we are done
unsafe {
fixed (int* ID = &iSrsID) {
int iErr = BART(0, 0, 0, null, 1, ID);
ReportJError(iErr);
}
}
}
}
}

Thanks,
Miguel
 
shapper said:
I am trying to use a C++ library in my C# code.

The C++ library is 32 bit. But I am using Windows Vista 64 Bit.
When I tried to access the library in Matlab I got an error because of
that.

Can the C# wrapper be used in 64 bits even if the C++ library is in 32
bits?
No. 64-bit applications cannot load 32-bit DLLs and vice versa -- this is a
Windows limitation, not a .NET issue. .NET actually has it slightly better
because you can change the bitness of an assembly without recompilation,
using the corflags utility.

If Matlab is 64-bits (which it appears to be, as otherwise it couldn't load
your wrapper) you can't use the C++ library unless you put a 32-bit process
between you and the library to which you delegate the requests. This is slow
and clumsy, but the only way to do it if you really must.

If Matlab has a 32-bit version that installs on 64-bit systems you could run
that. Of course, that comes with memory restrictions.
 
Back
Top