views:

1091

answers:

3

I'm building a project along with a Dll.

The Dll must support native code so I declared it as a /clr. My project was initialy also a /clr project and everything was fine. However I'd like to include some NUnit testing so I had to switch my main project from /clr to /clr:pure.

Everything still compiles but any Dll call generates a runtime error. When I revert back to /clr everything is ok

In my Dll, exported functions are declared as follow :

#define DllExport   __declspec( dllexport )
DllExport bool DisplayScan(bool bShow, bool bAllPasses) { }

I also made a .def file containing the real names of all the exported functions

LIBRARY "Controller"
EXPORTS
DisplayScan

From my main project my imports are declared as follow :

#define _DllImport [DllImport("Controller.dll", CallingConvention = CallingConvention::Cdecl)] static
_DllImport bool DisplayScan(bool bShow, bool bAllPasses)

Anyone ever encountered such a problem?

+2  A: 

Ok everything is working now

In fact, it has been working from the beginning.

Moral : don't try to cast a char* into a std::string

Weird thing : its ok in /clr until you return from the function. It crashes right away in /clr:pure

Eric
+2  A: 

Hey Eric,

Basically you are doing something that's not supported; /clr:pure and native DLL exports. As quoted from this MSDN article "pure assemblies cannot export functions that are callable from native functions because entry points in a pure assembly use the __clrcall calling convention."

I'm not sure of the best workaround. However, with a little experimenting, you could probably take advantage of the __clrcall calling convention with the /clr option. Here's a link that may be useful. From what I can gather you should be able to export those managed classes and consume them from within a managed assembly such as your managed NUnit test project, but keep your unmanaged exports there with different method signatures. Keep in mind that as soon as you expose any .net class via an export, it will need to use the __clrcall calling convention.

Regards, Brian

Brian
A: 

your problem is calling conventionCallingConvention = CallingConvention::Cdecl ... define your function like that or use stdcall or clrcall, clecl is for pure C

or problem is here: define that function extern not static

by the way managed C++ from MS is evil, if you want mix managed code and unmanaged. I am not a baby, I don't need garbage collector, I can free my allocated memory.

Does anybody knows how to mix Manaded code and unmanaged, I need structure like this: struct foo { char bar[10]; int a, struct Vector3d; // X,Y,Z doubles }; // I really need this in memory in one sequence (for memcpy function) And I have managed class with managed functions and variables that has this structure only for exporting to flat memory... how can i export this class ?? __dllexport doesnt work on ref class and managed fuctions doesnt work in unmanaged class and if I reference whole namespace I can only see managed classes not unmanaged structures or classes.... Please help

filip