Using a native C struct in Managed C++
Mar 8, 2013 at 2:50pm UTC
Hi
I am trying to make a C wrapper around some native code so that I can use it in an Azure Web Service. As far as I am aware, Azure only allows pinvoke on C functions and does not allow for interop on to C++.
I am getting some link errors with regard to using a C struct in a C++/CLI wrapper. Here is my code:
PointCloudGenerator.h (Native dll)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifdef MESHGENERATIONNATIVE_EXPORTS
#define MESHGENERATIONNATIVE_API __declspec(dllexport)
#else
#define MESHGENERATIONNATIVE_API __declspec(dllimport)
#endif
struct Vector3d
{
double x;
double y;
double z;
};
extern "C" MESHGENERATIONNATIVE_API void PCGen_Sphere(const Vector3d& centre, double radius, int numPoints, Vector3d* pPointList);
PointCloudGenerator.cpp (Native dll)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "stdafx.h"
#include "PointCloudGenerator.h"
#include <cstdlib>
#include <cmath>
MESHGENERATIONNATIVE_API void PCGen_Sphere(const Vector3d& centre, double radius, int numPoints, Vector3d* pPointList)
{
static double PI = atan(1) * 4;
// First test create a series of true points representing a 3x2 grid
// in X/Y space
for (int i = 0; i < numPoints; ++i)
{
// double angleA = 2 * PI * (double) rand() / RAND_MAX;
double angleA = PI * (double ) rand() / RAND_MAX;
// double angleB = 2 * PI * (double) rand() / RAND_MAX;
double angleB = PI * (double ) rand() / RAND_MAX;
double x = centre.x + radius * cos(angleA) * sin(angleB);
double y = centre.y + radius * sin(angleA) * sin(angleB);
double z = centre.z + radius * cos(angleB);
pPointList[i].x = x;
pPointList[i].y = y;
pPointList[i].z = z;
}
}
Now for my managed wrapper:
PointCloudGeneratorWrap.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "stdafx.h"
#include "PointCloudGeneratorWrap.h"
#include "../MeshGenerationNative/MeshGenerationNative.h"
#include "../MeshGenerationNative/PointCloudGenerator.h"
namespace MeshGenerationWrapper
{
PointCloudGeneratorWrap::PointCloudGeneratorWrap()
{
}
bool
PointCloudGeneratorWrap::CreateSphere()
{
bool success = true ;
int value = fnMeshGenerationNative();
try
{
Vector3d centre;
double radius = 5;
static const int numPoints = 500;
Vector3d points[numPoints];
PCGen_Sphere(centre, radius, numPoints, points);
int hello = 0;
}
catch (...)
{
success = false ;
}
return true ;
}
}
I am getting the following link error when building my managed dll:
1 2
1>PointCloudGeneratorWrap.obj : error LNK2032: metadata inconsistent with COFF symbol table: symbol '?PCGen_Sphere@@$$J0YAXAEBUVector3d@@NHPEAU1@@Z' (0A00000D) mapped to '<unknown symbol>' (06000021) in PointCloudGenerator.obj
1>PointCloudGeneratorWrap.obj : error LNK2019: unresolved external symbol "extern " C" void __cdecl PCGen_Sphere(struct Vector3d const &,double,int,struct Vector3d *)" (?PCGen_Sphere@@$$J0YAXAEBUVector3d@@NHPEAU1@@Z) referenced in function "public: bool __clrcall MeshGenerationWrapper::PointCloudGeneratorWrap::CreateSphere(void)" (?CreateSphere@PointCloudGeneratorWrap@MeshGenerationWrapper@@$$FQE$AAM_NXZ)
I am assuming that I am doing something wrong in exporting my C Struct. Any advice on how to fix this would be greatly appreciated.
Paul
Topic archived. No new replies allowed.