Problem Calling C++ Lib function from VB.Net

J

JackRazz

I'm trying to call a Managed C++ library function from VB and cannot get it to return
the exponent paramater in Frexp(double floatValue, int* exponent). But I'm getting
the following compile error in the vb code:

'Frexp' has a return type that is not supported or parameter types that are not
supported, which is the passed e variable in frexp(d2, e).

Below is the relevant code from the C++ library. I'm very new to C++ and know its
something very simple. Whats wrong with this C++ code?


Thanks - JackRazz


------------------------------------------------
'This is the test VB code:
Dim d1 As Double
Dim e As Integer
Dim d2 As Double = 16.4
d1 = CLib.MyMath.Frexp(d2, e)

------------------------------------------------
// CLib.h

public:
static double Frexp(double floatValue, int* exponent);


------------------------------------------------
// This is the main DLL file.

double CLib::MyMath::Frexp(double floatValue, int* exponent)
{
return frexp( floatValue, exponent );
};
 
T

Tomas Restrepo \(MVP\)

I'm trying to call a Managed C++ library function from VB and cannot get
it to return
the exponent paramater in Frexp(double floatValue, int* exponent). But I'm getting
the following compile error in the vb code:

'Frexp' has a return type that is not supported or parameter types that are not
supported, which is the passed e variable in frexp(d2, e).

Below is the relevant code from the C++ library. I'm very new to C++ and know its
something very simple. Whats wrong with this C++ code?


Thanks - JackRazz


------------------------------------------------
'This is the test VB code:
Dim d1 As Double
Dim e As Integer
Dim d2 As Double = 16.4
d1 = CLib.MyMath.Frexp(d2, e)

------------------------------------------------
// CLib.h

public:
static double Frexp(double floatValue, int* exponent);

The exponent variable seems to be the problem. You'll need to declare it as
a __gc pointer, and, since it is probably an out only parameter, you'll
likely want something like this:

using namespace System::Runtime::InteropServices;

static double Frexp(double floatValue, [Out] int __gc* exponent)
{
int __pin* e = exponent;

return frexp( floatValue, e );
}
 
J

JackRazz

Tomas,
Thanks for help. I got it working. I had a couple of problems.

First, I couldn't get the function to compile static on the C++ side:

Frexp: 'identifier' : 'static' should not be used on member functions defined at
file scope

From CLib.h
-------------
namespace CLib
{
public __gc class MyMath
{
public:
static double Frexp(double floatValue, int __gc* exponent);
};
}

From CLib.cpp
----------------
static double CLib::MyMath::Frexp(double floatValue, int __gc* exponent)


I'm confused in that the function is under 'public:' I don't mind non static, just
trying to understand. What am I doing wrong?


Second, I got the following for the out attribute. 'CLib.h(19): error C3108: 'out' :
this attribute may not be used in a managed context' I think this one is cut and
dry - don't use [out]. Just want to make sure.

Thanks for all the help. I would never have gotten this in my own.

JackRazz







|
| The exponent variable seems to be the problem. You'll need to declare it as
| a __gc pointer, and, since it is probably an out only parameter, you'll
| likely want something like this:
|
| using namespace System::Runtime::InteropServices;
|
| static double Frexp(double floatValue, [Out] int __gc* exponent)
| {
| int __pin* e = exponent;
|
| return frexp( floatValue, e );
| }
|
| --
| Tomas Restrepo
| (e-mail address removed)
|
|
 
T

Tomas Restrepo \(MVP\)

Hi Jack,
Tomas,
Thanks for help. I got it working. I had a couple of problems.

First, I couldn't get the function to compile static on the C++ side:

Frexp: 'identifier' : 'static' should not be used on member functions defined at
file scope
From CLib.h
-------------
namespace CLib
{
public __gc class MyMath
{
public:
static double Frexp(double floatValue, int __gc* exponent);
};
}

From CLib.cpp

You should only put static on the method declaration, not the definition
(just remove it from CLib.cpp).
I'm confused in that the function is under 'public:' I don't mind non static, just
trying to understand. What am I doing wrong?


Second, I got the following for the out attribute. 'CLib.h(19): error C3108: 'out' :
this attribute may not be used in a managed context' I think this one is cut and
dry - don't use [out]. Just want to make sure.

Case problem: It's [Out] not [out].
Also, make sure you add a using namespace System::Runtime::InteropServices;
 

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